Skip to main content

Quick start

Requirements

  • Node.js version 18.16.1 or higher:
    • When you install Node.js we recommend checking all your dependencies and their compatibility.

Installation

We install the package from npm

pnpm install @alfabc/sdk
note

You can use your desired package manager, npm, pnpm or yarn.

Modules

The sdk is composed of different modules or packages that are available from the Alfa class. This class is imported from the @alfabc/sdk package.

import { Alfa } from "@alfabc/sdk"

To create an instance of the class, an object with the main configurations will be passed as an argument. Which are detailed below.

Settings

The settings object will have the following options:

info

The apiKey and host options are required.

  • apiKey string

    This is the api key provided by Alfa.

  • host string

    This is the link to the api services provided by Alfa.

  • local boolean

    Optional. By default it is false and means that the transactions will be sent to the server. In true it means that the transactions will be sent to a local ethereum provider.

    caution

    If you use local mode it means that you are using the sdk from the frontend. The sdk will try to connect to an ethereum wallet from the browser. In addition, the wallet module will not be available if the local mode is not activated.

  • chainIds number[]

    An array with the ids of the networks supported by your application. Take into account that they must be evm networks. This arrangement will be used locally by the wallet to allow operations on the blockchain. For example:

    constchainIds = [1,5,80001];
  • wallets Map<string, string>

    caution

    Required if local mode is set to false

    Key map with Ethereum addresses, this is what we will call caller. It will be used to determine which account is the one that executes the transactions in the network. At least one address is needed and its key can be any.

    Example:

    const wallets = new Map<string, string>();
    const myWalletAddress = '0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703';

    wallets.set('default', myWalletAddress);

    If you use in your application users with different ethereum accounts you can pass the following:

    const user1 = ['John', '0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703'];
    const user2 = ['Peter', '0xa73a3b8ACa335855EeaC2f9Fb506BB0360A1B702'];
    const user3 = ['Lucas', '0xa73a3b8ACa335855EeaC2f1Fb505BB0360A1C603'];

    const wallets = new Map<string, string>([user1, user2, user3]);
    info

    As wallets are a Map of ethereum keys and accounts. You could use the reference to this object to dynamically add or remove accounts at runtime.

  • ipfs object

    • useCache boolean

      Activate the cache of downloaded files from ipfs or mfs in the frontend.

      caution

      This option only works in local mode

  • logLevel LogLevel

    LogLevel can be log | warn | error | debug ;

Initialize

To initialize it is enough to pass the object with the basic configurations.

const sdk = new Alfa({
apiKey: '96b7ac903dd1b10fd4b14be043e10b64d1e8c525d8e188c559cfc4c1f8c09c99',
host: 'https://web3.alfabc.io',
local: false,
wallets: new Map(['default','0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703']),
logLevel: 'warn'
});