-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalances.ts
More file actions
50 lines (42 loc) · 1.73 KB
/
balances.ts
File metadata and controls
50 lines (42 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {bitgo, COIN, walletId} from './bitgo';
// Show wallet balances. Copied from https://developers.bitgo-dev.com/guides/wallets/view/balances
async function getWalletInstance(id: string) {
if (!id.length) {
throw new Error('AFTER YOU GENERATE A WALLET, YOU NEED TO SET VARIABLE `walletId` IN bitgo.ts');
}
return await bitgo.coin(COIN).wallets().get({ id, allTokens: true });
}
async function getWalletNFTBalances(id: string) {
const walletInstance = await getWalletInstance(id);
const walletJSON = walletInstance.toJSON();
// check ERC20 balances with walletJSON.tokens
return { ...walletJSON.nfts, ...walletJSON.unsupportedNfts };
}
getWalletInstance(walletId)
.then(walletInstance => {
console.log('Wallet ID:', walletInstance.id());
console.log('Current Receive Address:', walletInstance.receiveAddress()); // equal to base address if no new addresses created
console.log('Balance:', walletInstance.balanceString()); // base asset balance
console.log('Confirmed Balance:', walletInstance.confirmedBalanceString()); // cumulative balance on the wallet (incl. receive addresses)
console.log('Spendable Balance:', walletInstance.spendableBalanceString()); // balance on the base address that can be sent (not locked)
})
.catch(e => console.log(e.message));
getWalletNFTBalances(walletId)
.then(balances => {
console.log('NFT balances: ', balances);
})
.catch(e => console.log(e.message));
/*
Example NFT balance object
{
'0xba4bfed386dac111866aa2369319f2c2daf454af': {
type: 'ERC721',
collections: { '2089': '1' },
metadata: {
name: 'tpolygon:name',
tokenContractAddress: '0xba4bfed386dac111866aa2369319f2c2daf454af'
},
transferCount: 0
}
}
*/