getAllowance

getAllowance

The getAllowance function determines the number of tokens an owner has authorized another Ethereum address to expend on their behalf. It is commonly used in conjunction with token approvals, particularly in scenarios where a smart contract needs permission to move tokens on behalf of an owner. This function is a property of the ERC20Instance object, which is obtained through the getERC20Instance method of the ZkyugaClient.

Method Signature

getAllowance(owner: string, spender: string): Promise<BigNumber>

Parameters

Parameter
Type
Description

owner

string

The Ethereum address of the token owner.

spender

string

The Ethereum address authorized to utilize tokens on behalf of the owner.

Returns

  • A Promise that resolves to a BiBigNumbergNumber representing the quantity of tokens that the spender can withdraw from the owner.

Usage Example

To begin, initialize the desired ERC20 token contract using its designated address:

const erc20 = zkyugaClient.getERC20Instance("0xaC405eCD43eD9cEbd8621E8dA0D02196a55425B6");

Next, call the getAllowance function, providing the Ethereum addresses of the token owner and the spender:

const ownerAddress = "0xaC405eCD43eD9cEbd8621E8dA0D02196a55425B6";
const spenderAddress = "0xaC405eCD43eK7cEbd8621R2dA0D02894a83794K9";
const allowance = await erc20.getAllowance(ownerAddress, spenderAddress);

Considerations

  • The disclosed allowance will be in the smallest unit of the ERC20 token (such as Wei for Ether). Depending on the token's decimals, you may need to convert this value into a format more easily understood by humans.

  • An allowance value of 0 indicates that the spender currently lacks permission to transfer any tokens on behalf of the owner.


Last updated