NodeJS get privacy token balance

Hello guys. I want to generate an Incognito wallet using NodeJS SDK, then generate a deposit BTC address, send money to it and ensure that balance of BTC token in Incognito wallet has increased. So, my main objective is to generate a deposit address, send BTC to it and let Incognito do the magic.

Important - I am using testnet.

I am creating a wallet with the following code:

const wallet = new WalletInstance()
await wallet.init(walletName)
const masterAccountPrivateKey = wallet.masterAccount.key.keySet.privateKeySerialized
await wallet.masterAccount.importAccount('MASTER', masterAccountPrivateKey)
const masterAccount = await wallet.masterAccount.getAccountByName('MASTER')
const BTCTokenId = '4584d5e9b2fc0337dfb17f4b5bb025e5b82c38cfa4f54e8a3d4fcdd03954ff82'

Then, I am doing a wallet backup like that:

const backupPassword = Math.random().toString(36)
const backupWalletString = wallet.backup(backupPassword) 

In order to generate a deposit BTC address I am doing:

const masterAccount = await wallet.masterAccount.getAccountByName(masterAccountName)
const privacyToken = await masterAccount.getPrivacyTokenById(BTCTokenId)
const signPublicKey = await masterAccount.getSignPublicKey()
const depositAddress = await privacyToken.bridgeGenerateDepositAddress({signPublicKey})
return depositAddress.address

Then, I am sending BTC to the deposit address and I can see that the balance of deposit BTC address that I have mentioned before increases.

Finally, I want to ensure that the Incognito wallet has the BTC that I send. In order to get balance of BTC token of the wallet I am doing the following:

const walletObject = await WalletInstance.restore(backupWalletString, backupPassword)
const masterAccount = await walletObject.masterAccount.getAccountByName(masterAccountName)
const followingTokens = await masterAccount.getFollowingPrivacyToken()
const privacyToken = followingTokens[0] // I have only one token, so for now I can use 0 index
const tokenBalance = await privacyToken.getTotalBalance()

The problem is that balance is always zero.

Maybe I am doing something wrong?

UPDATE If I am calling bridgeGetHistory method, I can see that there is a transaction in pending status. How long can it be pending in testnet?
Also, this transaction is quite strange. It has a correct “CreatedAt” field and BTC deposit address is also correct but “RequestedAmount”, “ReceivedAmount” and “IncognitoAmount” are empty.
May testnet affect this zero balance?
As I understand, the fact that I haven’t implemented the storage should not affect because, according to the code of the SDK, it performs RPC calls in order to get balance.

UPDATE #1 I have found the problem. Actually, my code is fine. The problem was that I was sending transactions with too small amount. Minimum amount is 0.001 BTC. When I sent transaction with the necessary amount, everything worked.

I am not familiar with the code but would assume index 0 is for PRV as it is available by default. Did you try index 1?
Just a wild guess.

Thanks for the reply but it will not help. When you do

await masterAccount.getFollowingPrivacyToken()

It returns only those token IDs that you follow explicitly. So, it will return only those tokens which you have followed via the call:


It returns the right token ID.

Too bad. I hope someone who has been working on/with the code will jump in soon.

1 Like