DEX API
获取授权额度

获取授权额度#

Note
当前欧易 DEX 已停止提供查询交易授权额度的接口,需要用户自己采用第三方方法查询,建议方法如下。

第三方查询方法#

以 ETH chain 举例#

  • Demo 为 JavaScript 语言
  1. 连接到以太坊节点:您需要确保您已经连接到一个可用的以太坊节点。您可以使用 web3.js 或其他以太坊开发库来连接到节点。在代码中,您需要指定节点的 HTTP 或 WebSocket 端点。
  2. 获取代币合约实例:使用代币的合约地址和 ABI,您需要创建一个代币合约的实例。您可以使用 web3.js 的 web3.eth.Contract 方法来实现这一点,将合约地址和 ABI 作为参数传递给合约实例。
  3. 查询授权额度:通过调用合约实例的 allowance 函数来查询授权额度。该函数需要两个参数:拥有者的地址和被授权者的地址。您可以通过在调用时提供这两个地址来查询授权额度。
  4. spenderAddress 地址可以参考 此处 接口 Response 中的 dexTokenApproveAddress。
const { Web3 } = require('web3');
// Connect to an Ethereum node
const web3 = new Web3('https://xxxxx');
// token address and  ABI
const tokenAddress = '0xxxxxxxxx';
// user address
const ownerAddress = '0xxxxxxxx';
// ETH dex token approval address
const spenderAddress = '0x40aa958dd87fc8305b97f2ba922cddca374bcd7f';


const tokenABI = [
    {
        "constant": true,
        "inputs": [
            {
                "name": "_owner",
                "type": "address"
            },
            {
                "name": "_spender",
                "type": "address"
            }
        ],
        "name": "allowance",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
];


// Create token contract instance
const tokenContract = new web3.eth.Contract(tokenABI, tokenAddress);
// Query token approve allowance function
async function getAllowance(ownerAddress, spenderAddress) {
    try {
        const allowance = await tokenContract.methods.allowance(ownerAddress, spenderAddress).call();
        console.log(`Allowance for ${ownerAddress} to ${spenderAddress}: ${allowance}`);
    } catch (error) {
        console.error('Failed to query allowance:', error);
    }
}

getAllowance(ownerAddress, spenderAddress).then(r => console.log(r));