use.miden::account # ERRORS # ================================================================================================= # Failed to build the fungible asset because the provided faucet id is not from a fungible faucet const.ERR_FUNGIBLE_ASSET_PROVIDED_FAUCET_ID_IS_INVALID=0x0002004B # Fungible asset build operation called with amount that exceeds the maximum allowed asset amount const.ERR_FUNGIBLE_ASSET_AMOUNT_EXCEEDS_MAX_ALLOWED_AMOUNT=0x0002004C # Failed to build the non-fungible asset because the provided faucet id is not from a non-fungible faucet const.ERR_NON_FUNGIBLE_ASSET_PROVIDED_FAUCET_ID_IS_INVALID=0x0002004D # CONSTANTS # ================================================================================================= # Two raised to the power of 32 (2^32) const.TWO_POW_32=4294967296 # The bit 29 of the most significant half of the element is used to identify the asset type const.FUNGIBLE_BITMASK_U32=0x20000000 # PROCEDURES # ================================================================================================= #! Builds a fungible asset for the specified fungible faucet and amount. #! #! Stack: [faucet_id, amount] #! Output: [ASSET] #! #! - faucet_id is the faucet to create the asset for. #! - amount is the amount of the asset to create. #! - ASSET is the built fungible asset. export.build_fungible_asset # assert the faucet is a fungible faucet dup exec.account::is_fungible_faucet assert.err=ERR_FUNGIBLE_ASSET_PROVIDED_FAUCET_ID_IS_INVALID # => [faucet_id, amount] # assert the amount is valid dup.1 exec.get_fungible_asset_max_amount lte assert.err=ERR_FUNGIBLE_ASSET_AMOUNT_EXCEEDS_MAX_ALLOWED_AMOUNT # => [faucet_id, amount] # create the asset push.0.0 movup.2 # => [ASSET] end #! Creates a fungible asset for the faucet the transaction is being executed against. #! #! Stack: [amount] #! Output: [ASSET] #! #! - amount is the amount of the asset to create. #! - ASSET is the created fungible asset. export.create_fungible_asset # fetch the id of the faucet the transaction is being executed against. exec.account::get_id # => [id, amount] # build the fungible asset exec.build_fungible_asset # => [ASSET] end #! Builds a non fungible asset for the specified non-fungible faucet and amount. #! #! Stack: [faucet_id, DATA_HASH] #! Output: [ASSET] #! #! - faucet_id is the faucet to create the asset for. #! - DATA_HASH is the data hash of the non-fungible asset to build. #! - ASSET is the built non-fungible asset. export.build_non_fungible_asset # assert the faucet is a non-fungible faucet dup exec.account::is_non_fungible_faucet assert.err=ERR_NON_FUNGIBLE_ASSET_PROVIDED_FAUCET_ID_IS_INVALID # => [faucet_id, DATA_HASH] # build the asset movup.3 drop movdn.2 # => [hash_0, hash_1, faucet_id, hash_3] # Force the non-fungible bit to 0 u32split dup push.FUNGIBLE_BITMASK_U32 u32and u32xor push.TWO_POW_32 mul add # => [ASSET] end #! Creates a non-fungible asset for the faucet the transaction is being executed against. #! #! Stack: [DATA_HASH] #! Output: [ASSET] #! #! - DATA_HASH is the data hash of the non-fungible asset to create. #! - ASSET is the created non-fungible asset. export.create_non_fungible_asset # get the id of the faucet the transaction is being executed against exec.account::get_id # => [id, DATA_HASH] # build the non-fungible asset exec.build_non_fungible_asset # => [ASSET] end # PROCEDURES COPIED FROM KERNEL (TODO: get rid of this duplication) # ================================================================================================= const.FUNGIBLE_ASSET_MAX_AMOUNT=9223372036854775807 #! Returns the maximum amount of a fungible asset. #! #! Stack: [] #! Outputs: [fungible_asset_max_amount] #! #! fungible_asset_max_amount is the maximum amount of a fungible asset. export.get_fungible_asset_max_amount push.FUNGIBLE_ASSET_MAX_AMOUNT # => [fungible_asset_max_amount] end