Interact with other smart contracts
.call()
function, which allows you to interact with other smart contracts without using an interface.
call()
function to interact with another contract without using an interface.call()
functionPoolCreator
with the following code:
IUniswapV3Factory
interface. The interface contains function declarations that include getPool
and createPool
:IUniswapV3Factory
.createPool
function includes a validation to ensure the pool doesn’t exist.createPool
function creates a new pool.PoolCreator.test.ts
with the following content:
0x1F98431c8aD98523631AE4a59f267346ea31F984
is the address of the Uniswap pool factory deployed to the Ethereum mainnet. This can be verified by looking at the Uniswap documentation that includes the Deployment addresses of the contracts.Token
contract.npx hardhat test
and you should get a result similar to the following:
.call()
call
.
Using call
, you can call any contract as long as you know minimal information of the function signature. In this case, you should at least know that createPool
requires three parameters:
abi.encodeWithSignature
, you encode the payload required to make a smart contract call using the .call()
function..call()
doesn’t require you to import the interface.mload
.npx hardhat test
and you should expect the same result:
.call
function are two ways to interact with external contracts. Using interfaces provides several advantages, including type safety, code readability, and compiler error checking. When interacting with well-documented contracts like Uniswap, using interfaces is often the preferred and safest approach.
On the other hand, the .call
function offers more flexibility but comes with greater responsibility. It allows developers to call functions on contracts even without prior knowledge of their interfaces. However, it lacks the type safety and error checking provided by interfaces, making it more error-prone.