NEAR Blockchain

The Fungible Token Standard

Lesson 3 Module 1

The NEAR Fungible Token Standard implements an interface with four change methods and three view methods.  By contrast, the Ethereum ERC-20 standard implements three optional methods, two view methods, four change methods and two events.  These differences are worth taking a moment to discuss.

When I'm talking about view and change methods, I'm referring to whether the function is able to change state.  A view method will just query the chain and return some data.  They don't cost anything to use.  A change method does cost something because it sends a transaction that results in an update to what is stored in the contract's state.

Ethereum has the concept of events which are emitted when defined actions occur on the chain.  A frontend can then listen for those events and update when they happen.  NEAR doesn't currently have events.  To get similar functionality we have to poll the chain which is something we'll implement in our project later on.

Here are the minimum methods the fungible token spec expects to be handled:

Change Methods

// increments the 'allowance' for the identified escrow account Id by an amount on the caller of the contract ('predecessor_id') who is the balance owner
inc_allowance(escrow_account_id: AccountId, allowance: Amount): void {}

// decrements the 'allowance' for the identified escrow account Id by an amount on the caller of the contract ('predecessor_id') who is the balance owner
dec_allowance(escrow_account_id: AccountId, allowance: Amount): void {}

// transfers tokens from owner_id to new_owner_id
transfer_from(owner_id: AccountId, new_owner_id: AccountId, amount: Amount): void {}

It's interesting that the next function exists as it simply calls transfer_from inside it.  It just behaves as if the owner_id in transfer_from is equal to the caller of the contract.  The NEAR non fungible token spec has done away with it.  At any rate, it's still part of the spec, so it's here.

// transfers an amount of tokens from caller of contract to new_owner_id
transfer(new_owner_id: AccountId, amount: Amount): void {}

View Methods

// returns total supply of tokens
get_total_supply(): u128 {}

// returns balance of owner_id account
get_balance(owner_id: AccountId): u128 {}

The next view function should only be used by the frontend.  Other contracts must not rely on this information to be accurate because the moment a contract receives the information it may have already been changed by the owner.

// returns allowance of escrow_account_id for account of owner_id
get_allowance(owner_id: AccountId, escrow_account_id: AccountId): u128 {}