|
| 1 | +/** |
| 2 | + * Approve an ERC20 token for use with a batcher contract at BitGo. |
| 3 | + * |
| 4 | + * Copyright 2023, BitGo, Inc. All Rights Reserved. |
| 5 | + */ |
| 6 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 7 | +import { Tbsc } from '@bitgo/sdk-coin-bsc'; |
| 8 | +require('dotenv').config({ path: '../../.env' }); |
| 9 | + |
| 10 | +const bitgo = new BitGoAPI({ |
| 11 | + accessToken: process.env.TESTNET_ACCESS_TOKEN, |
| 12 | + env: 'staging', |
| 13 | +}); |
| 14 | + |
| 15 | +const coin = 'tbsc'; |
| 16 | +bitgo.register(coin, Tbsc.createInstance); |
| 17 | + |
| 18 | +const walletId = process.env.TESTNET_BSC_WALLET_ID; |
| 19 | +const walletPassphrase = process.env.TESTNET_BSC_WALLET_PASSPHRASE; |
| 20 | +const tokenName = 'tbsc:busd'; // Replace with the token you want to approve |
| 21 | + |
| 22 | +async function main() { |
| 23 | + if (!walletId) { |
| 24 | + throw new Error('Please set TESTNET_BSC_WALLET_ID environment variable'); |
| 25 | + } |
| 26 | + if (!walletPassphrase) { |
| 27 | + throw new Error('Please set TESTNET_BSC_WALLET_PASSPHRASE environment variable'); |
| 28 | + } |
| 29 | + |
| 30 | + const walletInstance = await bitgo.coin(coin).wallets().get({ id: walletId }); |
| 31 | + |
| 32 | + console.log('Wallet ID:', walletInstance.id()); |
| 33 | + console.log('Current Receive Address:', walletInstance.receiveAddress()); |
| 34 | + |
| 35 | + console.log(`Approving token ${tokenName} for use with batcher contract...`); |
| 36 | + |
| 37 | + try { |
| 38 | + const approvalTransaction = await walletInstance.sendMany({ type: 'tokenApproval', walletPassphrase, tokenName }); |
| 39 | + |
| 40 | + console.log('Token Approval Transaction:', JSON.stringify(approvalTransaction, null, 4)); |
| 41 | + console.log('Transaction ID:', approvalTransaction.txid); |
| 42 | + console.log('Status:', approvalTransaction.status); |
| 43 | + } catch (e) { |
| 44 | + console.error('Error approving token:', e.message); |
| 45 | + if (e.stack) { |
| 46 | + console.error(e.stack); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +main().catch((e) => console.error(e)); |
0 commit comments