gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:44:06 24 4
gpt4 key购买 nike

我有一个函数需要调用另一个合约的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com