# 合约授权

## 一、EVM链相关

{% hint style="info" %}
本示例使用 BSC链USDT 对合约0xb685760ebd368a891f27ae547391f4e2a289895b进行授权
{% endhint %}

#### abi文件

{% file src="/files/svCCKA6KUoYNLog5PP4V" %}

### 判断是否授权及授权额度是否充足

```
// isApproved example
// USDT(BSC)  0x55d398326f99059ff775485246999027b3197955
 import { ethers } from "ethers";
 import abi from './abi'  // abi文件详见上方BridgersAbi.json文件
 import BigNumber from 'bignumber.js'

 const walletAddress = '0x6F14653a91AC36935bdB15db6ccC66dC57593653' // wallet address
 const contractAddress = '0x1ed5685f345b2fa564ea4a670de1fde39e484751' //contract address
 const tokenContract = '0x55d398326f99059ff775485246999027b3197955' //token contract
 const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
 const contract = new ethers.Contract(tokenContract, abi, provider)
 const fromNumber = 100 // 100 as a comparison quantity
 contract
  .allowance(walletAddress, contractAddress)
  .then((allowAmt) => {
    const num = new BigNumber(
     ethers.utils.formatUnits(allowAmt, 18),
    )
    const fromTokenNum = new BigNumber(fromNumber)
    console.log(num.toString())
    console.log(fromTokenNum.toString())
    if (num.gt(fromTokenNum)) {
        console.log('don`t need Approved')
    } else {
        console.log('need Approved')
    }
  })
  .catch( err => {
    //console.log(err)
  })
```

### 授权操作

```
// Approved example
// USDT(BSC)  0x55d398326f99059ff775485246999027b3197955
 import { ethers } from "ethers";
 import abi from './abi'
 import BigNumber from 'bignumber.js'
 
 const walletAddress = '0x6F14653a91AC36935bdB15db6ccC66dC57593653' // wallet address
 const contractAddress = '0x1ed5685f345b2fa564ea4a670de1fde39e484751' //contract address
 const tokenContract = '0x55d398326f99059ff775485246999027b3197955' //token contract
 const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
 const signer = provider.getSigner()
 const contract = new ethers.Contract(tokenContract, abi, provider)
 contract.estimateGas
   .approve(contractAddress, ethers.constants.MaxUint256, {
     from: walletAddress,
   }).then(res => {
     contract.connect(signer)
       .approve(contractAddress, ethers.constants.MaxUint256, {
         from: walletAddress,
       })
       .then((res) => {
          console.log('user Approved ')
        })
       .catch((error) => {
          console.log('user cancle Approved ')
       })
  })
 let filter = contract.filters.Approval(
   walletAddress,
   contractAddress,
   null,
 )
 contract.on(filter, (from, to, amount, event) => {
   console.log('Approve success')
 })
```

## 二、TRON链相关

{% hint style="info" %}
本示例使用 TRON链USDT 对合约TEorZTZ5MHx8SrvsYs1R3Ds5WvY1pVoMSA进行授权
{% endhint %}

### 判断是否授权及授权额度是否充足

```
// isApproved example
// USDT(TRON)  TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
 import { ethers } from "ethers";
 import BigNumber from 'bignumber.js'
 
const walletAddress = 'TV6MuMXfmLbBqPZvBHdwFsDnQeVfnmiuSi' // wallet address
const contractAddress = 'TEorZTZ5MHx8SrvsYs1R3Ds5WvY1pVoMSA' //contract address
const tokenContract = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' //token contract
const fromNumber = 100 // 100 as a comparison quantity

let tronWeb = window.tronWeb
const contract = await tronWeb.contract().at(tokenContract );
const allowance = contract.allowance(walletAddress , contractAddress)
const allowAmt = await allowance.call()
const num = new BigNumber(
  ethers.utils.formatUnits(allowAmt, 6 ),
)
const fromTokenNum = new BigNumber(fromNumber)
console.log(num.toString())
console.log(fromTokenNum.toString())
if (num.gt(fromTokenNum)) {
   console.log('don`t need Approved')
} else {
  console.log('need Approved')
}
```

### 授权操作

```
// Approved example
// USDT(TRON)  TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
 import { ethers } from "ethers";
 import BigNumber from 'bignumber.js'
 
 const walletAddress = 'TV6MuMXfmLbBqPZvBHdwFsDnQeVfnmiuSi' // wallet address
 const contractAddress = 'TEorZTZ5MHx8SrvsYs1R3Ds5WvY1pVoMSA' //contract address
 const tokenContract = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' //token contract
const contract = await tronWeb.contract().at(tokenContract);
contract.approve(contractAddress , ethers.constants.MaxUint256).send().then( (hash)=> {
    console.log('用户授权',hash)
    tronWeb.trx.getTransaction(hash).then((result) => {
      console.log('监听授权成功',result)
    })
}).catch(err => {
  console.log('用户取消授权')
 })
```

### 发币操作

```
    const tronWeb = window.tronWeb
    const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
      response.data.txData.tronRouterAddrees, // 从【获取callData数据】模块中的swap接口获取tronRouterAddrees字段
      response.data.txData.functionName,      // 从【获取callData数据】模块中的swap接口获取functionName字段
      response.data.txData.options,     // 从【获取callData数据】模块中的swap接口获取options字段
      response.data.txData.parameter,   //从【获取callData数据】模块中的swap接口获取parameter字段
      response.data.txData.fromAddress  //从【获取callData数据】模块中的swap接口获取fromAddress字段
    )
    let signedTx
    try{
      signedTx = await tronWeb.trx.sign(transaction.transaction)
    }catch{
      console.log("签名失败")
    }
    tronWeb.trx.sendRawTransaction(signedTx)
    .then((broastTx) => {
      console.log(broastTx)
    }).catch( error => {
       console.log("发送失败")
    })
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-bridgers.bridgers.xyz/bridgers-api-jie-kou/contract-approval.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
