Skip to main content
Last updated

Ledger contract

The Marmalade token standard provides interfaces that enable you to define, mint, and secure tokens. As discussed in Contract architecture, the Marmalade ledger contract provides the core functionality to create, manage, and transfer tokens minted using the Marmalade token standard. The ledger contract records and manages all token-related activity to ensure every transaction is accurate, every policy is enforced, and every account is up-to-date.

This part of the documentation describes the functions and capabilities defined in the Marmalade ledger contract.

Source code: ledger.pact

create-token-id

Use create-token-id to generate a unique token identifier with the specified token identifier.

Arguments

Use the following arguments to create the token identifier.

ArgumentTypeDescription
detailsobjectDefines token properties using the metadata schema in JSON file format.
creation-guard guardSpecifies the temporary guard—for example, a keyset—used to generate the token identifier. This guard isn't stored and ensure that only the owner of the creation key can create a specific token identifier.

Before creating a token, you must choose a temporary guard. The guard can be

  • A keyset you've already defined or used in the guard-policy.
  • A single-use keyset that won't be used again.
  • Another type guard.

This guard becomes part of the hashed data in the token-id string prefixed with t:. Including the guard in the hashed token identifier protects anyone else from creating the token. With this mechanism, only you—as the owner of the creation key—can create the token specified by the token-id string.

Example

Generate a unique token-id by calling the following function:

(ledger.create-token-id details creation-guard)

create-token

Use create-token to create a token with the specified token identifier.

Arguments

Use the following arguments to create a token.

ArgumentTypeDescription
idstringSpecifies the unique token identifier generated using the create-token-id function and formatted as t:{token-detail-hash}.
precisionintegerSpecifies the number of decimals allowed for the token supply amount. For non-fungible tokens, the precision must be 0, and should be enforced in the policy's enforce-init.
uristringSpecifies the uniform resource identifier (uri) to an external JSON file containing token metadata.
policieslistSpecifies one or more policy contracts with custom functions to execute at marmalade functions
creation-guardguardSpecifies the temporary guard—for example, a keyset—used to generate the token identifier. This guard isn't stored and ensure that only the owner of the creation key can create a specific token identifier.

To create a token with this function:

  • Generate a unique token-id by calling (ledger.create-token-id details creation-guard)

  • Create the token by calling (ledger.create-token ... creation-guard)

    The create-token transaction must include the TOKEN capability signed with the keyset creation-guard that you used to generate the token identifier.

mint

Use mint to mint the specified token amount to the specified account.

Arguments

Use the following arguments to mint a token.

ArgumentTypeDescription
idstringSpecifies the unique token identifier generated using create-token-id function and formatted as t:{token-detail-hash}.
accountstringSpecifies the account that will receive the minted token.
guardguardSpecifies the guard for the minted token account.
amountdecimalSpecifies the number of tokens to be minted.

When you submit the mint transaction, the policy-manager.enforce-mint function calls the policy:enforce-mint function in the stored token policies and the function is executed at ledger.mint.

burn

Use burn to destroy the specified token amount from the specified account.

Arguments

Use the following arguments to burn a token.

ArgumentTypeDescription
idstringSpecifies the unique token identifier generated using create-token-id function and formatted as t:{token-detail-hash}.
accountstringSpecifies the account where the token will be destroyed.
amountdecimalSpecifies the number of tokens to be burned.

When you submit the burn transaction, the policy-manager.enforce-burn function calls the policy:enforce-burn function in the stored token policies and the function is executed at ledger.burn.

transfer

Use transfer to transfer the specified token amount from the specified sender to the specified receiver.

Arguments

Use the following arguments to transfer a token.

ArgumentTypeDescription
idstringSpecifies the unique token identifier generated using create-token-id function and formatted as t:{token-detail-hash}.
senderstringSpecifies the sender account that the token will be transferred from.
receiverstringSpecifies the receiver account that the token will be transferred to.
amountdecimalSpecifies the number of tokens to be transferred.

When you submit the transfer transaction, the policy-manager.enforce-transfer function calls the policy:enforce-transfer function in the stored token policies and the function is executed at ledger.transfer.

sale

Use sale to execute a two-step transaction using the offer and buy steps with an escrow account as described in Token sales and trustless-escrow.

Because a sale requires two steps that must be completed in a specific order, the transaction is defined using a pact. For information about the syntax used to define a pact, see defpact.

Arguments

Use the following arguments to initiate the sale of a token.

ArgumentTypeDescription
idstringSpecifies the unique token identifier generated using create-token-id function and formatted as t:{token-detail-hash}.
sellerstringSpecifies the seller account that the token will be offered from.
amountdecimalSpecifies the number of tokens to be offered for sale.
timeoutintegerSpecifies when the offer is set to expire in the number of blocks that must be mined before the offer can be withdrawn. The timeout argument is optional. If not specified, an offer can be withdrawn at any time by the token owner.

offer

The first step of a sale pact (step 0) executes the offer function. The offer function transfers the token from the seller's account to the escrow account.

The policy-manager.enforce-offer function calls the policy:enforce-offer function in the stored token policies and the function is executed at step 0 of the sale.

withdraw (cont)

The sale pact includes a rollback step (step 0-rollback). The rollback step executes the withdraw function. The withdraw function transfers the tokens held in the escrow account for the sale back to the seller. If a timeout is specified, the withdraw function can only be executed after the timeout period has passed. You can execute the withdraw function by sending a continuation (cont) command with the following information:

yaml
pactTxHash: sale-pact-idstep: 0rollback: true
yaml
pactTxHash: sale-pact-idstep: 0rollback: true

For more information about formatting continuation commands, see YAML Continuation command request

The policy-manager.enforce-withdraw function calls the policy:enforce-withdraw function in the stored token policies and the function is executed at step 0-rollback of sale.

buy (cont)

The second step of a sale pact (step 1) executes the buy function. The buy function transfers the tokens held in the escrow account to the buyer. The buy function can be executed before timeout. The buyer and buyer-guard information is read from the env-data of the command instead of passing in arguments. Like the withdraw function, the buy function is executed using a continuation (cont) command:

yaml
pactTxHash: sale-pact-idstep: 0rollback: false
yaml
pactTxHash: sale-pact-idstep: 0rollback: false

The policy-manager.enforce-buy function calls the policy:enforce-buy function in the stored token policies and the function is executed at step 1 of sale.