How to set up an interface to an external contract with Vyper

Sam Richards
1 min readJul 13, 2019

--

Let’s say you want your Ethereum smart contract to interact with an existing smart contract (e.g. an ERC20 token). In order to accomplish that you’ll need to create an interface:

First, we define our ERC20Token interface. Notice we only have to include the functions we want to expose to our contract.

Second, we establish a storage variable of type ERC20Token, so we can reference the external contract across our contract functions (I don’t recommend this if you only need to call the external contract once).

Finally, in our contract’s constructor (or in any function), we can establish a reference to the external contract using the syntax ContractInterface(contract_address). We can now call the external contract’s methods. In this example, we’re calling the approve function of the ERC20Token contract, granting the Uniswap exchange approval to transfer our contract’s tokens.

If you also need to send value in your function call, see this post on how to send value in an external contract call.

FYI, these code examples use vyper version: 0.1.0b10

Happy coding! Leave a comment if you have any thoughts or questions.

--

--