首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将以太从一份合同转到另一份合同

将以太从一份合同转到另一份合同
EN

Ethereum用户
提问于 2022-01-21 11:20:57
回答 1查看 78关注 0票数 1

我目前正在尝试写两个聪明的合同。以下是第一个:

代码语言:javascript
复制
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract Token {
    mapping(address => uint) public userBalances;

    function buy() public payable {
        require(msg.value > 0, "Not enough coins sent");
        userBalances[msg.sender] += msg.value;
    }

    function withdrawBalance() public {
        uint amountToWithdraw = userBalances[msg.sender];
        // contains reentrancy attack vector here
        (bool success, ) = msg.sender.call{value: amountToWithdraw}("");
        require(success);
        userBalances[msg.sender] = 0;
    }
}

第二个例子是:

代码语言:javascript
复制
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "./Token.sol";

contract Attacker {
    Token public tokenContract;

    constructor(Token tokenContract) {
        tokenContract = tokenContract;
    }

    function buy() public payable {
        tokenContract.buy{ value: msg.value }();
    }
}

攻击者应通过合同转发以太,以购买一些令牌。然而,当调用攻击者契约的buy函数时,我遇到了问题。

这就是我在混音中得到的错误信息。

代码语言:javascript
复制
transact to Attacker.buy errored: VM error: revert.

revert
    The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.

我不知道这里出了什么问题。有人知道吗?

EN

回答 1

Ethereum用户

回答已采纳

发布于 2022-01-24 10:00:09

我告诉你出了什么问题:屏幕前的开发(facepalm)

攻击者契约的构造函数中的参数使用tokenContract作为名称。这将隐藏状态变量,从而导致问题。在这里,更正的代码:

代码语言:javascript
复制
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "./Token.sol";

contract Attacker {
    Token public tokenContract;

    constructor(Token _token) {
        tokenContract = _token;
    }

    function buy() public payable {
        tokenContract.buy{ value: msg.value }();
    }
}
票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/119555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档