我有一个函数需要调用另一个合约上的传输方法。我希望从原始调用者的地址而不是合约中调用转移方法。是否可以?
这是代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
erc20.transfer(_feesAccount, currentItem.cost);
Run Code Online (Sandbox Code Playgroud)
您当前的代码执行消息调用(文档)。同一事物的另一个名称是EVM 调用。它使用目标合约的存储,并且msg.sender是您的合约(而不是原始交易发送者)。
如果您希望msg.sender成为原始交易发送者(用户),则需要使用委托调用(文档)。但是... delegatecall 使用调用者的存储(您的合约;而不是被调用的合约),因此它对于代理合约最有用。
出于安全原因,无法使用目标合约存储和msg.sender原始发送者来执行目标合约中的功能。
如果可能的话,理论上你可以从任何不/无法验证你的合约源代码的人那里窃取代币。例子:
usdt.transfer(attacker, usdt.balanceOf(victim));
weth.transfer(attacker, weth.balanceOf(victim));
// ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7704 次 |
| 最近记录: |