Skip to content

Commit

Permalink
feat: added getUTXO to wrap around fetchUTXO
Browse files Browse the repository at this point in the history
  • Loading branch information
0x31 committed Jul 13, 2020
1 parent b6c5599 commit e146c94
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
13 changes: 9 additions & 4 deletions src/handlers/BCH/BCHHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class BCHHandler implements Handler {
this.privateKey, changeAddress, toAddress, valueIn, utxos, { ...options, signFlag: bitcoin.Transaction.SIGHASH_SINGLE | bitcoin.Transaction.SIGHASH_BITCOINCASHBIP143 },
);

txHash = await retryNTimes(() => fallback(_apiFallbacks.broadcastTransaction(this.testnet, built.toHex())), 5);
txHash = await retryNTimes(() => fallback(_apiFallbacks.broadcastTransaction(this.testnet, built.toHex())), 3);

promiEvent.emit('transactionHash', txHash);
promiEvent.resolve(txHash);
Expand All @@ -134,7 +134,7 @@ export class BCHHandler implements Handler {
return promiEvent;
};

private readonly _getConfirmations = (txHash: string) => retryNTimes(() => fallback(_apiFallbacks.fetchConfirmations(this.testnet, txHash)), 5);
private readonly _getConfirmations = (txHash: string) => retryNTimes(() => fallback(_apiFallbacks.fetchConfirmations(this.testnet, txHash)), 2);
private readonly _bitgoNetwork = () => this.testnet ? bitcoin.networks.bitcoincashTestnet : bitcoin.networks.bitcoincash;
}

Expand All @@ -143,11 +143,16 @@ export const getUTXOs = async (testnet: boolean, options: { address: string, con
const confirmations = options.confirmations || 0;

const endpoints = _apiFallbacks.fetchUTXOs(testnet, address, confirmations);
const utxos = await retryNTimes(() => fallback(endpoints), 5);
const utxos = await retryNTimes(() => fallback(endpoints), 2);
return utxos;
};

export const getConfirmations = async (testnet: boolean, txHash: string): Promise<number> => {
const endpoints = _apiFallbacks.fetchConfirmations(testnet, txHash);
return retryNTimes(() => fallback(endpoints), 5);
return retryNTimes(() => fallback(endpoints), 2);
};

export const getUTXO = async (testnet: boolean, txHash: string, vOut: number): Promise<UTXO> => {
const endpoints = _apiFallbacks.fetchUTXO(testnet, txHash, vOut);
return retryNTimes(() => fallback(endpoints), 2);
};
12 changes: 8 additions & 4 deletions src/handlers/BTC/BTCHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class BTCHandler implements Handler {
this.privateKey, changeAddress, to, valueIn, utxos, options,
);

txHash = await retryNTimes(() => fallback(_apiFallbacks.broadcastTransaction(this.testnet, built.toHex())), 5);
txHash = await retryNTimes(() => fallback(_apiFallbacks.broadcastTransaction(this.testnet, built.toHex())), 3);

promiEvent.emit('transactionHash', txHash);
promiEvent.resolve(txHash);
Expand All @@ -135,18 +135,22 @@ export class BTCHandler implements Handler {
return promiEvent;
};

private readonly _getConfirmations = (txHash: string) => retryNTimes(() => fallback(_apiFallbacks.fetchConfirmations(this.testnet, txHash)), 5);
private readonly _getConfirmations = (txHash: string) => retryNTimes(() => fallback(_apiFallbacks.fetchConfirmations(this.testnet, txHash)), 2);
}

export const getUTXOs = async (testnet: boolean, options: { address: string, confirmations?: number }): Promise<readonly UTXO[]> => {
const confirmations = options && options.confirmations !== undefined ? options.confirmations : 0;

const endpoints = _apiFallbacks.fetchUTXOs(testnet, options.address, confirmations);
return retryNTimes(() => fallback(endpoints), 5);
return retryNTimes(() => fallback(endpoints), 2);
};

export const getConfirmations = async (testnet: boolean, txHash: string): Promise<number> => {
const endpoints = _apiFallbacks.fetchConfirmations(testnet, txHash);
return retryNTimes(() => fallback(endpoints), 5);
return retryNTimes(() => fallback(endpoints), 2);
};

export const getUTXO = async (testnet: boolean, txHash: string, vOut: number): Promise<UTXO> => {
const endpoints = _apiFallbacks.fetchUTXO(testnet, txHash, vOut);
return retryNTimes(() => fallback(endpoints), 2);
};
13 changes: 9 additions & 4 deletions src/handlers/ZEC/ZECHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class ZECHandler implements Handler {
{ ...options, version: 4, versionGroupID: this.testnet ? 0xf5b9230b : 0x892F2085 },
);

txHash = await retryNTimes(() => fallback(_apiFallbacks.broadcastTransaction(this.testnet, built.toHex())), 5);
txHash = await retryNTimes(() => fallback(_apiFallbacks.broadcastTransaction(this.testnet, built.toHex())), 3);

promiEvent.emit('transactionHash', txHash);
promiEvent.resolve(txHash);
Expand All @@ -163,15 +163,20 @@ export class ZECHandler implements Handler {
return promiEvent;
};

private readonly _getConfirmations = (txHash: string) => retryNTimes(() => fallback(_apiFallbacks.fetchConfirmations(this.testnet, txHash)), 5);
private readonly _getConfirmations = (txHash: string) => retryNTimes(() => fallback(_apiFallbacks.fetchConfirmations(this.testnet, txHash)), 2);
}

export const getUTXOs = async (testnet: boolean, options: { address: string, confirmations?: number }): Promise<readonly UTXO[]> => {
const confirmations = options && options.confirmations !== undefined ? options.confirmations : 0;
return retryNTimes(() => fallback(_apiFallbacks.fetchUTXOs(testnet, options.address, confirmations)), 5);
return retryNTimes(() => fallback(_apiFallbacks.fetchUTXOs(testnet, options.address, confirmations)), 2);
};

export const getConfirmations = async (testnet: boolean, txHash: string): Promise<number> => {
const endpoints = _apiFallbacks.fetchConfirmations(testnet, txHash);
return retryNTimes(() => fallback(endpoints), 5);
return retryNTimes(() => fallback(endpoints), 2);
};

export const getUTXO = async (testnet: boolean, txHash: string, vOut: number): Promise<UTXO> => {
const endpoints = _apiFallbacks.fetchUTXO(testnet, txHash, vOut);
return retryNTimes(() => fallback(endpoints), 2);
};

0 comments on commit e146c94

Please sign in to comment.