Calling Other Contracts

Apr 29, 2024 11:46:57 PM

The onNEP17Payment Callback

If the recipient is a smart contract, we need to invoke it it passing the correct parameters.

The implementation of the onNEP17Payment method is up to the developer. An exception is thrown if the method is not implemented.

The callback should be called after triggering the Transfer event.

Invoking Other Smart Contracts

Smart contracts can call other contracts using the CallContract method. This method receives the script hash of the contract to call, the method to invoke, and the parameters to pass to the method.

Using the ContractManagement Native Contract

Use the built-in ContractManagement native contract to verify if the recipient is a smart contract and if it implements the onNEP17Payment method. If it does, then we need to call it passing the correct parameters.

Checking if the Recipient is a Smart Contract

This part of the code checks if the recipient is a smart contract. If it is, then we use Call Contract to invoke the onNEP17Payment callback.

Coin.py

We need to add the code above if we want to make sure the COIN contract is compatible with the NEP-17 standard. If we don’t add this code, the contract will still work, but it won’t be NEP-17 compliant.

Accepting NEP-17 Transfers

If you don’t implement the onNEP17Payment method, your contract won’t be able to accept NEP-17 tokens, including $NEO and $GAS.

Add the following code to allow your contract to accept tokens:

Coin.py

Let’s add some metadata to the COIN contract. The metadata is used by explorers and other applications to display information about the token.

The metadata also contains information about permissions and other details about the token. By default, contracts can’t call other contracts. We need to add it to the contract manifest.

In Go, the metadata is added to the project config file, while in other programming languages, it’s added directly to the contract file.