#How get balance of current acount in th smartcontract

1 messages · Page 1 of 1 (latest)

fallow cove
#

I want to check contract balance, when I tried locally it works fine but not on testnet, it always return 0

Code :

export class TokenHolder extends SmartContract {
    init() {
        super.init();
    }

    @method
    async withdraw(
        amount: UInt64
    ) {
        const balance = this.account.balance.getAndRequireEquals();
        Provable.log("balance", balance);
        balance.assertGreaterThanOrEqual(amount, "Balance less than withdrawal amount");

        this.balance.subInPlace(amount);
    }
}

My vault :

export class Vault extends SmartContract {
    // we need the token address to instantiate it
    @state(PublicKey) tokenA = State<PublicKey>();
    @state(UInt64) liquiditySupply = State<UInt64>();

    async deploy(args: VaultDeployProps) {
        await super.deploy(args);
        args.tokenA.isEmpty().assertFalse("token empty");

        this.account.permissions.set({
            ...Permissions.default(),
            access: Permissions.none(),
            setVerificationKey: Permissions.VerificationKey.proofOrSignature()
        });

        this.tokenA.set(args.tokenA);
    }


    @method async deposit(amount: UInt64) {
        const addressA = this.tokenA.getAndRequireEquals();

        amount.assertGreaterThan(UInt64.zero, "No amount A supplied");

        let tokenContractA = new TokenA(addressA);
        let dexX = AccountUpdate.create(this.address, tokenContractA.deriveTokenId());

        let sender = this.sender.getUnconstrained();
        await tokenContractA.transfer(sender, dexX, amount);
    }

    @method async withdraw(amount: UInt64) {
        const addressA = this.tokenA.getAndRequireEquals();
        const tokenContractA = new TokenA(addressA);

        const holder = new TokenHolder(this.address, tokenContractA.deriveTokenId());
        await holder.withdraw(amount);

        const sender = this.sender.getUnconstrained();
        await tokenContractA.transfer(holder.self, sender, amount);
    }
}
crude crescent
#

Do you call fetchAccount() before checking the balance?

fallow cove
# crude crescent Do you call `fetchAccount()` before checking the balance?

yes

let amtWithdraw = UInt64.from(555);
        const fetch = await fetchAccount({ publicKey: zkAppAddress, tokenId: zkToken0.deriveTokenId() });
        const bal = Mina.getBalance(zkAppAddress, zkToken0.deriveTokenId());
        console.log("bal", bal.toBigInt());
        let tx = await Mina.transaction({ sender: feepayerAddress, fee }, async () => {
            await zkApp.withdraw(amtWithdraw);
            zkToken0.approveAccountUpdate(zkApp.self);
        });
        await tx.prove();
        let sentTx = await tx.sign([feepayerKey]).send();```

the logs :
withdraw
bal 5000000000000n
balance 0
Error: Balance less than withdrawal amount
    at assert (o1js/dist/node/lib/util/assert.js:4:15)
crude crescent
#

It seems like in the first case you check the balance for {publicKey, tokenId} and in the second case for {publicKey}

fallow cove
crude crescent
#

You can explicitly create AccountUpdate for {publicKey, tokenId} to make it work

fallow cove
crude crescent
#

Can you try just pass tokenId to withdraw() of TokenHolder?

fallow cove
#

the problem is not token id is when I call AccountUpdate.create with a tokenId, no problem if I don't put a tokenId

crude crescent
fallow cove
#

Works when I use this example, but in my case I always got the same error Token_owner_not_caller

fallow cove
#

For the moment I just store the balance in the contract state, I spent too much time on it

it seems accountupdate.create works when we are in the contract deployed on this.address, but for sub account (contract deployed at address and token id), I got this error

crude crescent
fallow cove