Solidity

如何在給定地址和呼叫數據的合約中執行交易?

  • May 7, 2021

假設我有一份名為 的契約MyContract。我的合約有一個函式,它接收 calldata 和另一個合約的地址,稱為OtherContract

我希望能夠在 MyContract 中獲取 calldata 和地址,並從 MyContract 中執行 OtherContract 中的 callData。

我對 OtherContract 或使用 calldata 的函式一無所知。

這可能嗎?

謝謝你的時間!!!

是的,可以僅使用地址和數據呼叫任意函式。

function foo(address target, bytes memory data) external {
   // Make the function call
   (bool success, bytes memory result) = target.call(data);

   // success is false if the call reverts, true otherwise
   require(success, "Call failed");

   // result contains whatever has returned the function
   emit ExecutionResult(success, result);
}

您可以檢查函式呼叫是否恢復。由於您對契約一無所知,因此無法解釋結果,但您可以返回它們。

撥打電話更改msg.sender。對於目標合約,發出呼叫的合約似乎是發送者。

引用自:https://ethereum.stackexchange.com/questions/98479