0

Does anyone have template code calling balance_of entrypoint using JSLigo? I want sth similar to Tezos.get_balance() but getting FA2 token balance instead of tez.

1 Answer 1

0

This should be it. Please note that this is contract we work on right now so it is not tested.

type BalanceOfRequestType = // @layout comb 
{
    owner: address,
    token_id: nat
};

type BalanceOfResponseType = // @layout comb 
{
    request: BalanceOfRequestType,
    balance: nat
};

type BalanceOfType = // @layout comb 
{
    requests: list<BalanceOfRequestType>,
    callback: contract<list<BalanceOfResponseType>>
};

let get_balance_of = (balanceRequest: BalanceOfRequestType, storage: StorageType): BalanceOfResponseType => {
    let balance = 0n;
    let { owner, token_id } = balanceRequest;
    let balance_ = match(Big_map.find_opt(token_id, storage.ledger), {
        Some: (v) => v == owner ? 1 as nat: 0 as nat,
        None: () => 0 as nat
    };
    return { request: balanceRequest, balance: balance_ };
}

let balance_of  = (balanceOfParam: BalanceOfType, storage: StorageType): [list<operation>, StorageType<T>] => {
    let callbackParam = [] as list<BalanceOfResponseType>;
    let { requests, callback } = balanceOfParam;
    for (const request of requests) {
        callbackParam = [...callbackParam, get_balance_of(request, storage)];
    }
    return [
        [Tezos.transaction(callbackParam, 0 as tez, callback)] as list<operation>, 
        storage
    ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.