Skip to content

Commit 4095f31

Browse files
authored
Merge pull request #6662 from BitGo/TMS-1303
feat(examples): adds example for initiating mint order
2 parents 874eda6 + 707ea24 commit 4095f31

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Initiate a Mint Order for a Stablecoin
3+
*
4+
* Copyright 2023, 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 ofcUsdCoin = 'ofctusd';
26+
const stablecoin = 'tbsc:usd1';
27+
const fromAmount = '3000'; // in base units, e.g., 3000 for 30 USD
28+
29+
const bitgo = new BitGoJS.BitGo({ env: environment });
30+
bitgo.authenticateWithAccessToken({ accessToken: accessToken });
31+
32+
const basecoin = bitgo.coin(ofcUsdCoin);
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 mint 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 mint order
62+
const orderRequestBody = {
63+
sourceWalletId: walletId,
64+
destinationWalletId: trustAccountWalletId,
65+
destinationType: "go_account",
66+
toAssetId: stablecoinAsset.id,
67+
fromAssetId: usdAsset.id,
68+
fromAmount,
69+
type: "mint",
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('Mint 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 mint order process:', error);
95+
throw error;
96+
}
97+
}
98+
99+
main().catch((e) => console.error(e));

0 commit comments

Comments
 (0)