Skip to content

Commit 092d3fc

Browse files
Merge pull request #9028 from BitGo/t1-3579-waive-paygo-fees-v1-to-v2-migration
feat(sdk-api): pass recipient addresses to v1 billing fee endpoint for PayGo migration waiver
2 parents ec131f7 + ff0cbb7 commit 092d3fc

3 files changed

Lines changed: 86 additions & 10 deletions

File tree

modules/sdk-api/src/v1/transactionBuilder.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,19 @@ exports.createTransaction = function (params) {
288288
if (bitgoFeeInfo) {
289289
return;
290290
}
291-
return params.wallet.getBitGoFee({ amount: totalOutputAmount, instant: params.instant }).then(function (result) {
292-
if (result && result.fee > 0) {
293-
bitgoFeeInfo = {
294-
amount: result.fee,
295-
};
296-
}
297-
});
291+
return params.wallet
292+
.getBitGoFee({
293+
amount: totalOutputAmount,
294+
instant: params.instant,
295+
recipients: params.recipients?.map((r: any) => r.address).filter(Boolean) ?? [],
296+
})
297+
.then(function (result) {
298+
if (result && result.fee > 0) {
299+
bitgoFeeInfo = {
300+
amount: result.fee,
301+
};
302+
}
303+
});
298304
}).then(function () {
299305
if (bitgoFeeInfo && bitgoFeeInfo.amount > 0) {
300306
totalAmount += bitgoFeeInfo.amount;

modules/sdk-api/src/v1/wallet.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,9 +2555,12 @@ Wallet.prototype.getBitGoFee = function (params, callback) {
25552555
if (params.instant && !_.isBoolean(params.instant)) {
25562556
throw new Error('invalid instant argument');
25572557
}
2558-
return Promise.resolve(this.bitgo.get(this.url('/billing/fee')).query(params).result())
2559-
.then(callback)
2560-
.catch(callback);
2558+
const { recipients, ...baseParams } = params;
2559+
let req = this.bitgo.get(this.url('/billing/fee')).query(baseParams);
2560+
if (Array.isArray(recipients) && recipients.length > 0) {
2561+
req = req.query({ 'recipients[]': recipients });
2562+
}
2563+
return Promise.resolve(req.result()).then(callback).catch(callback);
25612564
};
25622565

25632566
/*

modules/sdk-api/test/unit/v1/wallet.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,4 +1823,71 @@ describe('Wallet Prototype Methods', function () {
18231823
});
18241824
});
18251825
});
1826+
1827+
describe('getBitGoFee', function () {
1828+
let bgUrl: string;
1829+
let wallet: any;
1830+
1831+
before(function () {
1832+
nock.pendingMocks().should.be.empty();
1833+
const prodBitgo = new BitGoAPI({ env: 'prod', clientConstants: { constants: {} } });
1834+
bgUrl = common.Environments[prodBitgo.getEnv()].uri;
1835+
wallet = new Wallet(prodBitgo, {
1836+
id: '2NCoSfHH6Ls4CdTS5QahgC9k7x9RfXeSwY4',
1837+
private: { keychains: [userKeypair, backupKeypair, bitgoKey] },
1838+
});
1839+
});
1840+
1841+
afterEach(function () {
1842+
nock.cleanAll();
1843+
});
1844+
1845+
it('sends amount and instant without recipients when recipients array is empty', async function () {
1846+
const scope = nock(bgUrl)
1847+
.get(`/api/v1/wallet/${wallet.id()}/billing/fee`)
1848+
.query({ amount: '100000', instant: 'false' })
1849+
.reply(200, { fee: 1000 });
1850+
1851+
const result = await wallet.getBitGoFee({ amount: 100000, instant: false });
1852+
result.fee.should.equal(1000);
1853+
scope.isDone().should.be.true();
1854+
});
1855+
1856+
it('sends recipients[] query params when recipients are provided', async function () {
1857+
const addr1 = '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy';
1858+
const addr2 = '3FZbgi29cpjq2GjdwV8eyHuJJnkLtktZc5';
1859+
1860+
const scope = nock(bgUrl)
1861+
.get(`/api/v1/wallet/${wallet.id()}/billing/fee`)
1862+
.query((query) => {
1863+
const recipientsParam = query['recipients[]'];
1864+
const recipientsList = Array.isArray(recipientsParam) ? recipientsParam : [recipientsParam];
1865+
return query.amount === '100000' && recipientsList.includes(addr1) && recipientsList.includes(addr2);
1866+
})
1867+
.reply(200, { fee: 0 });
1868+
1869+
const result = await wallet.getBitGoFee({ amount: 100000, recipients: [addr1, addr2] });
1870+
result.fee.should.equal(0);
1871+
scope.isDone().should.be.true();
1872+
});
1873+
1874+
it('omits recipients[] when recipients array is empty', async function () {
1875+
const scope = nock(bgUrl)
1876+
.get(`/api/v1/wallet/${wallet.id()}/billing/fee`)
1877+
.query((query) => query.amount === '200000' && !('recipients[]' in query))
1878+
.reply(200, { fee: 500 });
1879+
1880+
const result = await wallet.getBitGoFee({ amount: 200000, recipients: [] });
1881+
result.fee.should.equal(500);
1882+
scope.isDone().should.be.true();
1883+
});
1884+
1885+
it('throws when amount is not a number', function () {
1886+
(() => wallet.getBitGoFee({ amount: 'bad' })).should.throw('invalid amount argument');
1887+
});
1888+
1889+
it('throws when instant is not a boolean', function () {
1890+
(() => wallet.getBitGoFee({ amount: 100, instant: 'yes' })).should.throw('invalid instant argument');
1891+
});
1892+
});
18261893
});

0 commit comments

Comments
 (0)