-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4209 from BitGo/rbf-test
feat: add an example to test bitcoin replace by fee feature
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const rbfConfig = { | ||
accessToken: '', | ||
walletId: '', | ||
walletPassphrase: '', | ||
env: 'test', | ||
coin: 'tbtc', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Send a transaction to be replaced from a multi-sig wallet at BitGo. | ||
* Then quickly send a replacement transaction with a higher fee. | ||
* | ||
* Copyright 2024, BitGo, Inc. All Rights Reserved. | ||
*/ | ||
import { BitGo, EnvironmentName } from '../../../../modules/bitgo'; | ||
import { rbfConfig } from './config'; | ||
|
||
const bitgo = new BitGo({ env: rbfConfig.env as EnvironmentName }); | ||
bitgo.authenticateWithAccessToken({ accessToken: rbfConfig.accessToken }); | ||
|
||
const RECEIVE_ADDRESS = ''; | ||
|
||
async function getWallet() { | ||
return await bitgo.coin(rbfConfig.coin).wallets().get({ id: rbfConfig.walletId }); | ||
} | ||
|
||
function delay(ms: number) { | ||
console.log('Start delay for ', ms / 1000, 'secs'); | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
async function main() { | ||
console.log('Starting...'); | ||
|
||
const wallet = await getWallet(); | ||
const txToBeReplaced = await wallet.sendMany({ | ||
recipients: [ | ||
{ | ||
amount: '50000', | ||
address: RECEIVE_ADDRESS, | ||
}, | ||
], | ||
isReplaceableByFee: true, | ||
feeRate: 1000, // 1 sat/vbyte | ||
walletPassphrase: rbfConfig.walletPassphrase, | ||
}); | ||
console.log('Transaction to be replaced: ', txToBeReplaced); | ||
|
||
await delay(45_000); // Delay for 45secs so that the original tx gets propagated to the network | ||
console.log('End delay'); | ||
|
||
const idOfTxToBeReplaced = txToBeReplaced.txid; | ||
const replacementTx = await wallet.sendMany({ | ||
recipients: [], | ||
rbfTxIds: [idOfTxToBeReplaced], | ||
feeMultiplier: 3.5, | ||
walletPassphrase: rbfConfig.walletPassphrase, | ||
}); | ||
console.log('Replacement Transaction: ', replacementTx); | ||
} | ||
|
||
main().catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |