Contract Mixins

NEO•ONE provides mixins for common smart contract patterns.

This guide will walk you through how to use a mixin.



Requirements

Install the following packages if you have not done so:

yarn add @neo-one/client @neo-one/cli @neo-one/smart-contract @neo-one/smart-contract-test @neo-one/smart-contract-lib @neo-one/smart-contract-typescript-plugin
npm install @neo-one/client @neo-one/cli @neo-one/smart-contract @neo-one/smart-contract-test @neo-one/smart-contract-lib @neo-one/smart-contract-typescript-plugin

Important

The package @neo-one/smart-contract-lib contains the mixins and is important to this guide.


What Is A Mixin?

A mixin is a class (abstract or non-abstract) in which some or all of its methods and/or properties are unimplemented.

We use mixin as a way to insert common properties and methods (sometimes required such as in the case of meeting a token standard) into your contract.

Tip

You can think of our mixins as a template or a base where you can build your contracts on.


Usage

import { SmartContract } from '@neo-one/smart-contract';
// import template of your choice from @neo-one/smart-contract-lib
import { NEP5Token } from '@neo-one/smart-contract-lib';

// Mixins
export class YourContract extends NEP5Token(SmartContract) {
  // 'SmartContract' here can be any other contract (abstract & non-abstract class) that extends SmartContract class
  // 'YourContract' inherits the methods and properties defined in NEP5TokenClass returned by NEP5Token
  // ... your smart contract code.
}

If you find the syntax NEP5Token(SmartContract) strange, checkout this page on Mixins from the TypeScript Handbook for more details.

Tip

We encourage you to look at the NEP5Token mixin to understand how we are implementing a NEP5 token. Go here to see the source code for the NEP5Token mixin


Example

The following example set uses a mixin to design a new token that follows the NEP5 token standard.

Note

A token standard simply defines a set of methods and properties that must exist in the token.

SimpleToken.ts

SimpleToken is injected with NEP5Token's methods and properties.

import { Address, Fixed, SmartContract } from '@neo-one/smart-contract';
import { NEP5Token } from '@neo-one/smart-contract-lib';

export abstract class SimpleToken extends NEP5Token(SmartContract) {
  public readonly owner: Address;
  public readonly decimals: 8 = 8;

  public constructor(owner: Address, amount: Fixed<8>) {
    super();
    if (!Address.isCaller(owner)) {
      throw new Error('Sender was not the owner.');
    }
    this.owner = owner;
    this.issue(owner, amount);
  }
}

Redtoken.ts

RedToken inherits methods and properties that adheres to the NEP5 token standard because of the mixin. It also inherits everything defined in SimpleToken.

import { Address, Deploy, Fixed } from '@neo-one/smart-contract';
import { SimpleToken } from './SimpleToken';

export class RedToken extends SimpleToken {
  public readonly name: string = 'RedToken';
  public readonly symbol: string = 'RT';

  public constructor(owner: Address = Deploy.senderAddress, amount: Fixed<8> = 1_000_000_00000000) {
    super(owner, amount);
  }
}

Available Templates

  • NEP5Token - NEP5 Token Standard
  • ICO - Initial Coin Offering.
  • Ownership/Ownable - This mixin provides a means to assign an address as the owner of a contract. Extending this class and adding this.ownerOnly(); to the beginning of all public functions will throw an error anytime an address other than the primary makes requests.
  • Ownership/Secondary - This mixin provides a means to mark an address as the primary caller of this contract. Extending this class and adding this.primaryOnly(); to the beginning of all public functions will throw an error anytime an address other than the primary makes requests.
Edit this page
  • Quick Start
  • Getting Started
  • Environment Setup
  • CLI
  • Playground
  • Contract Mixins
Previous Article
Playground
Next Article
Hello World
DOCS
InstallationMain ConceptsAdvanced GuidesAPI ReferenceContributing
CHANNELS
GitHubStack OverflowDiscord ChatTwitterYouTube
COPYRIGHT © 2021 NEO•ONE