Calling Other Contracts

Mar 12, 2024 11:28:29 PM

NEP-17 Transfer Implementation

The transfer method must be implemented following rules defined in the standard.

They are:

  • The transfer method must trigger the Transfer event, even if the value is 0.
  • The transfer method must check if the recipient is a smart contract and invoke the onNEP17Payment method if it is.
    • The should be called after triggering the Transfer event.
    • If the recipient wishes to refuse the transfer, it must throw an exception.
    • If the recipient doesn’t implement the onNEP17Payment method, an exception is thrown.
  • If the user doesn’t have enough funds, the transfer method must return False.
  • If the user is not authorized to transfer the funds, the transfer method must return False.
  • If the parameters are invalid, the transfer method must throw an exception.

The transfer method from the previous class already follows all these rules. We are going to refer to it in the next sections.

Triggering the Transfer Event

The event must be triggered if the transfer is valid, even if the value is 0 or if the recipient is the same as the .

This is the same event we are triggering when we mint tokens during deployment.

The following line is responsible for triggering the event:

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.

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 $ and $GAS.

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

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.