CreateAssetTx
is used to create a new asset on the chain. An asset can contain whatever data you put inside the data
field: merchandise, goods, certificate, string...
After CreateAssetTx
is executed, an asset is created with a unique addree and the owner is the account that sent the CreateAssetTx
.
The asset could be transferred/exchanged to/with others. The use cases for asset are pretty rich, e.g.:
Sample Code
message CreateAssetTx {
string moniker = 1;
google.protobuf.Any data = 2;
bool readonly = 3;
bool transferrable = 4;
uint32 ttl = 5;
string parent = 6;
string address = 7;
}
Name | Data Type | Description | Required |
---|---|---|---|
moniker | string | The nick name for the asset | |
readonly | bool | Whether or not the asset is modifiable. By default it's False : an asset could be modified later by using update_asset, | |
transferrable | bool | whether or not the asset can be transferred to others. By default it is True : the asset can be transferred. | |
ttl | int | Time To Live(ttl) means how long the asset is availiable after first consumption. By default it is 0 , which is unlimited usage. This is used when an asset is something like a park ticket, it could be valid for 24 hours after first use, or if it is an annual park permit, it could be valid for 365 days. | |
parent | string | Parent asset for this asset. For example, an event is an asset, and all the tickets generated by this event will set its parent to the event address. | |
address | string | pre-computed asset address. This field is required, however Forge SDK will help to calculate the address. | Yes |
data (optional) | Google.Protobuf.Any | Custom user data | Yes |
The most important field for an asset is its data - it could be anything that encoded into a google.protobuf.Any
. Application can define and explain what's inside data, for example, if the asset is a movie ticket, application could define a Ticket message:
message Ticket {
string row = 1;
string seat = 2;
string room = 3;
string time = 4;
string name = 5;
}
Sample Usage
> wallet = ForgeSdk.create_wallet()
> ForgeSdk.declare(ForgeAbi.DeclareTx.new(moniker: "sisyphus"), wallet: wallet)
> ticket = "ticket_12345"
> itx = ForgeAbi.CreateAssetTx.new(data: Google.Protobuf.Any.new(type_url: "fg:x:string", data: ticket), readonly: true, transferrable: true, ttl: 7200)
> ForgeSdk.create_asset(itx, wallet: wallet)
Calculating asset address
If unfortunately your language has no SDK support, you need to calculate the address yourself. Below is the algorithm and elixir code to build asset address:
- encode filled the CreateAssetTx itx to binary
- do sha3 to it
- use hashtodid / pkhashtodid to generate the did (type shall be asset)
hash = Mcrypto.hash(%Mcrypto.Hasher.Sha3{}, ForgeAbi.CreateAssetTx.encode(itx))
AbtDid.hash_to_did(:asset, hash, form: :short)