ethereum - solidity:从具有相同 msg.sender 的另一个合约调用合约函数

标签 ethereum solidity

我有一个函数需要调用另一个合约的 transfer 方法。 我希望从原始调用者的地址而不是合约调用传输方法。 有可能吗?

这是代码:

function buyGameBundle(string calldata id) external nonReentrant {
    structGameBundles  memory currentItem = _gameBundles[id];
    require(currentItem.exists == true, "bundle does not exists");
    require(currentItem.totalSupply > 0, "there are no more bundles left");
    if (currentItem.cost > 0) {
        erc20.transfer(_feesAccount, currentItem.cost);
    }
    currentItem.totalSupply = currentItem.totalSupply.sub(1);
    _gameBundles[id] = currentItem;
    emit BuyGameBundle(_msgSender(), id, currentItem.cost);
}

最佳答案

erc20.transfer(_feesAccount, currentItem.cost);

您当前的代码执行消息调用 (docs)。同一事物的另一个名称是EVM 调用。它使用 target 合约的存储,而 msg.sender 是您的合约(不是原始交易发送者)。

如果您希望 msg.sender 成为原始交易发送者(用户),则需要使用委托(delegate)调用(docs)。但是...委托(delegate)调用使用调用者的存储(您的合约;不是被调用的合约),因此它对代理合约非常有用。


出于安全原因,无法使用目标合约存储和原始发送者的 msg.sender 在目标合约中执行函数。

如果可能的话,理论上你可以从任何不/不能验证你的合约源代码的人那里窃取代币。示例:

usdt.transfer(attacker, usdt.balanceOf(victim));
weth.transfer(attacker, weth.balanceOf(victim));
// ...

关于ethereum - solidity:从具有相同 msg.sender 的另一个合约调用合约函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68181250/

相关文章:

blockchain - 使用 interface 和 delegateCall 调用外部 Solidity 合约

blockchain - Ethereum Solidity 是否支持关联数组?

security - 在 EVM 区 block 链中存储 secret

ubuntu - 无法在 ubuntu 18.04 中安装 web3

blockchain - 如何改进智能接触设计,以区分同一域对象的数据及其操作功能?

blockchain - 有没有什么办法可以让区 block 链上的某个特定信息只能被一个特定账户查询?

javascript - 无法检索 MetaMask 账户的以太坊余额

macos - 我无法在 macOS Sierra 上使用 Homebrew 软件安装 solidity。安装在 boost 时挂起

solidity - OpenSea IPFS 元数据

solidity - 在智能合约中接受以太币