From 807d1c5eb60cbf74ec53d62a02c2356d6869610f Mon Sep 17 00:00:00 2001 From: Evan Zheng Date: Tue, 22 Aug 2023 15:26:37 -0700 Subject: [PATCH] chore(bitgo): add trx smc consolidation example script EA-1323 TICKET: EA-1323 --- ...ts => consolidate-hot-account-balances.ts} | 0 .../ts/trx/consolidate-smc-account-balance.ts | 59 +++++++++++++++++++ 2 files changed, 59 insertions(+) rename examples/ts/trx/{consolidate-account-balances.ts => consolidate-hot-account-balances.ts} (100%) create mode 100644 examples/ts/trx/consolidate-smc-account-balance.ts diff --git a/examples/ts/trx/consolidate-account-balances.ts b/examples/ts/trx/consolidate-hot-account-balances.ts similarity index 100% rename from examples/ts/trx/consolidate-account-balances.ts rename to examples/ts/trx/consolidate-hot-account-balances.ts diff --git a/examples/ts/trx/consolidate-smc-account-balance.ts b/examples/ts/trx/consolidate-smc-account-balance.ts new file mode 100644 index 0000000000..88797d0765 --- /dev/null +++ b/examples/ts/trx/consolidate-smc-account-balance.ts @@ -0,0 +1,59 @@ +import axios from 'axios'; + +// TODO: set your local external signer url +const LOCAL_EXTERNAL_SIGNER_URL = 'http://localhost:3080'; +// TODO: set your access token here +// You can get this from User Settings > Developer Options > Add Access Token +const accessToken = ''; +// TODO: set your wallet id +const walletId = ''; +const options = { + headers: { + Authorization: `Bearer ${accessToken}`, + 'Content-Type': 'application/json', + }, +}; + +async function consolidateReceiveAddresses(coin: string, receiveAddresses?: string[]) { + const consolidateUrl = `${LOCAL_EXTERNAL_SIGNER_URL}/api/v2/${coin}/wallet/${walletId}/consolidateAccount`; + const response = receiveAddresses + ? await axios.post( + consolidateUrl, + { + consolidateAddresses: receiveAddresses, + }, + options + ) + : await axios.post(consolidateUrl, {}, options); + + const txids = response.data.success.map((item) => item.txid); + console.info(`Succeeded to consolidate receive addresses. Txids: ${txids}`); +} + +async function main() { + try { + const unlockUrl = `${LOCAL_EXTERNAL_SIGNER_URL}/api/v2/user/unlock`; + const unlockData = { + duration: 3600, + otp: '000000', + }; + await axios.post(unlockUrl, unlockData, options); + console.info(`Succeeded to unlock with OTP`); + + // Consolidate native token at specific receive addresses + await consolidateReceiveAddresses('ttrx', ['receiveAddress1']); + + // Consolidate TRC20 token at specific receive addresses + await consolidateReceiveAddresses('ttrx:usdt', ['receiveAddress1', 'receiveAddress2']); + + // Consolidate native token at all receive addresses + await consolidateReceiveAddresses('ttrx'); + + // Consolidate TRC20 token at all receive addresses + await consolidateReceiveAddresses('ttrx:usdt'); + } catch (e) { + console.error(`Failed to consolidate receive addresses error: ${e.message}`); + } +} + +main().catch((e) => console.error(e));