Skip to main content

Contract Implementation

In GitHub, you will find a library and an example implementation of the ERC-20 token for the Casper Network. The ERC-20 standard is defined in an Ethereum Improvement Proposal (EIP).

The reference contract implementation is on the example contract file . The next sections explain the main functionalities of the ERC-20 contract in that file. Those are,

  1. Installing Required Crates
  2. Initializing the Contract
  3. Implementing Contract Methods
note

To successfully execute this reference contract you need to copy the full contract file with all the necessary imports, declarations, and functions. All those parts are required to compile the contract. To execute the contract you need to deploy the .wasm file on the network.

Installing Required Crates

Since this is a Rust implementation of the ERC-20 token for Casper, we will go over a few implementation details. Casper contracts require the following crates to be included:

  • casper_contract - A Rust library for writing smart contracts on the Casper Network
  • casper_types - Types used to allow creation of Wasm contracts and tests for use on the Casper Network
  • casper_erc20 - A library for developing ERC-20 tokens for the Casper Network

Here is the code snippet which imports those crates,

import-crates
note

In Rust, the keyword use is like an include statement in C/C++.

Initializing the Contract

Initializing the contract happens through the call() function inside the contract file. When you deploy the contract, you need to initialize it with a call() function and define name, symbol, decimals, and total_supply, which require to start the token supply.

The code snippet for initializing the contract should look like this:

call-function

Contract Methods

This section briefly explains the contract methods used in our ERC-20 contract.

To see the full implementation of the below contract methods, refer to the contract file in Github. If you have any questions, review the casper_erc20 library and the EIP-20 standard.

Also, for further unresolved issues please contact the casper development team via the Discord channel.

Contract methods are:

  • allowance - Returns the amount of owner’s tokens allowed to be spent by the spender
  • approve - Allows a spender to transfer up to an amount of the direct caller’s tokens
  • balance_of - Returns the token balance of the owner
  • decimals - Returns the decimals of the token
  • name- Returns the name of the token
  • symbol - Returns the symbol of the token
  • total_supply - Returns the total supply of the token
  • transfer - Transfers an amount of tokens from the direct caller to a recipient
  • transfer_from - Transfers an amount of tokens from the owner to a recipient, if the direct caller has been previously approved to spend the specified amount on behalf of the owner