Asset
An asset is an object that represents a resource on the blockchain. Assets can be used to represent tokens, nfts, etc.
The Asset
class is an abstract class that cannot be instantiated directly. To create an asset you must use one of the classes that extend Asset
.
The type of the asset is defined in the type
property of the asset's primitive object. The types of assets available are:
AssetType.NFT
AssetType.ERC20
AssetType.ERC1155
Properties
Property | Type | Description |
---|---|---|
assetId | ID | Unique identifier of the asset. |
address | EthereumAddress | Asset contract address. |
amount | Amount | Amount of the asset. |
chainId | ChainId | Identifier of the blockchain in which the asset is located. |
metadata | Metadata | Asset metadata. |
Methods
Method | Return | Description |
---|---|---|
setAssetId(assetId: ID) | void | Sets the asset identifier. |
setAmount(amount: Amount) | void | Sets the amount of the asset. |
getTransactionHash() | TransactionHash | Gets the hash of the transaction that created the asset. |
setTransactionHash(transactionHash: TransactionHash) | void | Sets the hash of the transaction that created the asset. |
getOwner() | Owner | Gets the address of the owner of the asset. |
setOwner(owner: Owner) | void | Sets the address of the owner of the asset. |
equals(other: Asset) | boolean | Compare if the asset is equal to another asset. |
toPrimitive() | Primitives<Asset> | Gets the primitive representation of the asset. |
isSameAddressAccount(address: EthereumAddress) | boolean | Check if the account address is the same as the asset address. |
isOwned() | boolean | Check if the asset has an owner. |
isOwnedBy(other: Owner) | boolean | Compare if the asset is owned by the address of the account. |
formatAmount(fixed:number) | Amount | Format the amount of the asset. |
isNFT() | boolean | Check if the asset is an NFT. |
isERC20() | boolean | Check if the asset is an ERC20. |
isERC1155() | boolean | Check if the asset is an ERC1155. |
decimals() | Decimal | Gets the number of decimal places in the asset. |
Related classes
The NFT
class, Token
and TokenNft
extend from the Asset
class. And they represent the different types of assets that can be created.
The mineable assets are shown in the diagram. They include a prepare
method that allows preparing the asset to be mined.
Asset Factory
The AssetsFactory
class allows you to create assets. It will create them depending on the type of asset being sent.
import { AssetsFactory, Asset, Primitives } from '@alfabc/sdk';
const primitiveAsset: 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'
}
], // An array with the asset's attributes
}
}
const asset = AssetsFactory.from(primitiveAsset); // Create an NFT asset
Related valuables
Valuable object | Description |
---|---|
Owner | Represents the address of an account and the owner of an asset. Extends from EthereumAddress |