pub struct AssetId {
bits: b256,
}
Expand description
An AssetId is used for interacting with an asset on the network.
Additional Information
It is calculated by taking the sha256 hash of the originating ContractId and a SubId.
i.e. sha256((contract_id, sub_id)).
An exception is the Base Asset.
The SubId is used to differentiate between different assets that are created by the same contract.
Fields
bits: b256
Implementations
fn new(contract_id: ContractId, sub_id: SubId) -> Self
fn new(contract_id: ContractId, sub_id: SubId) -> Self
Creates a new AssetId from a ContractId and SubId.
Arguments
contract_id
: [ContractId] - The ContractId of the contract that created the asset.sub_id
: [SubId] - The SubId of the asset.
Returns
- [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and SubId.
Examples
use std::callframes::contract_id;
fn foo() {
let contract_id = contract_id();
let sub_id = b256::zero();
let asset_id = AssetId::new(contract_id, sub_id);
}
fn default() -> Self
fn default() -> Self
Creates a new AssetId with the default SubId for the current contract.
Additional Information
WARNING If called in an external context, this will not return a correct AssetId.
If called externally, will actually use the Transaction Id as a the ContractId.
Returns
- [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and the default SubId.
Examples
use std::{callframes::contract_id, constants::DEFAULT_SUB_ID};
fn foo() {
let asset_id = AssetId::default();
assert(asset_id == AssetId::new(contract_id(), DEFAULT_SUB_ID));
}
fn base() -> Self
fn base() -> Self
The base asset of a chain.
Additional Information
On the Fuel network, the base asset is Ether.
Returns
- [AssetId] - The AssetId of the base asset.
Examples
use std::asset::transfer;
fn foo() {
let asset_id = AssetId::base();
let amount = 100;
let recipient = Identity::ContractId(ContractId::zero());
transfer(recipient, asset_id, amount);
fn bits(self) -> b256
fn bits(self) -> b256
Returns the underlying raw b256
data of the asset id.
Returns
- [b256] - The raw data of the asset id.
Examples
fn foo() -> {
let my_asset = AssetId::from(b256::zero());
assert(my_asset.bits() == b256::zero());
}
fn zero() -> Self
fn zero() -> Self
Returns the zero value for the AssetId
type.
Returns
- [AssetId] -> The zero value for the
AssetId
type.
Examples
fn foo() {
let zero_asset_id = AssetId::zero();
assert(zero_asset_id == AssetId::from(b256::zero()));
}
fn is_zero(self) -> bool
fn is_zero(self) -> bool
Returns whether an AssetId
is set to zero.
Returns
- [bool] -> True if the
AssetId
is zero, otherwise false.
Examples
fn foo() {
let zero_asset_id = AssetId::zero();
assert(zero_asset_id.is_zero());
}
Trait Implementations
impl AbiEncode for AssetId
impl AbiEncode for AssetId
fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for AssetId
impl AbiDecode for AssetId
fn abi_decode(refmut buffer: BufferReader) -> Self
impl Eq for AssetId
impl Eq for AssetId
fn eq(self, other: Self) -> bool
fn neq(self, other: Self) -> bool
fn neq(self, other: Self) -> bool
Evaluates if two values of the same type are not equal.
Additional Information
This function is inherited when eq()
is implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
if the two values are not equal, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 2 };
let result = struct1 != struct2;
assert(result);
}