-
Notifications
You must be signed in to change notification settings - Fork 300
SDK example for initiating a Burn order #6663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,99 @@ | ||||||
| /** | ||||||
| * Initiate a Burn Order for a Stablecoin | ||||||
| * | ||||||
| * Copyright 2025, BitGo, Inc. All Rights Reserved. | ||||||
| */ | ||||||
|
|
||||||
| import * as BitGoJS from 'bitgo'; | ||||||
| import { common } from '@bitgo/sdk-core'; | ||||||
| require('dotenv').config({ path: '../../../.env' }); | ||||||
|
|
||||||
| /** | ||||||
| * Step 1. GET /assets API | ||||||
| * Step 2. GET /constants API | ||||||
| * Step 3. POST /order API | ||||||
| * Step 4. sendMany | ||||||
| */ | ||||||
|
|
||||||
| // Add the parameters here | ||||||
| const environment = 'test'; | ||||||
| const accessToken = ''; | ||||||
| const enterpriseId = ''; | ||||||
| const walletId = ''; // GoAccount Wallet ID | ||||||
| const walletPassphrase = ''; // Wallet passphrase | ||||||
| const usdcoin = 'tfiatusd'; | ||||||
| const stablecoin = 'tbsc:usd1'; | ||||||
| const ofcStablecoin = 'ofctbsc:usd1'; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const fromAmount = '3000000000000000000'; // in base units, e.g., 3000000000000000000 for 3 TBSC:USD1 | ||||||
|
|
||||||
| const bitgo = new BitGoJS.BitGo({ env: environment }); | ||||||
| bitgo.authenticateWithAccessToken({ accessToken: accessToken }); | ||||||
|
|
||||||
| const basecoin = bitgo.coin(ofcStablecoin); | ||||||
|
|
||||||
| async function main() { | ||||||
| try { | ||||||
| // Custom API path helper function | ||||||
| const stablecoinUrl = (path: string) => { | ||||||
| return common.Environments[bitgo.getEnv()].uri + '/api/stablecoin/v1' + path; | ||||||
| }; | ||||||
|
|
||||||
| // STEP - 1: Gets the list of assets | ||||||
| const assets = await bitgo.get(stablecoinUrl('/assets')); | ||||||
| console.log('assets:', assets.body); | ||||||
|
|
||||||
| // Finds the USD and Stablecoin assets from above response to use in initiating the burn order | ||||||
| const usdAsset = assets.body.find((asset: any) => asset.token === usdcoin); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Is it possible to avoid usage of any? |
||||||
| const stablecoinAsset = assets.body.find((asset: any) => asset.token === stablecoin); | ||||||
| if (!usdAsset || !stablecoinAsset) { | ||||||
| throw new Error(`Asset ${usdcoin}/${stablecoin} not found`); | ||||||
| } | ||||||
|
|
||||||
| // STEP - 2: Gets the constants for the stablecoin | ||||||
| const constants = await bitgo.get(stablecoinUrl(`/${stablecoin}/constants`)).send(); | ||||||
| console.log('constants:', constants.body); | ||||||
| // Extract the treasury account wallet ID from the constants response | ||||||
| const trustAccountWalletId = constants.body.trustAccountWalletId; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if (!trustAccountWalletId) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| throw new Error(`Trust account wallet ID not found for ${stablecoin}`); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| // STEP - 3: Initiates the burn order | ||||||
| const orderRequestBody = { | ||||||
| sourceWalletId: walletId, | ||||||
| destinationWalletId: walletId, | ||||||
| destinationType: "go_account", | ||||||
| fromAssetId: stablecoinAsset.id, | ||||||
| toAssetId: usdAsset.id, | ||||||
| fromAmount, | ||||||
| type: "burn", | ||||||
| }; | ||||||
| const postOrderResponse = await bitgo.post(stablecoinUrl(`/enterprise/${enterpriseId}/order`)).send(orderRequestBody); | ||||||
| const newOrder = postOrderResponse.body; | ||||||
| console.log('Order created:', newOrder); | ||||||
|
|
||||||
| // STEP - 4: Sends the transaction to the Treasury Account using sendMany | ||||||
| const walletInstance = await basecoin.wallets().get({ id: walletId }); | ||||||
| const transaction = await walletInstance.sendMany({ | ||||||
| recipients: [ | ||||||
| { | ||||||
| address: trustAccountWalletId, | ||||||
| amount: fromAmount, | ||||||
| } | ||||||
| ], | ||||||
| sequenceId: newOrder.id, // IMPORTANT: Use the order ID as the sequence ID | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As part of input sanitization, we can check for nullity of order id. Something FE can also do if not already doing. |
||||||
| walletPassphrase, | ||||||
| }); | ||||||
| console.log('Burn order process completed successfully!'); | ||||||
| console.log('New Transaction:', JSON.stringify(transaction, null, 4)); | ||||||
|
|
||||||
| const order = await bitgo.get(stablecoinUrl(`/enterprise/${enterpriseId}/orders?ids=${newOrder.id}`)).send(); | ||||||
| console.log('Order details:', JSON.stringify(order.body, null, 4)); | ||||||
|
|
||||||
| } catch (error) { | ||||||
| console.error('Error in burn order process:', error); | ||||||
| throw error; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| main().catch((e) => console.error(e)); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| /** | ||
| * Initiate a Mint Order for a Stablecoin | ||
| * | ||
| * Copyright 2023, BitGo, Inc. All Rights Reserved. | ||
| * Copyright 2025, BitGo, Inc. All Rights Reserved. | ||
| */ | ||
|
|
||
| import * as BitGoJS from 'bitgo'; | ||
|
|
@@ -61,7 +61,7 @@ async function main() { | |
| // STEP - 3: Initiates the mint order | ||
| const orderRequestBody = { | ||
| sourceWalletId: walletId, | ||
| destinationWalletId: trustAccountWalletId, | ||
| destinationWalletId: walletId, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| destinationType: "go_account", | ||
| toAssetId: stablecoinAsset.id, | ||
| fromAssetId: usdAsset.id, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other nits: