Skip to content

Commit 6ed0aaf

Browse files
authored
Merge pull request #6781 from BitGo/TMS-1375
chore: use the treasuryAccount id from assets response instead of the constants API
2 parents 0ecb0a7 + 60132ce commit 6ed0aaf

File tree

2 files changed

+30
-62
lines changed

2 files changed

+30
-62
lines changed

examples/ts/stablecoin/initiate-burn-order.ts

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ require('dotenv').config({ path: '../../../.env' });
1212
* Step 1. GET /assets API
1313
* Retrieves:
1414
* - The list of assets supported for burning
15+
* - Treasury Account Wallet ID for the specified stablecoin, to which the funds need to be sent
1516
*
16-
* Step 2. GET /{token}/constants API
17-
* Retrieves:
18-
* - Treasury Account Wallet ID for the specified stablecoin
19-
*
20-
* Step 3. POST /order API
17+
* Step 2. POST /order API
2118
* Creates a Burn Order with the specified parameters
2219
*
23-
* Step 4. Transfer the assets to be burnt, to the treasury account
20+
* Step 3. Transfer the assets to be burnt, to the treasury account
2421
* Initiates the send transaction to the Treasury Account using sendMany
2522
*/
2623

@@ -34,7 +31,7 @@ const walletPassphrase = ''; // Wallet passphrase
3431
const usdcoin = 'tfiatusd'; // USD asset token
3532
const stablecoin = 'tbsc:usd1'; // Stablecoin to burn
3633
const ofcStablecoin = `ofc${stablecoin}`; // ofc stablecoin (for initiating the send from the specified GoAccount wallet to the Treasury Go Account)
37-
const fromAmountInFullUnits = '100'; // Amount in full units of the stablecoin (3 tbsc:usd1) - Must be an integer
34+
const fromAmountInFullUnits = '100'; // Amount in full units of the stablecoin (100 tbsc:usd1) - Must be an integer
3835
// Note: fromAmount will be calculated dynamically using asset decimals
3936

4037
// Initialize BitGo SDK
@@ -46,24 +43,6 @@ function createStablecoinUrl(path: string): string {
4643
return common.Environments[bitgo.getEnv()].uri + '/api/stablecoin/v1' + path;
4744
}
4845

49-
/**
50-
* Fetch treasury wallet ID from the constants API
51-
* @param token - The stablecoin token to get constants for
52-
* @returns The treasury account wallet ID
53-
*/
54-
async function fetchTreasuryWalletId(token: string): Promise<string> {
55-
console.log(`\n🔍 STEP 2: Fetching treasury wallet ID from constants API for ${token}...`);
56-
const constants = await bitgo.get(createStablecoinUrl(`/${token}/constants`));
57-
const treasuryAccountWalletId = constants.body.trustAccountWalletId;
58-
59-
if (!treasuryAccountWalletId) {
60-
throw new Error(`Treasury account wallet ID not found in constants for ${token}`);
61-
}
62-
63-
console.log(`🏦 Treasury Account Wallet ID (from constants): ${treasuryAccountWalletId}`);
64-
return treasuryAccountWalletId;
65-
}
66-
6746
/**
6847
* Main function to execute the stablecoin burn order process
6948
*/
@@ -77,8 +56,7 @@ async function main() {
7756
console.log('='.repeat(50));
7857

7958
// Execute the burn order process step by step
80-
const { usdAsset, stablecoinAsset, fromAmount } = await fetchAndValidateAssets();
81-
const treasuryAccountWalletId = await fetchTreasuryWalletId(stablecoin);
59+
const { usdAsset, stablecoinAsset, treasuryAccountWalletId, fromAmount } = await fetchAndValidateAssets();
8260
const newOrder = await createBurnOrder(stablecoinAsset, usdAsset, fromAmount);
8361
await sendTokensToTreasury(treasuryAccountWalletId, newOrder.id, fromAmount);
8462
const order = await fetchOrderDetails(newOrder.id);
@@ -119,9 +97,14 @@ async function fetchAndValidateAssets() {
11997
console.log(`📋 USD Asset: ${usdAsset.token} (ID: ${usdAsset.id})`);
12098
console.log(`🪙 Stablecoin Asset: ${stablecoinAsset.token} (ID: ${stablecoinAsset.id})`);
12199

100+
const treasuryAccountWalletId = stablecoinAsset.treasuryAccountWalletId;
122101
// Calculate fromAmount using stablecoin asset decimals
123102
const decimals = stablecoinAsset.decimals;
124103

104+
if (!treasuryAccountWalletId) {
105+
throw new Error(`Treasury account wallet ID not found for ${stablecoin}`);
106+
}
107+
125108
if (decimals === undefined) {
126109
throw new Error(`Decimals not found for ${stablecoin}`);
127110
}
@@ -133,8 +116,9 @@ async function fetchAndValidateAssets() {
133116
console.log(` • Full Units: ${fromAmountInFullUnits} ${stablecoinAsset.token}`);
134117
console.log(` • Decimals: ${decimals}`);
135118
console.log(` • Base Units: ${fromAmount}`);
119+
console.log(`🏦 Treasury Account Wallet ID: ${treasuryAccountWalletId}`);
136120

137-
return { usdAsset, stablecoinAsset, fromAmount };
121+
return { usdAsset, stablecoinAsset, treasuryAccountWalletId, fromAmount };
138122
}
139123

140124
/**
@@ -145,7 +129,7 @@ async function fetchAndValidateAssets() {
145129
* @returns The created order object
146130
*/
147131
async function createBurnOrder(stablecoinAsset: any, usdAsset: any, fromAmount: string) {
148-
console.log('\n🔥 STEP 3: Creating burn order...');
132+
console.log('\n🔥 STEP 2: Creating burn order...');
149133

150134
const orderRequestBody = {
151135
sourceWalletId: walletId,
@@ -181,7 +165,7 @@ async function createBurnOrder(stablecoinAsset: any, usdAsset: any, fromAmount:
181165
* @returns The transaction object
182166
*/
183167
async function sendTokensToTreasury(treasuryAccountWalletId: string, orderId: string, fromAmount: string) {
184-
console.log('\n💸 STEP 4: Sending stablecoin to treasury account...');
168+
console.log('\n💸 STEP 3: Sending stablecoin to treasury account...');
185169

186170
const walletInstance = await basecoin.wallets().get({ id: walletId });
187171

@@ -208,7 +192,7 @@ async function sendTokensToTreasury(treasuryAccountWalletId: string, orderId: st
208192
}
209193

210194
async function fetchOrderDetails(orderId: string) {
211-
console.log('\n🔍 STEP 5: Fetching final order details...');
195+
console.log('\n🔍 STEP 4: Fetching final order details...');
212196
const orderResponse = await bitgo.get(createStablecoinUrl(`/enterprise/${enterpriseId}/orders/${orderId}`)).send();
213197
return orderResponse.body;
214198
}

examples/ts/stablecoin/initiate-mint-order.ts

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ require('dotenv').config({ path: '../../../.env' });
1212
* Step 1. GET /assets API
1313
* Retrieves:
1414
* - The list of assets supported for minting
15+
* - Treasury Account Wallet ID for the specified stablecoin, to which the funds need to be sent
1516
*
16-
* Step 2. GET /{token}/constants API
17-
* Retrieves:
18-
* - Treasury Account Wallet ID for the specified stablecoin
19-
*
20-
* Step 3. POST /order API
17+
* Step 2. POST /order API
2118
* Creates a Mint Order with the specified parameters
2219
*
23-
* Step 4. Transfer the USD to the treasury account
20+
* Step 3. Transfer the USD to the treasury account
2421
* Initiates the send transaction to the Treasury Account using sendMany
2522
*/
2623

@@ -34,7 +31,7 @@ const walletPassphrase = ''; // GoAccount Wallet passphrase
3431
const usdcoin = 'tfiatusd'; // USD asset token
3532
const ofcUsdCoin = 'ofctusd'; // ofc usd (for initiating the send from the specified GoAccount wallet to the Treasury Go Account)
3633
const stablecoin = 'tbsc:usd1'; // Stablecoin to mint
37-
const fromAmountInFullUnits = '100'; // Amount in full units of USD (3 USD) - Must be an integer
34+
const fromAmountInFullUnits = '100'; // Amount in full units of USD (100 USD) - Must be an integer
3835
// Note: fromAmount will be calculated dynamically using asset decimals
3936

4037
// Initialize BitGo SDK
@@ -46,24 +43,6 @@ function createStablecoinUrl(path: string): string {
4643
return common.Environments[bitgo.getEnv()].uri + '/api/stablecoin/v1' + path;
4744
}
4845

49-
/**
50-
* Fetch treasury wallet ID from the constants API
51-
* @param token - The stablecoin token to get constants for
52-
* @returns The treasury account wallet ID
53-
*/
54-
async function fetchTreasuryWalletId(token: string): Promise<string> {
55-
console.log(`\n🔍 STEP 2: Fetching treasury wallet ID from constants API for ${token}...`);
56-
const constants = await bitgo.get(createStablecoinUrl(`/${token}/constants`));
57-
const treasuryAccountWalletId = constants.body.trustAccountWalletId;
58-
59-
if (!treasuryAccountWalletId) {
60-
throw new Error(`Treasury account wallet ID not found in constants for ${token}`);
61-
}
62-
63-
console.log(`🏦 Treasury Account Wallet ID (from constants): ${treasuryAccountWalletId}`);
64-
return treasuryAccountWalletId;
65-
}
66-
6746
/**
6847
* Main function to execute the stablecoin mint order process
6948
*/
@@ -77,8 +56,7 @@ async function main() {
7756
console.log('='.repeat(50));
7857

7958
// Execute the mint order process step by step
80-
const { usdAsset, stablecoinAsset, fromAmount } = await fetchAndValidateAssets();
81-
const treasuryAccountWalletId = await fetchTreasuryWalletId(stablecoin);
59+
const { usdAsset, stablecoinAsset, treasuryAccountWalletId, fromAmount } = await fetchAndValidateAssets();
8260
const newOrder = await createMintOrder(stablecoinAsset, usdAsset, fromAmount);
8361
await sendUsdToTreasury(treasuryAccountWalletId, newOrder.id, fromAmount);
8462
const order = await fetchOrderDetails(newOrder.id);
@@ -121,8 +99,13 @@ async function fetchAndValidateAssets() {
12199
console.log(`📋 USD Asset: ${usdAsset.token} (ID: ${usdAsset.id})`);
122100
console.log(`🪙 Stablecoin Asset: ${stablecoinAsset.token} (ID: ${stablecoinAsset.id})`);
123101

102+
const treasuryAccountWalletId = stablecoinAsset.treasuryAccountWalletId;
124103
const decimals = usdAsset.decimals;
125104

105+
if (!treasuryAccountWalletId) {
106+
throw new Error(`Treasury account wallet ID not found for ${stablecoin}`);
107+
}
108+
126109
if (decimals === undefined) {
127110
throw new Error(`Decimals not found for ${usdcoin}`);
128111
}
@@ -134,8 +117,9 @@ async function fetchAndValidateAssets() {
134117
console.log(` • Full Units: ${fromAmountInFullUnits} ${usdAsset.token}`);
135118
console.log(` • Decimals: ${decimals}`);
136119
console.log(` • Base Units: ${fromAmount}`);
120+
console.log(`🏦 Treasury Account Wallet ID: ${treasuryAccountWalletId}`);
137121

138-
return { usdAsset, stablecoinAsset, fromAmount };
122+
return { usdAsset, stablecoinAsset, treasuryAccountWalletId, fromAmount };
139123
}
140124

141125
/**
@@ -146,7 +130,7 @@ async function fetchAndValidateAssets() {
146130
* @returns The created order object
147131
*/
148132
async function createMintOrder(stablecoinAsset: any, usdAsset: any, fromAmount: string) {
149-
console.log('\n🪙 STEP 3: Creating mint order...');
133+
console.log('\n🪙 STEP 2: Creating mint order...');
150134

151135
const orderRequestBody = {
152136
sourceWalletId: walletId,
@@ -183,7 +167,7 @@ async function createMintOrder(stablecoinAsset: any, usdAsset: any, fromAmount:
183167
* @returns The transaction object
184168
*/
185169
async function sendUsdToTreasury(treasuryAccountWalletId: string, orderId: string, fromAmount: string) {
186-
console.log('\n💸 STEP 4: Sending USD to treasury account...');
170+
console.log('\n💸 STEP 3: Sending USD to treasury account...');
187171

188172
const walletInstance = await basecoin.wallets().get({ id: walletId });
189173

@@ -210,7 +194,7 @@ async function sendUsdToTreasury(treasuryAccountWalletId: string, orderId: strin
210194
}
211195

212196
async function fetchOrderDetails(orderId: string) {
213-
console.log('\n🔍 STEP 5: Fetching final order details...');
197+
console.log('\n🔍 STEP 4: Fetching final order details...');
214198
const orderResponse = await bitgo.get(createStablecoinUrl(`/enterprise/${enterpriseId}/orders/${orderId}`)).send();
215199
return orderResponse.body;
216200
}

0 commit comments

Comments
 (0)