# 調用其他合約機制 知道 ABI 地址,可以直接 `address($合約地址).methods` 不知道 ABI 地址,使用 call, 或者 delegateCall ### call 的作用域 => 誰被調用,就是誰 ### delegateCall 的作用域 => 初始調用者  ```solidity= pragma solidity >0.8.0; contract B { string greeting = "Hello"; event Greeting(string greeting); event sender(address sender); function greet() external { emit Greeting(greeting); emit sender(msg.sender); } } contract A { string greeting = "Hi"; function greetByDelegateCall(address _contract) external { (bool success,) = _contract.delegatecall( abi.encodeWithSignature("greet()") ); } function greetByCall(address _contract) external { (bool success,) = _contract.call( abi.encodeWithSignature("greet()") ); } } ``` **兩者的作用域不同** 使用合約 A 的 `greetByDelegateCall` Greeting 回傳 `Hi`, sender 回傳 `自己的地址` 使用合約 A `greetByCall` Greeting 回傳 `Hello`, sender 回傳 `B 合約的地址` ## Reference https://medium.com/coinmonks/delegatecall-calling-another-contract-function-in-solidity-b579f804178c
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up