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
20 changes: 13 additions & 7 deletions modules/sdk-api/src/v1/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,19 @@ exports.createTransaction = function (params) {
if (bitgoFeeInfo) {
return;
}
return params.wallet.getBitGoFee({ amount: totalOutputAmount, instant: params.instant }).then(function (result) {
if (result && result.fee > 0) {
bitgoFeeInfo = {
amount: result.fee,
};
}
});
return params.wallet
.getBitGoFee({
amount: totalOutputAmount,
instant: params.instant,
recipients: params.recipients?.map((r: any) => r.address).filter(Boolean) ?? [],
})
.then(function (result) {
if (result && result.fee > 0) {
bitgoFeeInfo = {
amount: result.fee,
};
}
});
}).then(function () {
if (bitgoFeeInfo && bitgoFeeInfo.amount > 0) {
totalAmount += bitgoFeeInfo.amount;
Expand Down
9 changes: 6 additions & 3 deletions modules/sdk-api/src/v1/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2555,9 +2555,12 @@ Wallet.prototype.getBitGoFee = function (params, callback) {
if (params.instant && !_.isBoolean(params.instant)) {
throw new Error('invalid instant argument');
}
return Promise.resolve(this.bitgo.get(this.url('/billing/fee')).query(params).result())
.then(callback)
.catch(callback);
const { recipients, ...baseParams } = params;
let req = this.bitgo.get(this.url('/billing/fee')).query(baseParams);
if (Array.isArray(recipients) && recipients.length > 0) {
req = req.query({ 'recipients[]': recipients });
}
return Promise.resolve(req.result()).then(callback).catch(callback);
};

/*
Expand Down
67 changes: 67 additions & 0 deletions modules/sdk-api/test/unit/v1/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1823,4 +1823,71 @@ describe('Wallet Prototype Methods', function () {
});
});
});

describe('getBitGoFee', function () {
let bgUrl: string;
let wallet: any;

before(function () {
nock.pendingMocks().should.be.empty();
const prodBitgo = new BitGoAPI({ env: 'prod', clientConstants: { constants: {} } });
bgUrl = common.Environments[prodBitgo.getEnv()].uri;
wallet = new Wallet(prodBitgo, {
id: '2NCoSfHH6Ls4CdTS5QahgC9k7x9RfXeSwY4',
private: { keychains: [userKeypair, backupKeypair, bitgoKey] },
});
});

afterEach(function () {
nock.cleanAll();
});

it('sends amount and instant without recipients when recipients array is empty', async function () {
const scope = nock(bgUrl)
.get(`/api/v1/wallet/${wallet.id()}/billing/fee`)
.query({ amount: '100000', instant: 'false' })
.reply(200, { fee: 1000 });

const result = await wallet.getBitGoFee({ amount: 100000, instant: false });
result.fee.should.equal(1000);
scope.isDone().should.be.true();
});

it('sends recipients[] query params when recipients are provided', async function () {
const addr1 = '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy';
const addr2 = '3FZbgi29cpjq2GjdwV8eyHuJJnkLtktZc5';

const scope = nock(bgUrl)
.get(`/api/v1/wallet/${wallet.id()}/billing/fee`)
.query((query) => {
const recipientsParam = query['recipients[]'];
const recipientsList = Array.isArray(recipientsParam) ? recipientsParam : [recipientsParam];
return query.amount === '100000' && recipientsList.includes(addr1) && recipientsList.includes(addr2);
})
.reply(200, { fee: 0 });

const result = await wallet.getBitGoFee({ amount: 100000, recipients: [addr1, addr2] });
result.fee.should.equal(0);
scope.isDone().should.be.true();
});

it('omits recipients[] when recipients array is empty', async function () {
const scope = nock(bgUrl)
.get(`/api/v1/wallet/${wallet.id()}/billing/fee`)
.query((query) => query.amount === '200000' && !('recipients[]' in query))
.reply(200, { fee: 500 });

const result = await wallet.getBitGoFee({ amount: 200000, recipients: [] });
result.fee.should.equal(500);
scope.isDone().should.be.true();
});

it('throws when amount is not a number', function () {
(() => wallet.getBitGoFee({ amount: 'bad' })).should.throw('invalid amount argument');
});

it('throws when instant is not a boolean', function () {
(() => wallet.getBitGoFee({ amount: 100, instant: 'yes' })).should.throw('invalid instant argument');
});
});
});
Loading