Skip to main content

API

These are the APIs that allow you to perform operations on the blockchain.

Blockchain Utilities

It contains methods for sending commands to the blockchain and the following functions:

Sending transactions

The sendTransaction method returns a PromiseEither<BlockchainError, UUID>. This method sends a transaction to the blockchain. It receives a payload of type SendTransactionParams and a caller.

const blkUtils = sdk.transactions.blkUtilsApi;

const caller = sdk.transactions.callers.getCaller();

const approveMethod = new ContractFunction('approve', AssetType.NFT);
approveMethod.addInput('to', '0x97C091cBc01feC79742b7d6657e82748e6039a15');
approveMethod.addInput('tokenId', 1);

const payload:SendTransactionParams = {
address: new EthereumAddress('0x5CDDc72bC879f311aa32cfE6B0C479ab9b5b4F23'),
chainId: new ChainID(80001),
method: approveMethod
}

const result = await blkUtils.sendTransaction(payload, caller);
Caller

The caller is an Ethereum address that is used to sign the transaction. Y represents the user who performs the transaction.

Sending queries

The query<T> method returns a PromiseEither<BlockchainError, T>. Allows you to query the blockchain. Receives a command of type QueryCommand<T>

const blkUtils = sdk.transactions.blkUtilsApi;

const cmd = new ReadQuery<string>(
newChainID(80001),
new EthereumAddress('0x5CDDc72bC879f311aa32cfE6B0C479ab9b5b4F23'),
new ContractFunction('owner', AssetType.NFT)
)

const result = await blkUtils.query(cmd)

Watch transaction sagas

The observeSaga method allows us to send the ID of a transaction and get the events that are related to that transaction. Returns an Observable<CommandEvent>.

const transaction = await blkUtils.sendTransaction(payload, caller);

transaction.fold(
(err) => console.error(err.message),
(id) =>
blkUtils.observeSaga(id).subscribe((event) => {
const { name, status } = event.payload;
console.log(`Method ${name}, Status ${status}`);
})
);

API for ERC20

Transfer ERC20 tokens

The transfer method returns a PromiseEither<BlockchainError, UUID> and receives a payload of type TransferErc20Payload and a caller..

const api = sdk.transactions.erc20Api;

const caller = sdk.transactions.callers.getCaller();

const payload: TransferErc20Payload = {
amount: new Amount("100"),
asset: Token.fromPrimitive({
type: AssetType.ERC20,
address: "0xd87ba7a50b2e7e660f678a895e4b72e7cb4ccd9c",
amount: "46.49995",
assetId: "0",
chainId: 80001,
metadata: new Metadata({}).toPrimitive(),
}),
to: new Owner("0x97C091cBc01feC79742b7d6657e82748e6039a15"),
};

const transaction = await api.transfer(payload, caller);

API for ERC721

Mine an NFT

The mint method returns a PromiseEither<BlockchainError, UUID> and receives a MintableAsset, the new owner and the caller.

const api = sdk.transactions.erc721Api;

const caller = sdk.transactions.callers.getCaller();

const file = new File([], "image.jpg"); // input file
const assetData = {
type: AssetType.NFT,
address: "0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB", // Contract address
chainId: 80001,
metadata: {
name: "My NFT",
description: "Description of my nft",
attributes: [
{
trait_type: "Color",
value: "green",
},
{
trait_type: "Year",
value: "1952",
},
], // An array with the asset's attributes
},
};

const mintableAsset = sdk.utils.mintableAssetFromPrimitive(file, assetData);
await mintableAsset.prepare();

const owner = new Owner("0x97C091cBc01feC79742b7d6657e82748e6039a15");

const transaction = await api.mint(mintableAsset, owner, caller);

Transfer an NFT

The transfer method returns a PromiseEither<BlockchainError, UUID> and receives an Asset, the new owner and the caller.

const api = sdk.transactions.erc721Api;

const caller = sdk.transactions.callers.getCaller();

const asset = AssetsFactory.fromPrimitive({
type: AssetType.NFT,
address: "0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB", // Contract address
chainId: 80001,
assetId: '1',
metadata: {},
});

const owner = new Owner("0x97C091cBc01feC79742b7d6657e82748e6039a15");

const transaction = await api.transfer(asset, owner, caller);

Delete an NFT

The burn method returns a PromiseEither<BlockchainError, UUID> and receives an Asset and the caller.

const api = sdk.transactions.erc721Api;

const caller = sdk.transactions.callers.getCaller();

const asset = AssetsFactory.fromPrimitive({
type: AssetType.NFT,
address: "0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB", // Contract address
chainId: 80001,
assetId: '1',
metadata: {},
});

const transaction = await api.burn(asset, caller);

API for ERC1155

Mine an erc1155

The mint method returns a PromiseEither<BlockchainError, UUID> and receive a MintableAsset, the new owner and the caller.

const api = sdk.transactions.erc1155Api;

const caller = sdk.transactions.callers.getCaller();

const data = {
type: AssetType.ERC1155,
address: "0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB", // Contract address
chainId: 80001,
amount: '10',
assetId: "1",
metadata: {
name: "My token erc1155",
description: "Description of my erc1155",
attributes: [], // An array of the asset's attributes
symbol: 'MTO',
decimal: 5
},
};

const file = new File([], "image.jp"); // input file

const asset = sdk.utils.mintableERC1155FromPrimitive(data, file);

const owner = new Owner("0x97C091cBc01feC79742b7d6657e82748e6039a15");

const transaction = await api.mint(asset, owner, caller);

Transfer an erc1155

The transfer method returns a PromiseEither<BlockchainError, UUID> and receives a payload that is of type TransferERC1155Payload and the caller.

const api = sdk.transactions.erc1155Api;

const caller = sdk.transactions.callers.getCaller();

const data = AssetsFactory.fromPrimitive({
type: AssetType.ERC1155,
address: "0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB", // Contract address
chainId: 80001,
amount: '10',
assetId: "1",
metadata: {},
});

const payload: TransferERC1155Payload = {
amount: new Amount('1'),
asset: data,
assetId: newID('2'),
to: new Owner("0x97C091cBc01feC79742b7d6657e82748e6039a15")
}

const transaction = await api.transfer(payload, caller);