In Chapter 8, we added WildPokemons
interface in the trainer contract. In this chapter, we will call the battle
function of the WildPokemon
interface.
If you recall from Chapter 8, we need a contract address to interact with WildPokemons
.
We will learn how to deploy a contract to Ethereum blockchain and get the contract address in future lessons. For the purpose of this chapter, I have deployed the WildPokemons
contract to the Rinkeby Testnet
Here is the contract address: 0x66f4804E06007630e1aF0a7B0b279e6F27A3FdE5
Using the contract address and the interface, you can make external calls to the interface functions (which was discussed in depth in Chapter 8):
interface Car:
def start() -> bool: nonpayable
def getSpeed() -> uint256: view
@external
def Driver(some_address: address):
Car(some_address).start()
Unlike Python, you cannot directly declare tuple types in Vyper. However, in certain cases you can use literal tuples during assignment. For example, when a function returns multiple values:
@internal
def getNameAndAge() -> (String[32], int128):
return "vasa", 22
@external
def getPerson():
name: String[32] = empty(String[32])
age: int128 = empty(int128)
# the return value of `getNameAndAge` is assigned using a tuple
(name, age) = self.getNameAndAge()
# Can also skip the parenthesis
name, age = self.getNameAndAge()
The battle
function returns the following 4 arguments:
battleResult
:bool
newPokemonName
:String[32]
newPokemonDNA
:uint256
newPokemonHP
:uint256
-
Inside
battleWildPokemon
, define these 4 function variables and initialize them using theempty
built-in function. -
Define a
constant
address
storage variable namedWILD_POKEMON
having the value0x66f4804E06007630e1aF0a7B0b279e6F27A3FdE5
. -
Inside
battleWildPokemon
, useWildPokemons
andWILD_POKEMON
to call thebattle
function that takes a single parameter of typePokemon
. This parameter can be obtained using thetrainerToPokemon
mapping by passing the 2 keys:msg.sender
as the first key andpokemonIndex
as the second key. -
Use the function variables created in step 1 to create a tuple in order to handle/capture the multiple return values.