如何解决"Warning: Could not decode event!"



我有一个智能合约,用于向用户发送货币。

在这个智能合约中,当用户选择硬币时,我会从Kovan网络传递智能合约地址。

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

这是Kovan网络中的ChainLink合同地址: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] });
})

})

I在项目中使用块菌:

松露配置:

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"
}
}
},
};

我的合同:

// 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个参数,你传递了所有参数,但由于它是应付的,你还必须确保你从哪个账户调用,所以你需要在函数中添加第五个参数:

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

在truffle测试套件中,当您创建契约时,它会将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

相关内容

最新更新