Skip to content

Commit

Permalink
add allowance check
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed Jun 27, 2024
1 parent c072586 commit ad54b08
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/bitcore-wallet-service/src/lib/chain/eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export class EthChain implements IChain {
const txpTotalAmount =
(opts.multisigContractAddress || opts.tokenAddress) && txp.payProUrl
? getInvoiceValue(txp)
: txp.getTotalAmount(opts);
: txp.getTotalAmount();

if (totalAmount < txpTotalAmount) {
return cb(Errors.INSUFFICIENT_FUNDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export class TxProposal {
* @return {Number} total amount of all outputs excluding change output
*/
getTotalAmount() {
return _.sumBy(this.outputs, 'amount');
return Number((this.outputs || []).reduce((total, o) => total += BigInt(o.amount), 0n));
}

/**
Expand Down
23 changes: 21 additions & 2 deletions packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,7 @@ export class WalletService implements IWalletService {
return next();
},
async next => {
if (_.isNumber(opts.fee) && !_.isEmpty(opts.inputs)) return next();
if (!isNaN(opts.fee) && (opts.inputs || []).length > 0) return next();
try {
({ feePerKb, gasPrice, maxGasFee, priorityGasFee, gasLimit, fee } = await ChainService.getFee(this, wallet, opts));
} catch (error) {
Expand Down Expand Up @@ -2677,6 +2677,25 @@ export class WalletService implements IWalletService {
if (opts.dryRun) return next();
this._store(wallet, txp.escrowAddress, next, true);
},
next => {
if (!txp.multiSendContractAddress || !txp.tokenAddress) {
return next();
}
// Check that the multisend contract is approved in the token contract for the total amount
const bc = this._getBlockchainExplorer(wallet.chain, wallet.network);
if (!bc) return cb(new Error('Could not get blockchain explorer instance'));
bc.getTokenAllowance({
tokenAddress: txp.tokenAddress,
ownerAddress: txp.from,
spenderAddress: txp.multiSendContractAddress
}, (err, allowance) => {
if (err) { return next(err); }
if (BigInt(allowance) < BigInt(txp.getTotalAmount())) {
return next(new Error(`Insufficient token allowance. Allowed: ${BigInt(allowance)}, Want: ${BigInt(txp.getTotalAmount())}`));
}
return next();
});
},
next => {
if (!changeAddress || wallet.singleAddress || opts.dryRun || opts.changeAddress) return next();

Expand Down Expand Up @@ -4093,7 +4112,7 @@ export class WalletService implements IWalletService {
);
}

getTxHistoryV8(bc, wallet, opts, skip, limit, cb) {
getTxHistoryV8(bc: V8, wallet, opts, skip, limit, cb) {
let bcHeight,
bcHash,
sinceTx,
Expand Down
5 changes: 3 additions & 2 deletions packages/bitcore-wallet-service/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"lib": [
"es5",
"es6",
"es2017"
"es2017",
"es2020"
],
"noImplicitAny": false,
"removeComments": true,
Expand All @@ -14,7 +15,7 @@
"experimentalDecorators": true,
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es5",
"target": "ES2020",
"typeRoots": [
"./node_modules/@types"
],
Expand Down

0 comments on commit ad54b08

Please sign in to comment.