reactjs 使用moralis、ethers和元掩码创建自动签名的事务处理审批

xxhby3vn  于 2023-02-08  发布在  React
关注(0)|答案(1)|浏览(118)

我使用metamask作为web3provider.有这样的代码,它返回一个令牌的abi:

import abiErc20 from './abi-erc20.json'
import { ethers } from "ethers"

const abiObj = {
    erc20abi: (tokenForAbi) => {
        const provider = new ethers.providers.Web3Provider(window.ethereum)
        const signer = provider.getSigner()
        const contract = new ethers.Contract(tokenForAbi, abiErc20, signer)
        return contract
    },
    methods: {
        allowance: async (abi, wallet, router) => {
            const res = await abi.allowance(wallet, router)
            console.log('Allowance : ', res)
            return res
        },
        balance: async (abi, wallet) => {
            const res = await abi.balanceOf(wallet)
            console.log('Balance : ', res)
            return res
        },
        approve: async (abi, router, amount) => {
            const res = await abi.approve(router, amount)
            console.log('Approve : ', res)
            return res
        }
    }
}

export default abiObj

此部分代码用于调用函数:

async function Approve() {
      const abiFrom = abiObj.erc20abi('0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c')
      console.log('AbiFrom: ', abiFrom)
      setAbiFrom(abiFrom)
      const allowanceChecked = await abiObj.methods.allowance(abiFrom, userAddress, exchangeAddresses.pancakeAddress)
      const fromBalance = await abiObj.methods.balance(abiFrom, userAddress)
      const fromApprove = await abiObj.methods.approve(abiFrom, '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c', '100', userAddress)
    }

是否有任何方法可以自动签署交易批准,而不弹出元掩码?
已查找docs,但未找到任何有用信息

ppcbkaq5

ppcbkaq51#

不,你不能。交易应该由用户签署,除非你的智能合约本身可以调用approve。

相关问题