|
| 1 | +/** |
| 2 | + * Initiate a Burn Order for a Stablecoin |
| 3 | + * |
| 4 | + * Copyright 2025, BitGo, Inc. All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +import * as BitGoJS from 'bitgo'; |
| 8 | +import { common } from '@bitgo/sdk-core'; |
| 9 | +require('dotenv').config({ path: '../../../.env' }); |
| 10 | + |
| 11 | +/** |
| 12 | + * Step 1. GET /assets API |
| 13 | + * Step 2. GET /constants API |
| 14 | + * Step 3. POST /order API |
| 15 | + * Step 4. sendMany |
| 16 | + */ |
| 17 | + |
| 18 | +// Add the parameters here |
| 19 | +const environment = 'test'; |
| 20 | +const accessToken = ''; |
| 21 | +const enterpriseId = ''; |
| 22 | +const walletId = ''; // GoAccount Wallet ID |
| 23 | +const walletPassphrase = ''; // Wallet passphrase |
| 24 | +const usdcoin = 'tfiatusd'; |
| 25 | +const stablecoin = 'tbsc:usd1'; |
| 26 | +const ofcStablecoin = 'ofctbsc:usd1'; |
| 27 | +const fromAmount = '3000000000000000000'; // in base units, e.g., 3000000000000000000 for 3 TBSC:USD1 |
| 28 | + |
| 29 | +const bitgo = new BitGoJS.BitGo({ env: environment }); |
| 30 | +bitgo.authenticateWithAccessToken({ accessToken: accessToken }); |
| 31 | + |
| 32 | +const basecoin = bitgo.coin(ofcStablecoin); |
| 33 | + |
| 34 | +async function main() { |
| 35 | + try { |
| 36 | + // Custom API path helper function |
| 37 | + const stablecoinUrl = (path: string) => { |
| 38 | + return common.Environments[bitgo.getEnv()].uri + '/api/stablecoin/v1' + path; |
| 39 | + }; |
| 40 | + |
| 41 | + // STEP - 1: Gets the list of assets |
| 42 | + const assets = await bitgo.get(stablecoinUrl('/assets')); |
| 43 | + console.log('assets:', assets.body); |
| 44 | + |
| 45 | + // Finds the USD and Stablecoin assets from above response to use in initiating the burn order |
| 46 | + const usdAsset = assets.body.find((asset: any) => asset.token === usdcoin); |
| 47 | + const stablecoinAsset = assets.body.find((asset: any) => asset.token === stablecoin); |
| 48 | + if (!usdAsset || !stablecoinAsset) { |
| 49 | + throw new Error(`Asset ${usdcoin}/${stablecoin} not found`); |
| 50 | + } |
| 51 | + |
| 52 | + // STEP - 2: Gets the constants for the stablecoin |
| 53 | + const constants = await bitgo.get(stablecoinUrl(`/${stablecoin}/constants`)).send(); |
| 54 | + console.log('constants:', constants.body); |
| 55 | + // Extract the treasury account wallet ID from the constants response |
| 56 | + const trustAccountWalletId = constants.body.trustAccountWalletId; |
| 57 | + if (!trustAccountWalletId) { |
| 58 | + throw new Error(`Trust account wallet ID not found for ${stablecoin}`); |
| 59 | + } |
| 60 | + |
| 61 | + // STEP - 3: Initiates the burn order |
| 62 | + const orderRequestBody = { |
| 63 | + sourceWalletId: walletId, |
| 64 | + destinationWalletId: walletId, |
| 65 | + destinationType: "go_account", |
| 66 | + fromAssetId: stablecoinAsset.id, |
| 67 | + toAssetId: usdAsset.id, |
| 68 | + fromAmount, |
| 69 | + type: "burn", |
| 70 | + }; |
| 71 | + const postOrderResponse = await bitgo.post(stablecoinUrl(`/enterprise/${enterpriseId}/order`)).send(orderRequestBody); |
| 72 | + const newOrder = postOrderResponse.body; |
| 73 | + console.log('Order created:', newOrder); |
| 74 | + |
| 75 | + // STEP - 4: Sends the transaction to the Treasury Account using sendMany |
| 76 | + const walletInstance = await basecoin.wallets().get({ id: walletId }); |
| 77 | + const transaction = await walletInstance.sendMany({ |
| 78 | + recipients: [ |
| 79 | + { |
| 80 | + address: trustAccountWalletId, |
| 81 | + amount: fromAmount, |
| 82 | + } |
| 83 | + ], |
| 84 | + sequenceId: newOrder.id, // IMPORTANT: Use the order ID as the sequence ID |
| 85 | + walletPassphrase, |
| 86 | + }); |
| 87 | + console.log('Burn order process completed successfully!'); |
| 88 | + console.log('New Transaction:', JSON.stringify(transaction, null, 4)); |
| 89 | + |
| 90 | + const order = await bitgo.get(stablecoinUrl(`/enterprise/${enterpriseId}/orders?ids=${newOrder.id}`)).send(); |
| 91 | + console.log('Order details:', JSON.stringify(order.body, null, 4)); |
| 92 | + |
| 93 | + } catch (error) { |
| 94 | + console.error('Error in burn order process:', error); |
| 95 | + throw error; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +main().catch((e) => console.error(e)); |
0 commit comments