Utilities
The utilities module allows obtaining information on Alfa's services as well as providing utility functions for application development.
Methods
Create a mineable asset
The createMintableAsset
method allows you to create a MintableAsset
from a File
.
const utils = sdk.utils;
const file = new File([], "hello.jpg");
const mintableAsset = utils.createMintableAsset(file);
The createMintableAsset
method creates an asset with no data, you must fill in the asset information before mining it. If you want to create an asset with data you can use the mintableAssetFromPrimitive
method.
Create a minable asset from a Primitives<Asset>
The mintableAssetFromPrimitive
method allows you to create a MintableAsset
from a Primitives<Asset>
.
const utils = sdk.utils;
const file = new File([], "hello.jpg");
const data: Primitives<Asset> = {
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",
},
]
},
};
const mintableAsset = utils.mintableAssetFromPrimitive(file, data);
Create a minable asset from a Primitives<ERC1155>
The mintableERC1155FromPrimitive
method allows you to create a MintableAsset
from a Primitives<ERC1155>
.
const utils = sdk.utils;
const file = new File([], "hello.jpg");
const data: Primitives<Asset> = {
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",
},
]
},
};
const mintableAsset = utils.mintableERC1155FromPrimitive(data, file);
The file
parameter is optional, if not passed the asset will be created without an image
file.
Debug a transaction
The debug
method allows you to obtain the status of a transaction on the blockchain.
const utils = sdk.utils;
const hash = '0x82d735b0afae88084f2e8c7f52d24c11206446dd4193270b377c446bfbaf10a8'
const result = await utils.debug(hash)
Get block explorer url
The getExplorerUri
method allows you to get the url of a block explorer for an asset.
const utils = sdk.utils;
const explorerUrl = utils.getExplorerUri(asset) // `asset` is of type `Asset`
Get coins by symbol
The getCoinsBySymbol
method allows you to get a list of coins based on their symbol.
const utils = sdk.utils;
const result = await utils.getCoinsBySymbol(["eth", "matic", "usdc"]);
Get currency metadata
The getCoinsMetadata
method allows you to get the metadata of a list of coins.
const utils = sdk.utils;
const start = 1;
const limit = 10;
const search = "usd";
const { total, data } = await utils.getCoinsMetadata(start, limit, search);
The search
parameter is optional, if not passed all coins will be returned.
Update coin metadata
The updateCoin
method allows updating the information of a coin.
const utils = sdk.utils;
const coin = new Metadata({
id: "venus-usdc",
name: "Venus USDC",
image: "https://assets.coingecko.com/coins/images/13906/large/usdc.png?1612799563",
description: "",
attributes: [
{
trait_type: "binance-smart-chain",
value: "0xeca88125a5adbe82614ffc12d0db554e2e2867c8",
},
],
symbol: "vusdc",
decimal: "6",
});
const newCoin = await utils.updateCoin(coin);
A coin is of type Metadata
. It represents the information of a coin in the blockchain.
You can see the documentation for the Metadata
class here.
Create a new currency
The newCoin
method allows you to create a new coin.
const utils = sdk.utils;
const coin = {
id: "new-coin",
name: "My Coin",
image: "https://myserver.com/images/mycoin.jpg",
description: "My coin description",
attributes: [],
symbol: "mci",
decimal: "6",
};
const newCoin = await utils.newCoin(coin);
Delete a coin
The deleteCoin
method allows you to delete a coin.
const utils = sdk.utils;
const coinId = 'new-coin';
await utils. deleteCoin(coinId);
Read a contract variable
The readContract
method allows you to read a variable from a contract.
const utils = sdk.utils;
const method = new ContractFunction("uri", AssetType.ERC1155);
method.addInput("tokenId", 1);
const tokenUri: string = await utils.readContract({
address: "0x7C6ac312c08b975a0e74Be8Cd8C6ad8b8b4A8DEB",
chainId: 80001,
method: method.toPrimitive(),
});
Create a random wallet
The getRandomWallet
method allows you to get a random wallet.
const utils = sdk.utils;
const walletDto = await utils.getRandomWallet();
The response from the getRandomWallet
method is of type WalletDto
.
Which has this interface:
interface WalletDto {
readonly address: EthereumAddress;
readonly mnemonic: MnemonicValue;
toWallet(): Wallet;
toPrimitive(): Primitives<WalletDto>;
}
address
is the new address of the wallet.
mnemonic
is the mnemonic phrase of the wallet.
Get information from a string
The getChainInfo
method allows you to get information from a chain.
const utils = sdk.utils;
const chainId = new ChainID(80001);
const data = utils.getChainInfo(chainId);
The result returned by the getChainInfo
method is of type Chain
.
export interface Chain {
name: string;
title?: string;
chain: string;
icon?: string;
rpc: string[];
faucets: any[];
nativeCurrency: NativeCurrency;
infoURL: string;
shortName: string;
chainId: number;
networkId: number;
slip44?: number;
ens?: ens;
explorers?: Explorer[];
parent?: {
type?: string;
chain?: string;
bridges?: {
url:string;
}[];
};
status?: string;
redFlags?: string[];
}
export interface ens {
registry: string;
}
export interface Explorer {
name: string;
url:string;
icon?: string;
standard: string;
}
export interface NativeCurrency {
name: string;
symbol: string;
decimals: number;
}