Contract API

Initializing

Instantiating the Contract API.

// web3Api.ts
import naxios from '@wpdas/naxios'

const naxiosInstance = new naxios({
  contractId: 'dev-1692221685438-15421910364142',
  network: 'testnet',
})

/**
 * Greeting Contract API
 */
export const greetingContractApi = naxiosInstance.contractApi()

You can instantiate it using a cache system too:

// web3Api.ts
import naxios, { StorageCache } from '@wpdas/naxios'

const naxiosInstance = new naxios({
  contractId: 'dev-1692221685438-15421910364142',
  network: 'testnet',
})

/**
 * Cached - Greeting Contract API
 */
const cache = new StorageCache({ expirationTime: 60 }) // expiration time in seconds
export const greetingContractApi = naxiosInstance.contractApi({ cache })

Get to know more about the cache system by clicking on the link below:

API Reference

  • view: Make a read-only call to retrieve information from the network. It has the following parameters:

    • method: Contract's method name.

    • props?: an optional parameter with args for the contract's method.

    • config?: currently, this has only the useCache prop. When useCache is true, this is going to use non-expired cached data instead of calling the contract's method.

  • call: Call a method that changes the contract's state. This is payable. It has the following parameters:

    • method: Contract's method name

    • props?: an optional parameter with args for the contract's method, gas, deposit to be attached and callbackUrl if you want to take the user to a specific page after a transaction succeeds.

  • callMultiple: Call multiple methods that change the contract's state. This is payable and has the following parameters:

    • transactionsList: A list of Transaction props. You can use buildTransaction(...) to help you out

    • callbackUrl?: A page to take the user to after all the transactions succeed.

Contract View

Using a view method is free.

import { greetingContractApi } from './web3Api'

// [free]
greetingContractApi.view<string>('get_greeting')
  .then((response) => console.log(response))

Contract Call

You need to pay for every request you make for acall method. This is going to change data and store it within the blockchain.

import { greetingContractApi } from './web3Api'

// Set greeting [payable]
const args: { message: 'Hi there!!!' }
greetingContractApi.call<string | undefined>('set_greeting', args)
  .then((response) => console.log(response || 'done!'))

Contract Multiple Calls at Once

As well as the call, you will need to pay for every request you make. This is going to change data and store it within the blockchain.

import { buildTransaction } from '@wpdas/naxios'
import { contractApi } from './web3Api'

// Using the default instance's contract
const transactionA = buildTransaction('set_greeting', { args: { greeting: 'Hello my dear!' } })
const transactionB = buildTransaction('set_age', { args: { age: 22 } })
// Using diff contract
const transactionC = buildTransaction('update_state', {
  receiverId: 'my-state-contract.testnet',
  args: { allowed: true },
})

// Optional
const callbackUrl = 'https://my-page.com/callback-success'

// [payable]
contractApi.callMultiple([transactionA, transactionB, transactionC], callbackUrl).then(() => console.log('Done!'))

Last updated