How to get the spender allowance of an ERC20 token
Step 1: Setup Moralis
Read the article Setting Up Moralis: Getting Started and make sure to finish all the steps. Only after that you can go ahead to complete this guide.
Step 2: Get the spender allowance of an ERC20 token
In order to get the spender allowance, Moralis provides you with an getTokenAllowance endpoint.
Here you'll need four parameters: address
, chain
, ownerAddress
, spenderAddress
.
address
: The address of the token contract.chain
: The chain to query.ownerAddress
: The address of the token owner.spenderAddress
: The address of the token spender.
Once you have obtained the parameters, you can copy the following code:
- index.js (JavaScript)
- index.ts (TypeScript)
- index.py (Python)
const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");
const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});
const chain = EvmChain.ETHEREUM;
const address = "0x514910771AF9Ca656af840dff83E8264EcF986CA";
const ownerAddress = "0x7c470D1633711E4b77c8397EBd1dF4095A9e9E02";
const spenderAddress = "0xed33259a056f4fb449ffb7b7e2ecb43a9b5685bf";
const response = await Moralis.EvmApi.token.getTokenAllowance({
address,
chain,
ownerAddress,
spenderAddress,
});
console.log(response.toJSON());
};
runApp();
import Moralis from "moralis";
import { EvmChain } from "@moralisweb3/common-evm-utils";
const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});
const chain = EvmChain.ETHEREUM;
const address = "0x514910771AF9Ca656af840dff83E8264EcF986CA";
const ownerAddress = "0x7c470D1633711E4b77c8397EBd1dF4095A9e9E02";
const spenderAddress = "0xed33259a056f4fb449ffb7b7e2ecb43a9b5685bf";
const response = await Moralis.EvmApi.token.getTokenAllowance({
address,
chain,
ownerAddress,
spenderAddress,
});
console.log(response.toJSON());
};
runApp();
from moralis import evm_api
api_key = "YOUR_API_KEY"
params = {
"address": "0x514910771AF9Ca656af840dff83E8264EcF986CA",
"owner_address": "0x7c470D1633711E4b77c8397EBd1dF4095A9e9E02",
"spender_address": "0xed33259a056f4fb449ffb7b7e2ecb43a9b5685bf",
"chain": "eth"
}
result = evm_api.token.get_token_allowance(
api_key=api_key,
params=params,
)
print(result)
Step 3: Run the script
To run the script, enter the following command:
- Shell (JavaScript)
- Shell (TypeScript)
- Shell (Python)
node index.js
ts-node index.ts
python index.py
In your terminal, you should see the following JSON response:
{
"allowance": "0"
}
Congratulations 🥳 You just got the spender allowance with just a few lines of code using the Moralis Token API!
API Reference
If you want to know more details on the endpoint and optional parameters, check out:
Support
If you face any trouble following the tutorial, feel free to reach out to our community engineers in our Discord or Forum to get 24/7 developer support.