Skip to main content

ethereum

Offers functionalities for accessing and interacting with smart contracts, including ABI (Application Binary Interface) handling, as well as encoding and decoding for various data types.

1. Function

Several methods commonly used for ABI operations are defined, ethereum namespace function list.

1. abiEncode

abi encode

   function abiEncode(method: string, types: Type[]): string
  • Parameter

    • string : method
    • types : ethereum.Type array
  • Returns

    • abi encode hex string
  • Example

import {
ethereum
} from "@artela/aspect-libs";

{
let payload = ethereum.abiEncode('store', [num]);
}

2. parseMethodSig

parse Method signature

   function parseMethodSig(calldata: Uint8Array): string
  • Parameter

    • Uint8Array : method name bytes.
  • Returns

    • string : string method name
  • Example

import {ethereum} from "@artela/aspect-libs";

{
// parseMethodSig to string "add"
let add = ethereum.parseMethodSig([4f2be91f]);
}

3. computeMethodSig

compute Method signature

   function computeMethodSig(method: string): string
  • Parameter

    • string : method name
  • Returns

    • string : method signature hex string
  • Example

import {ethereum} from "@artela/aspect-libs";

{
// parseMethodSig to string "f2be91f"
let addSig = ethereum.computeMethodSig("add");
}

2. Types

Define the data type in the smart contract and the data type conversion, encoding, and decoding operations of AssemblyScript.

  1. ethereum.Address
  2. ethereum.ArraySlice
  3. ethereum.Boolean
  4. ethereum.ByteArray
  5. ethereum.Bytes
  6. ethereum.BytesN
  7. ethereum.Int
  8. ethereum.Number
  9. ethereum.String
  10. ethereum.Tuple
  11. ethereum.Uint

3. UseCases

generate the abi encode of the function function execute(address account,uint64 num,bytes calldata data) public


let contractAddress="0x5B38Da6a701c568545dCfcB03FcB875f56beddC4"
let txData="0x112233"

const callData = ethereum.abiEncode('execute', [
ethereum.Address.fromHexString(contractAddress),
ethereum.Number.fromU64(0),
ethereum.Bytes.fromHexString(txData),
]);