Skip to main content

@portkey/contracts

Introduction

Enable contract deployment.

Installation

npm install @portkey/contracts

Usage

import { getContractBasic } from "@portkey/contracts";

// use ca contract
const contract = await getContractBasic({
chainType: "aelf",
account: "your account",
contractAddress: "contractAddress",
caContractAddress: "caContractAddress",
callType: "ca",
caHash: "caHash",
rpcUrl: "rpcUrl",
});

// use base contract
const contract = await getContractBasic({
account: "your account",
contractAddress: "contractAddress",
chainType: "aelf",
rpcUrl: "rpcUrl",
});

// use portkey provider
import detectProvider from "@portkey/detect-provider";
// detect provider
const provider = await detectProvider();
// get chain provider
const chainProvider = await provider.getChain("AELF");

const contract = await getContractBasic({
chainProvider,
contractAddress: "contractAddress",
});

// call view
contract.callViewMethod("your method", paramsOption);
// call send
contract.callSendMethod("your method", "your address", paramsOption);

Initiate a transaction and send one ELF to the target account

request

const res = await contract.callSendMethod("Transfer", "your address", {
symbol: "ELF",
to: "to address",
amount: "100000000",
});

Example response

{
data: {
transactionId: "transactionId";
}
}

You can request the result with transactionId in browser

Supplemental Code

import { IAElfRPCMethods } from '@portkey/types';
type ChainId = 'AELF' | 'tDVV' | 'tDVW';
type ChainType = 'ethereum' | 'aelf';

interface BaseContractOptions {
chainId?: ChainId;
contractAddress: string;
type: ChainType;
rpcUrl: string;
}

interface ContractProps extends BaseContractOptions {
aelfContract?: any;
aelfInstance?: { chain: IAElfRPCMethods };
}

interface ErrorMsg {
name?: string;
code?: number;
message?: string;
}

interface ViewResult<T = any> {
data?: T;
error?: ErrorMsg;
}

type SendOptions = {
from?: string;
gasPrice?: string;
gas?: number;
value?: number | string;
nonce?: number;
refBlockNumberStrategy?: number;
onMethod: 'transactionHash' | 'receipt' | 'confirmation';
};

interface SendResult<T = any> extends ViewResult<T> {
transactionId?: string;
}

callSendMethod<T = any>(
functionName: string,
account: string,
paramsOption?: any,
sendOptions?: SendOptions,
): Promise<SendResult<T>>;

Obtain the balance of the account

request

const tokenContract = await getContractBasic({
contractAddress: "tokenContractAddress",
account: aelf.getWallet(privateKey),
rpcUrl: "rpcUrl",
});

const SendResult = await tokenContract.callViewMethod("GetBalance", {
owner: "fromAccount.address",
symbol: "ELF",
});

type ChainType = "ethereum" | "aelf";

Example response

{
data: {
balance: 1000000000,
symbol: 'ELF',
}
}

You can request the result with transactionId in browser

Supplemental Code

import { IAElfRPCMethods } from '@portkey/types';
type ChainId = 'AELF' | 'tDVV' | 'tDVW';
type ChainType = 'ethereum' | 'aelf';

interface BaseContractOptions {
chainId?: ChainId;
contractAddress: string;
type: ChainType;
rpcUrl: string;
}

interface ContractProps extends BaseContractOptions {
aelfContract?: any;
aelfInstance?: { chain: IAElfRPCMethods };
}

interface ErrorMsg {
name?: string;
code?: number;
message?: string;
}

interface ViewResult<T = any> {
data?: T;
error?: ErrorMsg;
}

type CallOptions = {
defaultBlock: number | string;
options?: any;
callback?: any;
};

callViewMethod<T = any>(functionName: string, paramsOption?: any, callOptions?: CallOptions): Promise<ViewResult<T>>;