Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Variables

DEFAULT_MAX_BUFFER_SIZE: 1024 = 1024

Functions

  • applyBlock(block: block): i32
  • call(contractId: Uint8Array, entryPoint: number, contractArgs: Uint8Array): callReturn
  • Call a contract

    example
    // Transfer 10 tKOIN to 1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe
    const koinContractId = Base58.decode("1NvZvWNqDX7t93inmLBvbv6kxhpEZYRFWK");
    const tranferEntryPoint = 0x62efa292;
    const from = contractId; // this contract
    const to = Base58.decode("1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe");
    const amount = 10 * 10 ** 8; // needs to be multiplied by 10^8 because Koin is 8 decimals

    const koinTransferArgs = new koin.transfer_arguments();
    koinTransferArgs.from = from;
    koinTransferArgs.to = to;
    koinTransferArgs.value = amount;

    const resBuf = System.callContract(koinContractId, tranferEntryPoint, Protobuf.encode(koinTransferArgs, koin.transfer_arguments.encode));
    System.require(resBuf, `expected resBuf not "null", got "null"`);

    if (resBuf) {
    const transferRes = Protobuf.decode<koin.transfer_result>(resBuf, koin.transfer_result.decode, RETURN_BYTES[0]);
    System.require(transferRes.value, `expected transfer not "true", got "false"`);

    const impacted: Uint8Array[] = [];
    impacted.push(from);
    impacted.push(to);

    const transferEvent = new token.transfer_event();
    transferEvent.from = from;
    transferEvent.to = to;
    transferEvent.value = amount;

    System.event('koin.transfer', Protobuf.encode(transferEvent, token.transfer_event.encode), impacted);

    System.log(`transfered ${amount / 10 ** 8} tKoin from ${Base58.encode(from)} to ${Base58.encode(to)}`);
    }

    Parameters

    • contractId: Uint8Array

      id of the contract to call

    • entryPoint: number

      entry point of the contract to call

    • contractArgs: Uint8Array

      arguments of the contract to call

    Returns callReturn

    Uint8Array | null

  • checkAuthority(type: authorization_type, account: Uint8Array, data?: null | Uint8Array): bool
  • Check authority for an account

    example
    System.checkAuthority(authority.authorization_type.transaction_application, Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe));
    

    Parameters

    • type: authorization_type

      type of authority required

    • account: Uint8Array

      account to check

    • data: null | Uint8Array = null

    Returns bool

    bool true if the account has authority

  • checkSystemAuthority(): bool
  • consumeAccountRC(account: Uint8Array, value: number): bool
  • consumeBlockResources(disk_storage_consumed: number, network_bandwidth_consumed: number, compute_bandwidth_consumed: number): bool
  • Parameters

    • disk_storage_consumed: number
    • network_bandwidth_consumed: number
    • compute_bandwidth_consumed: number

    Returns bool

  • event(name: string, data: Uint8Array, impacted: Uint8Array[]): void
  • Emit an event

    example
    const from = Base58.decode("1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe");
    const to = Base58.decode("1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe");
    const impacted: Uint8Array[] = [];
    impacted.push(from);
    impacted.push(to);

    const transferEvent = new token.transfer_event();
    transferEvent.from = from;
    transferEvent.to = to;
    transferEvent.value = amount;

    System.event('koin.transfer', Protobuf.encode(transferEvent, token.transfer_event.encode), impacted);

    Parameters

    • name: string

      name of the event

    • data: Uint8Array

      data associated to the event

    • impacted: Uint8Array[]

      accounts impacted by the event

    Returns void

  • exit(code: number, value?: null | Uint8Array): void
  • Exit a contract

    example
    System.exitContract(0);
    

    Parameters

    • code: number
    • value: null | Uint8Array = null

    Returns void

  • fail(message?: string, code?: number): void
  • Fail the transaction in progress

    Parameters

    • message: string = ""

      Optional failure message

    • code: number = -1

      Optional error code, must be < 0, else code -1 is used (failure exit code)

      if (!System.checkAuthority(authority.authorization_type.transaction_application, Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe)))
      System.fail("contract is not authorized");

    Returns void

  • getAccountNonce(account: Uint8Array): Uint8Array | null
  • getAccountRC(account: Uint8Array): u64
  • Get arguments that were used when calling the contract

    example
    const rdbuf = System.getContractArguments();
    const contractArgs = Protobuf.decode<foobar.foobar_arguments>(rdbuf, foobar.foobar_arguments.decode, RETURN_BYTES[0]);
    System.log('contractArgs: ' + contractArgs.value.toString());

    Returns getArgumentsReturn

    Uint8Array

  • Get the current block

    example
     const b = System.getBlock();
    System.log("signer: " + Base58.encode((b.header!.signer!)));

    Returns block

    protocol.block

  • Get a field from the current block

    example
    const blField = System.getBlockField('header.signer');
    System.require(blField, `expected blField not "null", got "null"`);

    if (blField) {
    System.log("signer: " + Base58.encode(blField.bytes_value!));
    }

    Parameters

    • field: string

      field to get (i.e.: 'id', 'header.signer')

    Returns value_type | null

    value.value_type | null

  • Get bytes (Uint8Array)

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);
    let obj = System.getBytes(contractSpace, StringBytes.stringToBytes('key2'));

    if (obj) {
    const str = StringBytes.bytesToString(obj)!;
    System.log(str);
    }

    Type Parameters

    • K

    Parameters

    • space: object_space

      space where to get the object

    • key: K

      key of object to get

    Returns Uint8Array | null

    Uint8Array | null

  • Get the contract caller information

    example
    const callerData = System.getCaller();
    System.log('callerData.caller_privilege: ' + callerData.caller_privilege.toString());
    if (callerData.caller) {
    System.log('callerData.caller (b58): ' + Base58.encode(callerData.caller!));
    }

    Returns caller_data

    chain.caller_data

  • getChainId(): Uint8Array
  • getContractAddress(name: string): Uint8Array
  • Get the address for a given system contract name

    example
    const address = System.getContractAddress('koin');
    System.log('address (b58): ' + Base58.encode(address));

    Parameters

    • name: string

      The name of the system contract

    Returns Uint8Array

    Uint8Array The contract's address

  • getContractId(): Uint8Array
  • Get the id of the contract

    example
    const contractId = System.getContractId();
    System.log('contractId (b58): ' + Base58.encode(contractId));

    Returns Uint8Array

    Uint8Array

  • getContractName(address: Uint8Array): string
  • Get the name of a system contract for a given address

    example
    const name = System.getContractName(Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe'));
    System.log('contract name: ' + name);

    Parameters

    • address: Uint8Array

      The address of the system contract

    Returns string

    string The contract's name

  • getErrorMessage(): string
  • Gets a stored error message after a system call that can return an error.

    example
    if (System.applyTransaction(trx) != error.error_code.success)
    System.log(getErrorMessage())

    Returns string

    string

  • Get the blockchain head information (head block time, height, last irreversible block, etc...)

    example
     const headInfo = System.getHeadInfo();
    System.log('headInfo.head_block_time: ' + headInfo.head_block_time.toString());
    System.log('headInfo.head_topology.height: ' + headInfo.head_topology!.height.toString());
    System.log('headInfo.last_irreversible_block.: ' + headInfo.last_irreversible_block.toString());

    Returns head_info

    chain.head_info

  • getLastIrreversibleBlock(): u64
  • Get the last irreversible block height

    example
    const lastIrreversibleBlock = System.getLastIrreversibleBlock();
    System.log('lastIrreversibleBlock: ' + lastIrreversibleBlock.toString());

    Returns u64

    u64

  • Get next bytes (Uint8Array)

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);
    let obj = System.getNextBytes(contractSpace, StringBytes.stringToBytes('key2'));

    if (obj) {
    System.log('obj.value: ' + obj.value.toString());
    }

    Type Parameters

    • K

    Parameters

    Returns database_object | null

    system_calls.database_object

  • Get next proto object

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);
    let obj = System.getNextObject<string, test.test_object>(contractSpace, 'key3', test.test_object.decode);

    if (obj) {
    System.log('next obj.value: ' + obj.value.value.toString());
    }

    Type Parameters

    • K

    • TMessage

    Parameters

    • space: object_space
    • key: K

      key of object

    • decoder: ((reader: Reader, length: number) => TMessage)
        • (reader: Reader, length: number): TMessage
        • Parameters

          • reader: Reader
          • length: number

          Returns TMessage

    Returns ProtoDatabaseObject<TMessage> | null

    proto object (TMessage)

  • getObject<K, TMessage>(space: object_space, key: K, decoder: ((reader: Reader, length: number) => TMessage)): TMessage | null
  • Get proto object

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);
    let obj = System.getObject<string, test.test_object>(contractSpace, 'key2', test.test_object.decode);

    if (obj) {
    System.log('obj.value: ' + obj.value.toString());
    }

    Type Parameters

    • K

    • TMessage

    Parameters

    • space: object_space

      space where to get the object

    • key: K

      key of object to get

    • decoder: ((reader: Reader, length: number) => TMessage)
        • (reader: Reader, length: number): TMessage
        • Parameters

          • reader: Reader
          • length: number

          Returns TMessage

    Returns TMessage | null

    proto object (TMessage) or null

  • Get next bytes (Uint8Array)

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);
    let obj = System.getPrevBytes(contractSpace, StringBytes.stringToBytes('key2'));

    if (obj) {
    System.log('obj.value: ' + obj.value.toString());
    }

    Type Parameters

    • K

    Parameters

    Returns database_object | null

    system_calls.database_object

  • Get previous proto object

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);
    let obj = System.getPrevObject<string, test.test_object>(contractSpace, 'key3', test.test_object.decode);

    if (obj) {
    System.log('next obj.value: ' + obj.value.value.toString());
    }

    Type Parameters

    • K

    • TMessage

    Parameters

    • space: object_space
    • key: K

      key of object

    • decoder: ((reader: Reader, length: number) => TMessage)
        • (reader: Reader, length: number): TMessage
        • Parameters

          • reader: Reader
          • length: number

          Returns TMessage

    Returns ProtoDatabaseObject<TMessage> | null

    proto object (TMessage)

  • getSystemBufferSize(): u32
  • Get the current transaction

    example
     const tx = System.getTransaction();
    System.log("payer: " + Base58.encode((tx.header!.payer!)));

    Returns transaction

    protocol.transaction

  • getTransactionField(field: string): value_type | null
  • Get a field from the current transaction

    example
     const txField = System.getTransactionField('header.payer');
    if (txField) {
    System.log("payer: " + Base58.encode(txField.bytes_value!));
    }

    Parameters

    • field: string

      field to get (i.e.: 'id', 'header.payer')

    Returns value_type | null

    value.value_type | null

  • hash(code: number, obj: Uint8Array, size?: number): Uint8Array | null
  • Hash an object

    example
    const digest = System.hash(Crypto.multicodec.sha2_256, StringBytes.stringToBytes('hello world!));
    

    Parameters

    • code: number

      a Crypto.multicodec code

    • obj: Uint8Array

      object to hash

    • size: number = 0

      size of the object to hash

    Returns Uint8Array | null

    Uint8Array | null

  • log(s: string): void
  • processBlockSignature(digest: Uint8Array, header: block_header, signature: Uint8Array): bool
  • putBytes<K>(space: object_space, key: K, obj: Uint8Array): void
  • Store bytes (Uint8Array)

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);

    const nbBytesWritten = System.putBytes(contractSpace, 'testKey', StringBytes.stringToBytes('testValue'));
    System.log('nbBytesWritten: ' + nbBytesWritten.toString());

    Type Parameters

    • K

    Parameters

    • space: object_space

      space where to put the byets

    • key: K

      key of the bytes to store (string or Uint8Array)

    • obj: Uint8Array

      bytes to store (Uint8Array)

    Returns void

    number of bytes that were put in the database

  • putObject<K, TMessage>(space: object_space, key: K, obj: TMessage, encoder: ((message: TMessage, writer: Writer) => void)): void
  • Store proto object

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);
    const obj = new test.test_object(42);

    const nbBytesWritten = System.putObject(contractSpace, "test", obj, test.test_object.encode);
    System.log('nbBytesWritten: ' + nbBytesWritten.toString());

    Type Parameters

    • K

    • TMessage

    Parameters

    • space: object_space

      space where to put the object

    • key: K

      key of the object to store (string or Uint8Array)

    • obj: TMessage

      object to store (string or Uint8Array)

    • encoder: ((message: TMessage, writer: Writer) => void)
        • (message: TMessage, writer: Writer): void
        • Parameters

          • message: TMessage
          • writer: Writer

          Returns void

    Returns void

    number of bytes that were put in the database

  • recoverPublicKey(signatureData: Uint8Array, digest: Uint8Array, type?: ecdsa_secp256k1, compressed?: bool): Uint8Array | null
  • Recover a publick key given a signature and a digest that was signed by the public key

    example
    const message = 'hello-world';
    const signatureData = Base64.decode('IHhJwlD7P-o6x7L38den1MnumUhnYmNhTZhIUQQhezvEMf7rx89NbIIioNCIQSk1PQYdQ9mOI4-rDYiwO2pLvM4=');
    const digest = System.hash(Crypto.multicodec.sha2_256, StringBytes.stringToBytes(message));
    const recoveredKey = System.recoverPublicKey(signatureData, digest!);
    const addr = Crypto.addressFromPublicKey(recoveredKey!);
    System.log('recoveredKey (b58): ' + Base58.encode(addr));

    Parameters

    • signatureData: Uint8Array

      the signature of the digest

    • digest: Uint8Array

      digest that was signed by the public key

    • type: ecdsa_secp256k1 = chain.dsa.ecdsa_secp256k1

      type of signature

    • compressed: bool = true

      whether the public key should be compressed

    Returns Uint8Array | null

    Uint8Array | null

  • Remove an object

    example
    const contractId = System.getContractId();
    const contractSpace = new chain.object_space(false, contractId, 1);

    System.removeObject(contractSpace, 'testKey');

    Type Parameters

    • K

    Parameters

    • space: object_space

      space where to put the byets

    • key: K

      key of the bytes to store (string or Uint8Array)

    Returns void

  • require<T>(isTrueish: T, message?: string, code?: number): T
  • Require an expression to be true, log a message and exit the contract otherise

    example
    System.require(1 + 1 == 11, `expected "11", got "2"`);
    

    Type Parameters

    • T

    Parameters

    • isTrueish: T
    • message: string = ""
    • code: number = 1

    Returns T

    T it is Trueish, will exit the contract with exitCode otherwise

  • Require authority for an account

    throws

    revert the transaction if the account is not authorized

    example
    System.requireAuthority(authority.authorization_type.transaction_application, Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe));
    

    Parameters

    Returns void

  • requireSystemAuthority(): void
  • revert(message?: string, code?: number): void
  • Revert the transaction in progress

    Parameters

    • message: string = ""

      Optional reversion message

    • code: number = 1

      Optional error code, must be > 0, else code 1 is used (reverted exit code)

      if (!System.checkAuthority(authority.authorization_type.transaction_application, Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe)))
      System.revert("contract is not authorized");

    Returns void

  • setAccountNonce(account: Uint8Array, nonce: Uint8Array): void
  • setSystemBufferSize(size: number): void
  • verifyAccountNonce(account: Uint8Array, nonce: Uint8Array): bool
  • verifyMerkleRoot(root: Uint8Array, hashes: Uint8Array[]): bool
  • Verify a merkle root

    example
    
    

    Parameters

    • root: Uint8Array

      merkle root to verify

    • hashes: Uint8Array[]

      hashes to verify

    Returns bool

    bool

  • verifySignature(publicKey: Uint8Array, signature: Uint8Array, digest: Uint8Array, type?: ecdsa_secp256k1, compressed?: bool): bool
  • Verify that a public key signed a digest

    example
    let verify = System.verifySignature(recoveredKey!, signatureData, digest!);
    System.require(verify == true, `expected "true", got "${verify}"`);

    Parameters

    • publicKey: Uint8Array

      public key that signed the digest

    • signature: Uint8Array

      signature of the digest

    • digest: Uint8Array

      digest that was signed

    • type: ecdsa_secp256k1 = chain.dsa.ecdsa_secp256k1

      type of signature

    • compressed: bool = true

      whether or not the public key is compressed

    Returns bool

    bool

  • verifyVRFProof(publicKey: Uint8Array, proof: Uint8Array, hash: Uint8Array, message: Uint8Array, type?: ecdsa_secp256k1): bool
  • Verifies a VRF proof

    Parameters

    • publicKey: Uint8Array

      public key that generated the proof

    • proof: Uint8Array

      the VRF proof itself

    • hash: Uint8Array

      the hash of the proof

    • message: Uint8Array

      the original message input

    • type: ecdsa_secp256k1 = chain.dsa.ecdsa_secp256k1

      type of signature

    Returns bool

Generated using TypeDoc