本文介绍了警告:无法解码事件!在固定性和节点JS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份将货币用户发送给用户的智能合同。

在此智能合同中,当用户选择硬币时,我从Kovan Network传递智能合同地址。

在这种情况下,我将链链接约定地址传递给我的约定,以便将链链接令牌发送给用户。

这是Kovan Network中的链式合同地址:0xa36085f69e2889c224210f603d836748e7dc0088

现在我想用以下代码测试约定函数:

 const assert = require("assert");

const Dex = artifacts.require("Dex");

contract("Dex", (accounts) => {

    let dex;
    let contractOwner = null;
    let buyer = null;

    beforeEach(async () => {
        contractOwner = accounts[0];
        buyer = accounts[1];

        dex = await Dex.new();
        contractOwner = accounts[0];
        // sometimes you need to test a user except contract owner
        buyer = accounts[1];
    });

    // it("Add tokens and contract address Success", async () => {

    //     const tokenInfo = await dex.getTokenContractAddress("USDT");

    //     assert(tokenInfo == "0xdac17f958d2ee523a2206206994597c13d831ec7");
    // })

    // it("Add Token", async () => {

    //     await dex.addToken(web3.utils.fromAscii("LINK"), "0xa36085f69e2889c224210f603d836748e7dc0088", "0xa36085f69e2889c224210f603d836748e7dc0088")


    // })
    it("Deposit", async () => {

        await dex.deposit("0xa36085f69e2889c224210f603d836748e7dc0088", "0x5226a51522C23CcBEFd04a2d4C6c8e281eD1d680", "0xB643992c9fBcb1Cb06b6C9eb278b2ac35e6a2711", 1,
            // you were missing this
            { from: accounts[0] });

    })



})

我在项目中使用松露:

Truffle Config:

const HDWalletProvider = require('@truffle/hdwallet-provider');

const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();
const secrets = JSON.parse(
  fs.readFileSync('.secrets.json').toString().trim()
);

module.exports = {

  networks: {
    kovan: {
      networkCheckTimeout: 10000,
      provider: () => {
         return new HDWalletProvider(
          mnemonic,
           `https://kovan.infura.io/v3/1461728202954c07bd5ed5308641a054`,
           0,
           20
         );
      },
      network_id: "42",
   },
  },

  // Set default mocha options here, use special reporters, etc.
  mocha: {

  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "0.8.10",
      docker: false,
      settings: {
        optimizer: {
          enabled: false,
          runs: 200
        },
        evmVersion: "byzantium"
      }
    }
  },
};

My Contract:

  // SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

// import "./UserCoins.sol";

contract Dex {
    event Approval(
        address indexed tokenOwner,
        address indexed spender,
        uint256 tokens
    );
    event Transfer(address indexed from, address indexed to, uint256 tokens);

    constructor() {}

    function deposit(
        address ticker,


   address sender,
        address recipient,
        uint256 amount
    ) external payable {
        IERC20 token = IERC20(ticker);
        IERC20(ticker).approve(sender, amount);
        // Transfer Token To User
        token.transferFrom(sender, recipient, amount);
        emit Transfer(sender, recipient, amount);
    }

}

它显示以下错误:

现在问题出在哪里?我如何解决此问题?

推荐答案

function deposit(address ticker,address sender,address recipient,uint256 amount
                )
                external payable

此函数采用4个参数,您传递了所有参数,但由于它是应支付的,您还必须确保从哪个帐户调用,因此您需要向该函数添加第5个参数:

await dex.deposit("0xa36085f69e2889c224210f603d836748e7dc0088", "0x5226a51522C23CcBEFd04a2d4C6c8e281eD1d680", "0xB643992c9fBcb1Cb06b6C9eb278b2ac35e6a2711", "1",
// you were missing this
{from:accounts[0])


在松露测试套件中,当您创建约定时,它将accounts作为第一个参数传递给回调。您必须在before语句中定义帐户,以便当您运行测试时,这些帐户将在顶级可用。

contract("Dex", (accounts) => {
  let contractOwner = null;
  let buyer = null;
  let _contract = null;


  before(async () => {
      // Instead of Dex.new() try Dex.deployed()
      // I am not sure if "new()" is still supported
      _contract = await Dex.deployed();
      contractOwner = accounts[0];
      // sometimes you need to test a user except contract owner
      buyer = accounts[1];
  });

}

您需要审批

内部ERC20.sol您正在调用:

function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public virtual override returns (bool) {
        uint256 currentAllowance = _allowances[sender][_msgSender()];
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
            unchecked {
                _approve(sender, _msgSender(), currentAllowance - amount);
            }
        }
        _transfer(sender, recipient, amount);

        return true;
    }

所以你希望DEX从另一份合同中转移硬币。所以这另一份合同必须先批准这笔交易。因此,我需要有另一个合同的地址才能呼叫approve

这篇关于警告:无法解码事件!在固定性和节点JS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:56