Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/bitcore-node/src/modules/ethereum/api/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,19 @@ export class ETHStateProvider extends InternalStateProvider implements CSP.IChai
async broadcastTransaction(params: CSP.BroadcastTransactionParams) {
const { network, rawTx } = params;
const web3 = await this.getWeb3(network);
return new Promise((resolve, reject) => {
web3.eth
.sendSignedTransaction(rawTx)
.on('transactionHash', resolve)
.on('error', reject);
});
const rawTxs = typeof rawTx === 'string' ? [rawTx] : rawTx;
const txids = new Array<string>();
for (const tx of rawTxs) {
const txid = await new Promise<string>((resolve, reject) => {
web3.eth
.sendSignedTransaction(tx)
.on('transactionHash', resolve)
.on('error', reject)
.catch(e => reject(e));
});
txids.push(txid);
}
return txids.length === 1 ? txids[0] : txids;
}

async getWalletAddresses(walletId: ObjectID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,13 @@ export class InternalStateProvider implements CSP.IChainStateService {

async broadcastTransaction(params: CSP.BroadcastTransactionParams) {
const { chain, network, rawTx } = params;
return new Promise((resolve, reject) => {
this.getRPC(chain, network).sendTransaction(rawTx, (err: any, result: any) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
const txids = new Array<string>();
const rawTxs = typeof rawTx === 'string' ? [rawTx] : rawTx;
for (const tx of rawTxs) {
const txid = await this.getRPC(chain, network).sendTransaction(tx);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs error handling try catch or .catch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for bitcoin, so I actually want it to throw if it fails

txids.push(txid);
}
return txids.length === 1 ? txids[0] : txids;
}

async getCoinsForTx({ chain, network, txid }: { chain: string; network: string; txid: string }) {
Expand Down
74 changes: 33 additions & 41 deletions packages/bitcore-node/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class RPC {
);
}

async asyncCall(method: string, params: any[]) {
return new Promise((resolve, reject) => {
async asyncCall<T>(method: string, params: any[]) {
return new Promise<T>((resolve, reject) => {
this.callMethod(method, params, (err, data) => {
if (err) {
return reject(err);
Expand All @@ -50,59 +50,52 @@ export class RPC {
});
}

getChainTip(callback: CallbackType) {
this.callMethod('getchaintips', [], (err, result) => {
if (err) {
return callback(err);
}
return callback(null, result[0]);
});
async getChainTip() {
const tips = await this.asyncCall<{
height: number;
hash: string;
branchlen: number;
status: string;
}>('getchaintips', []);
return tips[0];
}

getBestBlockHash(callback: CallbackType) {
this.callMethod('getbestblockhash', [], callback);
getBestBlockHash() {
return this.asyncCall('getbestblockhash', []);
}

getBlockHeight(callback: CallbackType) {
this.callMethod('getblockcount', [], callback);
getBlockHeight() {
return this.asyncCall('getblockcount', []);
}

getBlock(hash: string, verbose: boolean, callback: CallbackType) {
this.callMethod('getblock', [hash, verbose], callback);
getBlock(hash: string, verbose: boolean) {
return this.asyncCall('getblock', [hash, verbose]);
}

getBlockHash(height: number, callback: CallbackType) {
this.callMethod('getblockhash', [height], callback);
getBlockHash(height: number) {
return this.asyncCall<string>('getblockhash', [height]);
}

getBlockByHeight(height: number, callback: CallbackType) {
this.getBlockHash(height, (err, hash) => {
if (err) {
return callback(err);
}
this.getBlock(hash, false, callback);
});
async getBlockByHeight(height: number) {
const hash = await this.getBlockHash(height);
return this.getBlock(hash, false);
}

getTransaction(txid: string, callback: CallbackType) {
this.callMethod('getrawtransaction', [txid, true], (err, result) => {
if (err) {
return callback(err);
}
return callback(null, result);
});
getTransaction(txid: string) {
return this.asyncCall('getrawtransaction', [txid, true]);
}

sendTransaction(rawTx: string, callback: CallbackType) {
this.callMethod('sendrawtransaction', [rawTx], callback);
sendTransaction(rawTx: string | Array<string>) {
const txs = typeof rawTx === 'string' ? [rawTx] : rawTx;
return this.asyncCall<string>('sendrawtransaction', txs);
}

decodeScript(hex: string, callback: CallbackType) {
this.callMethod('decodescript', [hex], callback);
decodeScript(hex: string) {
return this.asyncCall('decodescript', [hex]);
}

getWalletAddresses(account: string, callback: CallbackType) {
this.callMethod('getaddressesbyaccount', [account], callback);
getWalletAddresses(account: string) {
return this.asyncCall('getaddressesbyaccount', [account]);
}

async getEstimateSmartFee(target: number) {
Expand Down Expand Up @@ -147,14 +140,13 @@ export class AsyncRPC {

async signrawtx(txs: string): Promise<any> {
try {
const ret = await this.call('signrawtransactionwithwallet', [txs]);
const ret = await this.call('signrawtransactionwithwallet', [txs]);
return ret;
} catch (e) {
if (!e.code || e.code != -32601) return Promise.reject(e);
return await this.call('signrawtransaction', [txs]);
return await this.call('signrawtransaction', [txs]);
}
};

}

async transaction(txid: string, block?: string): Promise<RPCTransaction> {
const args = [txid, true];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export declare namespace CSP {
target: number;
};
export type BroadcastTransactionParams = ChainNetwork & {
rawTx: string;
rawTx: string | Array<string>;
};
export type CreateWalletParams = IWallet;
export type GetWalletParams = ChainNetwork & PubKey;
Expand Down