当前位置:   article > 正文

跟我一起阅读并修复源码(3:编译工程,源码分析)之六_transferhelper: transfer from failed

transferhelper: transfer from failed

在上一章跟我一起阅读并修复某知名DEX交易所源码(3:编译工程,源码分析)之五_lixiaodog的博客-CSDN博客

中我们详细分析了 contract MdexPair的源码,本章将继续编译的进程,来吧,少年,出发吧!!

输入编译命令truffle compile

  1. project:/contracts/mainnet/CoinChef.sol:48:43: TypeError: Operator ** not compatible with types int_const 100 and int_const 1000000000000000000
  2. uint256 public constant mdxPerBlock = 100 ** 1e18;

**运算符无法用于这两个常数,我认为应该是写错了,修改为:

uint256 public constant mdxPerBlock = 100*10**18;

每区块产量100个。如果是100**1E18 ,是个多大的数大家可以想像。

再次编译,如下图:

  1. > Duplicate contract names found for TransferHelper.
  2. > This can cause errors and unknown behavior. Please rename one of your contracts.
  3. > Duplicate contract names found for IWHT.
  4. > This can cause errors and unknown behavior. Please rename one of your contracts.
  5. > Duplicate contract names found for TransferHelper.
  6. > This can cause errors and unknown behavior. Please rename one of your contracts.
  7. > Duplicate contract names found for MdxToken.
  8. > This can cause errors and unknown behavior. Please rename one of your contracts.
  9. > Duplicate contract names found for SafeMath.
  10. > This can cause errors and unknown behavior. Please rename one of your contracts.

 合约名TransferHelper冲突了。在全局范围内搜索,如下图:

在 C:\work\blockchain\Mdex\contracts\assets\Airdrop.sol
  9,9: library TransferHelper {
  13,77:         require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
  19,77:         require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
  25,77:         require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
  87,9:         TransferHelper.safeTransferFrom(address(wht), msg.sender, address(this), _whtAmount);

C:\work\blockchain\Mdex\contracts\assets\AirdropMDX.sol
  10,9: library TransferHelper {
  14,77:         require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
  20,77:         require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
  26,77:         require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
  83,9:         TransferHelper.safeTransferFrom(mdx, msg.sender, address(this), _mdxAmount);

发现了同名定义,此时我们需要决定修改哪个定义,以解决冲突,通过对比我们发现这三处定义是一模一样的,所以我直接把这个库合并为一个库,在library新建文件TransferHelper.sol,如下图

同时删除其它两处的相关定义,并添加import "library/TransferHelper.sol";

  1. pragma solidity ^0.6.0;
  2. library TransferHelper {
  3. function safeApprove(address token, address to, uint value) internal {
  4. // bytes4(keccak256(bytes('approve(address,uint256)')));
  5. (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
  6. require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
  7. }
  8. function safeTransfer(address token, address to, uint value) internal {
  9. // bytes4(keccak256(bytes('transfer(address,uint256)')));
  10. (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
  11. require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
  12. }
  13. function safeTransferFrom(address token, address from, address to, uint value) internal {
  14. // bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
  15. (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
  16. require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
  17. }
  18. function safeTransferETH(address to, uint value) internal {
  19. (bool success,) = to.call{value : value}(new bytes(0));
  20. require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
  21. }
  22. }

搜索library SafeMath

 在oracle.sol中我们发的safemath只有定义,但实际上在这个文件中并未有实际使用,所以注释掉safeMath的定义和  //using SafeMathUint for uint;

搜索MdxToken,结果如下图:

我们发现在MdxToken.sol中的定义并未被引用过,全部注释掉 

搜索IWHT,如下图

 注释Airdrop.sol的定义,并在interface下新建文件IWHT.SOL,代码:

  1. interface IWHT {
  2. function balanceOf(address owner) external view returns (uint);
  3. function deposit() external payable;
  4. function transfer(address to, uint value) external returns (bool);
  5. function withdraw(uint) external;
  6. }

在上面两个文件中加入:import "../interface/IWHT.sol"; 

再次编译,结果如下图:

编译通过,至此,所有的编译问题已经全部解决,在下一章跟我一起阅读并修复某知名DEX交易所源码(4:部署工程,源码分析)_lixiaodog的博客-CSDN博客中,我们将会尝试布署MDEX至本地测试环境。敬请期待!!! 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/900147
推荐阅读
相关标签
  

闽ICP备14008679号