Skip to main content
Last updated

REPL-only functions

begin-tx

Use begin-tx to begin a new transaction with an optional name. This function is used to create tests that you want to execute using the Pact REPL. In most cases, you include this function in .repl files that test specific application features and failure scenarios.

Within the context of a transaction, you can set environment data, load modules, and execute functions. To complete the transaction, use the commit-tx function. The commit-tx function signals the end of a transaction block and can be followed by additional begin-tx and commit-tx blocks.

Basic syntax

To begin a transaction without a name, use the following syntax:

pact
(begin-tx)
pact
(begin-tx)

To begin a transaction with a specific name, use the following syntax:

pact
(begin-tx name)
pact
(begin-tx name)

Arguments

Use the following argument to specify an optional name for the transaction when using the begin-tx Pact function.

ArgumentTypeDescription
namestringSpecifies the name of the transaction (optional).

Return value

The begin-tx function returns a string indicating the transaction identifier and the optional name (if provided).

Examples

The following example demonstrates how to use the begin-tx function to begin a new transaction without a name:

pact
pact> (begin-tx)"Begin Tx 0"
pact
pact> (begin-tx)"Begin Tx 0"

The following example demonstrates how to use the begin-tx function to begin a new transaction with a specific name:

pact
pact> (begin-tx "load module")"Begin Tx 0: load module"
pact
pact> (begin-tx "load module")"Begin Tx 0: load module"

You can write an empty transaction as a placeholder for the logic to test specific function or failure scenarios. For example, to create a placeholder for the logic that defines a namespace:

pact
(begin-tx "Define a namespace called 'election") (commit-tx)
pact
(begin-tx "Define a namespace called 'election") (commit-tx)

commit-tx

Use commit-tx to commit the current transaction. In most cases, you include this function in .repl files that test specific application features and failure scenarios.

Within the context of a transaction started using the begin-tx function, you can set environment data, load modules, and execute functions. You use the commit-tx function to signal the end of a transaction block and can be followed by additional begin-tx and commit-tx blocks.

Basic syntax

To commit a transaction, use the following syntax:

pact
(commit-tx)
pact
(commit-tx)

Arguments

The commit-tx function does not take any arguments.

Return value

The commit-tx function returns a string indicating the transaction identifier that has been committed.

Examples

The following example demonstrates how to use the commit-tx function within a Pact REPL:

pact
pact> (begin-tx "load module")"Begin Tx 0: load module"pact> (commit-tx)"Commit Tx 0: load module"
pact
pact> (begin-tx "load module")"Begin Tx 0: load module"pact> (commit-tx)"Commit Tx 0: load module"

In this example, a new transaction—Tx0—is started using (begin-tx), and then (commit-tx) is called to commit the transaction. The function returns a string indicating that "Tx 0" has been committed.

Note that you should only call the commit-tx function after performing the necessary operations within a transaction. The commit-tx function finalizes the transaction, and any changes made during the transaction are persisted.

It's important that you always call the commit-tx function after begin-tx and after any other transaction-related operations to properly commit the transaction. If commit-tx is not called, the transaction remains open and may lead to unexpected behavior or inconsistencies in the system.

continue-pact

Use continue-pact to continue a previously-initiated multi-step transaction defined using a defpact declaration. You must specify the step at which to continue. You can also specify optional parameters for rollback, pact ID, and yielded value.

Basic syntax

To continue a pact with the specified step, use the following syntax:

pact
(continue-pact step)
pact
(continue-pact step)

To continue a pact with the specified step and rollback option, use the following syntax:

pact
(continue-pact step rollback)
pact
(continue-pact step rollback)

To continue a pact with the specified step, rollback option, and pact ID, use the following syntax:

pact
(continue-pact step rollback pact-id)
pact
(continue-pact step rollback pact-id)

To continue a pact with the specified step, rollback option, pact ID, and yielded value, use the following syntax:

pact
(continue-pact step rollback pact-id yielded)
pact
(continue-pact step rollback pact-id yielded)

Arguments

Use the following arguments to customize the behavior of the continue-pact Pact function.

ArgumentTypeDescription
stepintegerSpecifies the step of the pact to continue.
rollbackboolSpecifies whether to perform a rollback (optional). The default is false.
pact-idstringSpecifies the identifier of the pact to continue (optional). The default is the pact identifier initiated in the current transaction, if one is present.
yieldedobjectSpecifies the yielded value to be read with the resume function (optional). If not specified, the function uses the yield from the most recent pact execution, if any. The schema of the yielded object is object:<{y}>.

Return value

The continue-pact function returns a string indicating the result of continuing the pact.

Examples

The following example demonstrates how to use the continue-pact function to continue a pact with step 1:

pact
(continue-pact 1)
pact
(continue-pact 1)

The following example demonstrates how to use the continue-pact function to continue a pact with step 1 and perform a rollback:

pact
(continue-pact 1 true)
pact
(continue-pact 1 true)

The following example demonstrates how to use the continue-pact function to continue a pact with step 1, without rollback, and specify the pact ID:

pact
(continue-pact 1 false "[pact-id-hash]")
pact
(continue-pact 1 false "[pact-id-hash]")

The following example demonstrates how to use the continue-pact function to continue a pact with step 2, without rollback, specify the pact ID, and provide a yielded value:

pact
(continue-pact 2 false "[pact-id-hash]" { "rate": 0.9 })
pact
(continue-pact 2 false "[pact-id-hash]" { "rate": 0.9 })

describe-module

Use describe-module to get metadata for a specified module. This function returns an object with fields including name, hash, blessed, code, and keyset.

Basic syntax

To get metadata for a module, use the following syntax:

pact
(describe-module module)
pact
(describe-module module)

Arguments

Use the following argument to specify the module for the describe-module Pact function.

ArgumentTypeDescription
modulestringSpecifies the name of the module to describe.

Return values

The describe-module function returns an object with metadata for the specified module.

Examples

The following example demonstrates how to use describe-module function to describe a loaded module:

pact
pact> (module election-gas-station GOVERNANCE (defcap GOVERNANCE () true))"Loaded module election-gas-station, hash S6Dtzd7TzbRkYV6_5G5JRhjtF9Ztvaw7LuUEHEeRivQ" pact> (describe-module "election-gas-station"){  "blessed": []  ,"code": "(module election-gas-station GOVERNANCE (defcap GOVERNANCE () true))"  ,"hash": "S6Dtzd7TzbRkYV6_5G5JRhjtF9Ztvaw7LuUEHEeRivQ"  ,"interfaces": []  ,"keyset": "Governance {_gGovernance = Right (Def {_dDefName = DefName {_unDefName = "GOVERNANCE"}, _dModule = ModuleName {_mnName = "election-gas-station", _mnNamespace = Nothing}, _dDefType = Defcap, _dFunType = FunType {_ftArgs = [], _ftReturn = TyVar {_tyVar = TypeVar {_tvName = "a", _tvConstraint = []}}}, _dDefBody = Scope (TList {_tList = [TLiteral {_tLiteral = LBool {_lBool = True}, _tInfo = true}], _tListType = TyAny, _tInfo = (defcap GOVERNANCE () true)}), _dMeta = Meta {_mDocs = Nothing, _mModel = []}, _dDefMeta = Nothing, _dInfo = (defcap GOVERNANCE () true)})}" ,"name": "election-gas-station"}
pact
pact> (module election-gas-station GOVERNANCE (defcap GOVERNANCE () true))"Loaded module election-gas-station, hash S6Dtzd7TzbRkYV6_5G5JRhjtF9Ztvaw7LuUEHEeRivQ" pact> (describe-module "election-gas-station"){  "blessed": []  ,"code": "(module election-gas-station GOVERNANCE (defcap GOVERNANCE () true))"  ,"hash": "S6Dtzd7TzbRkYV6_5G5JRhjtF9Ztvaw7LuUEHEeRivQ"  ,"interfaces": []  ,"keyset": "Governance {_gGovernance = Right (Def {_dDefName = DefName {_unDefName = "GOVERNANCE"}, _dModule = ModuleName {_mnName = "election-gas-station", _mnNamespace = Nothing}, _dDefType = Defcap, _dFunType = FunType {_ftArgs = [], _ftReturn = TyVar {_tyVar = TypeVar {_tvName = "a", _tvConstraint = []}}}, _dDefBody = Scope (TList {_tList = [TLiteral {_tLiteral = LBool {_lBool = True}, _tInfo = true}], _tListType = TyAny, _tInfo = (defcap GOVERNANCE () true)}), _dMeta = Meta {_mDocs = Nothing, _mModel = []}, _dDefMeta = Nothing, _dInfo = (defcap GOVERNANCE () true)})}" ,"name": "election-gas-station"}

In this example, (describe-module 'm) is used to get metadata for the module named 'm'. The function returns an object providing detailed information about the module.

env-chain-data

Use env-chain-data to define chain information for transactions in your testing environment. You can this function to create an object with one or more of the following fields:

  • chain-id: The chain identifier (0-19) for the blockchain where the transaction is executed.
  • block-height: The height of the block that includes the transaction.
  • block-time: The timestamp of the block that includes the transaction.
  • prev-block-hash: The hash of the previous block.
  • sender: The sender of the transaction.
  • gas-limit: The gas limit for the transaction.
  • gas-price: The gas price for the transaction.
  • gas-fee: The gas fee for the transaction.

Basic syntax

To set one or more chain data fields, use the following syntax:

pact
(env-chain-data {object})
pact
(env-chain-data {object})

Arguments

Use the following argument to specify the chain data you want to set using the env-chain-data Pact function.

ArgumentTypeDescription
objectobjectSpecifies the chain data you want to be set for the transaction.

Return value

The env-chain-data function returns an object with the chain data you specify.

Examples

The following example demonstrates how to use the env-chain-data function to set a chain identifier and block time for a transaction:

pact
(begin-tx "create-token")  (env-chain-data {"chain-id":"3","block-time": (time "2023-07-20T11:26:35Z")})(commit-tx)
pact
(begin-tx "create-token")  (env-chain-data {"chain-id":"3","block-time": (time "2023-07-20T11:26:35Z")})(commit-tx)

env-data

Use env-data to set transaction data for your testing environment either as an encoded string or as Pact types coerced to JSON format.

Basic syntax

To set transaction data, use the following syntax:

pact
(env-data json)
pact
(env-data json)

Arguments

Use the following argument to specify the JSON data when using the env-data Pact function.

ArgumentTypeDescription
jsonobjectSpecifies the JSON data to be set for the transaction. The data can be provided as an encoded string or as Pact types that will be coerced to JSON.

Return value

The env-data function returns a string indicating that the transaction data is being set.

Examples

The following example demonstrates the usage of the env-data function within a Pact REPL:

pact
pact> (env-data { "keyset": { "keys": ["my-key" "admin-key"], "pred": "keys-any" } })"Setting transaction data"
pact
pact> (env-data { "keyset": { "keys": ["my-key" "admin-key"], "pred": "keys-any" } })"Setting transaction data"

In the following example, the env-data function is used to set up a mock token identifier and account information for testing Marmalade functions:

pact
(env-data {  "token-id": "t:YV6-cQBhE_EoIXAuNV08aGXLfcucBEGy0Gb1Pj6w_Oo"  ,"account": "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3"  ,"account-guard": {      "keys": ["e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3"],       "pred": "keys-all"    }  })
pact
(env-data {  "token-id": "t:YV6-cQBhE_EoIXAuNV08aGXLfcucBEGy0Gb1Pj6w_Oo"  ,"account": "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3"  ,"account-guard": {      "keys": ["e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3"],       "pred": "keys-all"    }  })

env-enable-repl-natives

Use env-enable-repl-natives to control whether REPL native functions are allowed in module code.

If you set this function to true to enable REPL native functions in module code, other environment configuration functions like env-sigs are allowed in module code, and you can use these functions in your module definitions.

If you set this function to false to disable REPL native functions in module code, other environment configuration functions are not allowed in module code, and attempting to use them will result in an error.

Basic syntax

To allow REPL native functions to be used in module code, use the following syntax:

pact
(env-enable-repl-natives enable)
pact
(env-enable-repl-natives enable)

Arguments

Use the following argument to specify whether to enable or disable REPL native functions in module code.

ArgumentTypeDescription
enableboolSpecifies whether to enable or disable REPL native functions in module code. Note that this argument is required. Set it to true to enable REPL native function calls. Set it to false to prevent REPL native functions from being called.

Return value

The env-enable-repl-natives function returns a string indicating the status of REPL natives.

Examples

The following example demonstrates how to enable REPL native functions:

pact
(env-enable-repl-natives true)"Repl natives enabled"
pact
(env-enable-repl-natives true)"Repl natives enabled"

After enabling REPL natives, you can use environment configuration functions like env-sigs in your module code.

It's important to note that you should only enable REPL native functions in module code—that is, executable .pact files—if absolutely necessary. Most REPL native functions are intended to be used exclusively in the REPL environment. If you enable access to the REPL native functions in module code, ensure that access to the functions is properly controlled and validated.

The following example demonstrates how to disable access to REPL native functions:

pact
(env-enable-repl-natives false)"Repl natives disabled"
pact
(env-enable-repl-natives false)"Repl natives disabled"

env-events

Use env-events to retrieve any accumulated events and optionally clear the event state.

Basic syntax

To retrieve events and clear the event state, use the following syntax:

pact
(env-events clear)
pact
(env-events clear)

Arguments

Use the following argument to specify whether to clear the event state after retrieving the events.

ArgumentTypeDescription
clearboolSpecifies whether to clear the event state after retrieving the events. Note that this argument is required. Set it to true to retrieve events, then clear the event state. Set it to false retrieve events and keep the event state.

Return value

The env-events function returns an array of objects representing the accumulated events. Each object in the array has the following fields:

  • name: The fully-qualified name of the event.
  • params: The parameters associated with the event.
  • module-hash: The hash of the module that emitted the event.

Examples

The following example demonstrates how to retrieve events, then clear the event state:

pact
(env-events true)[]
pact
(env-events true)[]

In this example, there were no events to retrieve, so an empty list is returned.

The following example retrieves three events—TOKEN, MINT, and ACCOUNT-GUARD—without clearing the event state:

pact
(env-events false)[{"module-hash": "DmPuO814Zw0C6RL9ubXTGeyNl0I2-svVHgZp-XqRsOs","name": "marmalade-v2.ledger.TOKEN","params": ["t:U50F3xof5EnLQFPd0v2vt8PR3GJTAt8DJ2oWFj7eOgA" 0 [marmalade-v2.non-fungible-policy-v1 marmalade-v2.non-updatable-uri-policy-v1] "uri" KeySet {keys: [e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3],pred: keys-all}]} {"module-hash": "DmPuO814Zw0C6RL9ubXTGeyNl0I2-svVHgZp-XqRsOs","name": "marmalade-v2.ledger.MINT","params": ["t:U50F3xof5EnLQFPd0v2vt8PR3GJTAt8DJ2oWFj7eOgA" "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3" 1.0]} {"module-hash": "DmPuO814Zw0C6RL9ubXTGeyNl0I2-svVHgZp-XqRsOs","name": "marmalade-v2.ledger.ACCOUNT_GUARD","params": ["t:U50F3xof5EnLQFPd0v2vt8PR3GJTAt8DJ2oWFj7eOgA" "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3" KeySet {keys: [e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3],pred: keys-all}]}] In this example, the formatting is modified for readability. ## env-exec-config (DEPRECATED) Use `env-exec-config` to query or set execution configuration information for executing transactions in your testing environment.You can use this function to set one or more of the following configuration flags: - "AllowReadInLocal"- "DisableHistoryInTransactionalMode"- "DisableInlineMemCheck"- "DisableModuleInstall"- "DisableNewTrans"- "DisablePact40"- "DisablePact410"- "DisablePact42"- "DisablePact43"- "DisablePact431"- "DisablePact44"- "DisablePact45"- "DisablePact46"- "DisablePact47"- "DisablePact48"- "DisablePact49"- "DisablePactEvents"- "DisableRuntimeReturnTypeChecking"- "EnforceKeyFormats"- "OldReadOnlyBehavior"- "PreserveModuleIfacesBug"- "PreserveModuleNameBug"- "PreserveNsModuleInstallBug"- "PreserveShowDefs"  ### Basic syntax To look up the current configuration settings, use the following syntax: ```pact(env-exec-config)
pact
(env-events false)[{"module-hash": "DmPuO814Zw0C6RL9ubXTGeyNl0I2-svVHgZp-XqRsOs","name": "marmalade-v2.ledger.TOKEN","params": ["t:U50F3xof5EnLQFPd0v2vt8PR3GJTAt8DJ2oWFj7eOgA" 0 [marmalade-v2.non-fungible-policy-v1 marmalade-v2.non-updatable-uri-policy-v1] "uri" KeySet {keys: [e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3],pred: keys-all}]} {"module-hash": "DmPuO814Zw0C6RL9ubXTGeyNl0I2-svVHgZp-XqRsOs","name": "marmalade-v2.ledger.MINT","params": ["t:U50F3xof5EnLQFPd0v2vt8PR3GJTAt8DJ2oWFj7eOgA" "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3" 1.0]} {"module-hash": "DmPuO814Zw0C6RL9ubXTGeyNl0I2-svVHgZp-XqRsOs","name": "marmalade-v2.ledger.ACCOUNT_GUARD","params": ["t:U50F3xof5EnLQFPd0v2vt8PR3GJTAt8DJ2oWFj7eOgA" "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3" KeySet {keys: [e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3],pred: keys-all}]}] In this example, the formatting is modified for readability. ## env-exec-config (DEPRECATED) Use `env-exec-config` to query or set execution configuration information for executing transactions in your testing environment.You can use this function to set one or more of the following configuration flags: - "AllowReadInLocal"- "DisableHistoryInTransactionalMode"- "DisableInlineMemCheck"- "DisableModuleInstall"- "DisableNewTrans"- "DisablePact40"- "DisablePact410"- "DisablePact42"- "DisablePact43"- "DisablePact431"- "DisablePact44"- "DisablePact45"- "DisablePact46"- "DisablePact47"- "DisablePact48"- "DisablePact49"- "DisablePactEvents"- "DisableRuntimeReturnTypeChecking"- "EnforceKeyFormats"- "OldReadOnlyBehavior"- "PreserveModuleIfacesBug"- "PreserveModuleNameBug"- "PreserveNsModuleInstallBug"- "PreserveShowDefs"  ### Basic syntax To look up the current configuration settings, use the following syntax: ```pact(env-exec-config)

To set one or more configuration flags, use the following syntax:

pact
(env-exec-config [flags])
pact
(env-exec-config [flags])

Arguments

Use the following argument to specify the configuration data you want to set using the env-exec-config Pact function.

ArgumentTypeDescription
flags[string]Specifies the configuration flags you want to be set for the execution environment.

Return value

The env-exec-config function returns the list of configuration flags that have been set for the execution environment.

Examples

The following example demonstrates how to use the env-exec-config function to enforce key formats and disable Pact events:

pact
(env-exec-config ["EnforceKeyFormats" "DisablePactEvents"])["DisablePactEvents" "EnforceKeyFormats"]
pact
(env-exec-config ["EnforceKeyFormats" "DisablePactEvents"])["DisablePactEvents" "EnforceKeyFormats"]

The following example demonstrates how to unset previously-set configuration flags using the env-exec-config function:

pact
(env-exec-config [])[]
pact
(env-exec-config [])[]

The following example demonstrates how to check the current configuration settings:

pact
(env-exec-config)[]
pact
(env-exec-config)[]

env-gas

Use env-gas to query the current gas state or set it to a specific value.

Basic syntax

To query the current gas state, use the following syntax:

pact
(env-gas)
pact
(env-gas)

To set the gas state to a specific value, use the following syntax:

pact
(env-gas gas)
pact
(env-gas gas)

Arguments

Use the following argument to set the gas state when using the env-gas Pact function.

ArgumentTypeDescription
gasintegerSpecifies the value to set the gas state to (optional).

Return value

When called without arguments, the env-gas function returns an integer representing the current gas state.

When called with the gas argument, the env-gas function returns a string indicating that the gas state has been set to the specified value.

Examples

The following example demonstrates how to use the env-gas function to query the current gas state:

pact
pact> (env-gas)7
pact
pact> (env-gas)7

In the following example, the env-gas function resets the current gas state to zero:

pact
pact> (env-gasmodel "table") (env-gaslimit 10) (env-gas 0) (map (+ 1) [1 2 3])"Set gas model to table-based cost model""Set gas limit to 10""Set gas to 0"[2 3 4]
pact
pact> (env-gasmodel "table") (env-gaslimit 10) (env-gas 0) (map (+ 1) [1 2 3])"Set gas model to table-based cost model""Set gas limit to 10""Set gas to 0"[2 3 4]

env-gaslimit

Use env-gaslimit to set the environment gas limit to a specific value.

Basic syntax

To set the environment gas limit to a specific value, use the following syntax:

pact
(env-gaslimit limit)
pact
(env-gaslimit limit)

Arguments

Use the following argument to set the gas limit when using the env-gaslimit Pact function.

ArgumentTypeDescription
limitintegerSpecifies the gas limit to set for the environment.

Return value

The env-gaslimit function returns a string indicating that the gas limit has been set to the specified value.

Examples

The following example demonstrates how to use the env-gaslimit function to set a gas limit for the environment:

pact
pact> (env-gaslimit 10)"Set gas limit to 10"
pact
pact> (env-gaslimit 10)"Set gas limit to 10"

env-gaslog

Use env-gaslog to enable gas logging for a block of code. You can use this function to check the gas required to execute a specific block of code.

Basic syntax

To enable gas logging for a specific block of code, use the following syntax:

pact
(env-gaslog) code (env-gaslog)
pact
(env-gaslog) code (env-gaslog)

Arguments

The env-gaslog function does not take any arguments.

Return value

The env-gaslog function returns the log messages in strings that describe the gas consumed by each operation in the code block.

Examples

The following example demonstrates how to use the env-gaslog function to report the gas consumed for the specified map function:

pact
(env-gasmodel "table") (env-gaslimit 10) (env-gaslog) (map (+ 1) [1 2 3]) (env-gaslog)"Set gas model to table-based cost model""Set gas limit to 10"["TOTAL: 0"][2 3 4]["TOTAL: 7""map:GUnreduced:currTotalGas=4: 4""+:GUnreduced:currTotalGas=5: 1"":GIntegerOpCost:(1, ):(1, ):Pact48IntThreshold:currTotalGas=5: 0""+:GUnreduced:currTotalGas=6: 1"":GIntegerOpCost:(1, ):(2, ):Pact48IntThreshold:currTotalGas=6: 0""+:GUnreduced:currTotalGas=7: 1"":GIntegerOpCost:(1, ):(3, ):Pact48IntThreshold:currTotalGas=7: 0"]
pact
(env-gasmodel "table") (env-gaslimit 10) (env-gaslog) (map (+ 1) [1 2 3]) (env-gaslog)"Set gas model to table-based cost model""Set gas limit to 10"["TOTAL: 0"][2 3 4]["TOTAL: 7""map:GUnreduced:currTotalGas=4: 4""+:GUnreduced:currTotalGas=5: 1"":GIntegerOpCost:(1, ):(1, ):Pact48IntThreshold:currTotalGas=5: 0""+:GUnreduced:currTotalGas=6: 1"":GIntegerOpCost:(1, ):(2, ):Pact48IntThreshold:currTotalGas=6: 0""+:GUnreduced:currTotalGas=7: 1"":GIntegerOpCost:(1, ):(3, ):Pact48IntThreshold:currTotalGas=7: 0"]

In this example, the ["TOTAL: 0"] message signals the beginning of gas logging for the map function. The messages after "TOTAL: 7" provide details about how the gas was calculated.

env-gasmodel

Use env-gasmodel to query or update the current gas model.

Basic syntax

To query the current gas model, use the following syntax:

pact
(env-gasmodel)
pact
(env-gasmodel)

To update the gas model to a table-based cost model, use the following syntax:

pact
(env-gasmodel model)
pact
(env-gasmodel model)

To update the gas model to a fixed-rate model with a specific rate, use the following syntax:

pact
(env-gasmodel model rate)
pact
(env-gasmodel model rate)

Arguments

Use the following arguments when using the env-gasmodel Pact function.

ArgumentTypeDescription
modelstringSpecifies the gas model to set. The supported gas models are table to use a table-based gas model and fixed to use a fixed rate gas model.
rateintegerSpecifies the fixed rate or every operation. This argument is required if you set the model argument to use the fixed gas model.

Return value

When called with the model argument, env-gasmodel returns a string indicating the updated gas model.

When called without arguments, env-gasmodel returns a string describing the current gas model.

Examples

The following example demonstrates how to use the env-gasmodel function to query the current gas model:

pact
pact> (env-gasmodel)"Current gas model is 'fixed 1': constant rate gas model with fixed rate 1"
pact
pact> (env-gasmodel)"Current gas model is 'fixed 1': constant rate gas model with fixed rate 1"

The following example demonstrates how to use the env-gasmodel function to update the current gas model to use a table-based cost model:

pact
pact> (env-gasmodel "table")"Set gas model to table-based cost model"
pact
pact> (env-gasmodel "table")"Set gas model to table-based cost model"

In the following example, the env-gasmodel function updates the gas model to use a fixed rate of two:

pact
pact> (env-gasmodel "fixed" 2)"Set gas model to constant rate gas model with fixed rate 2"
pact
pact> (env-gasmodel "fixed" 2)"Set gas model to constant rate gas model with fixed rate 2"

env-hash

Use env-hash to set the current transaction hash.

Basic syntax

To set the current transaction hash, use the following syntax:

pact
(env-hash hash)
pact
(env-hash hash)

Arguments

Use the following argument when using the env-hash Pact function.

ArgumentTypeDescription
hashstringSpecifies the hash value to set as the current transaction hash. The hash must be an unpadded base64-url encoded BLAKE2b 256-bit hash.

Return value

The env-hash function returns a string indicating that the transaction hash has been set to the specified value.

Example

The following example demonstrates how to use the env-hash function to set the transaction hash using the base64-url encoded string for the hello string:

pact
pact> (env-hash (hash "hello"))"Set tx hash to Mk3PAn3UowqTLEQfNlol6GsXPe-kuOWJSCU0cbgbcs8"
pact
pact> (env-hash (hash "hello"))"Set tx hash to Mk3PAn3UowqTLEQfNlol6GsXPe-kuOWJSCU0cbgbcs8"

The following example illustrates using the env-hash function to create a transaction hash for the test transaction that creates a token:

pact
(env-hash (hash "create-tokens"))
pact
(env-hash (hash "create-tokens"))

env-keys (DEPRECATED)

Use env-keys to set the transaction signer keys in older version of the Pact REPL. The env-keys function is deprecated in favor of env-sigs. You should use env-sigs for setting transaction signer keys with associated capabilities.

Basic syntax

To set transaction signer keys in older version of the Pact REPL, use the following syntax:

pact
(env-keys keys)
pact
(env-keys keys)

Arguments

Use the following argument when using the env-keys Pact function.

ArgumentTypeDescription
keys[string]Specifies the list of keys to set as transaction signer keys.

Return value

The env-keys function returns a string indicating that the transaction keys have been set.

Example

The following example demonstrates how to use the env-keys function to set "my-key" and "admin-key" as the current transaction signing keys in a Pact REPL:

pact
pact> (env-keys ["my-key" "admin-key"])"Setting transaction keys"
pact
pact> (env-keys ["my-key" "admin-key"])"Setting transaction keys"

env-namespace-policy

Use env-namespace-policy to install a managed namespace policy.

Basic syntax

To install a managed namespace policy, use the following syntax:

pact
(env-namespace-policy allow-root ns-policy-func)
pact
(env-namespace-policy allow-root ns-policy-func)

Arguments

Use the following arguments when using the env-namespace-policy Pact function.

ArgumentTypeDescription
allow-rootboolSpecifies whether to allow root-level namespace creation. If set to true, root-level namespaces can be created. If set to false, root-level namespace creation is restricted.
ns-policy-funcfunctionSpecifies the namespace policy function. This function takes two arguments: the namespace string (ns) and the namespace admin guard (ns-admin), and returns a boolean value indicating whether the namespace is allowed based on the policy. The function should return true if the namespace is allowed, and false if it is not.

Return value

The env-namespace-policy function returns a string indicating that the namespace policy has been installed.

Example

The following example demonstrates how to use the env-namespace-policy function to install a namespace policy in a Pact REPL:

pact
(env-namespace-policy true (my-ns-policy-func))
pact
(env-namespace-policy true (my-ns-policy-func))

env-sigs

Use env-sigs to set signature keys for signing transactions and granting capabilities.

Basic syntax

To set the signature keys to use for signing transactionL and granting capabilities, use the following syntax:

pact
(env-sigs sigs)
pact
(env-sigs sigs)

Arguments

Use the following argument when using the env-sigs Pact function.

ArgumentTypeDescription
sigs[object]Specifies the list of signature objects. Each object represents a signer key and its associated caps capabilities.

Return value

The env-sigs function returns a string indicating that the transaction signature keys and capabilities have been set.

Examples

The following example demonstrates how to use the env-sigs function to set two transaction signature keys—"my-key" and "admin-key"—and capabilities:

pact
(env-sigs [    {'key: "my-key", 'caps: [(accounts.USER_GUARD "my-account")]    },     {'key: "admin-key", 'caps: []}    ])
pact
(env-sigs [    {'key: "my-key", 'caps: [(accounts.USER_GUARD "my-account")]    },     {'key: "admin-key", 'caps: []}    ])

The following example illustrates using the env-sigs function to grant "any" key the MINT capability:

(env-sigs [ { 'key: 'any ,'caps: [(MINT (read-msg "token-id") (read-string 'account) 1.0)]} ])

expect-failure

Use expect-failure to evaluate an expression and succeed only if the expressions results in an error. This function enables you to verify that use cases that should failbehave as expected.

Basic syntax

To expect a failure without specifying the error message, use the following syntax:

pact
(expect-failure doc exp)
pact
(expect-failure doc exp)

To expect a failure with a specific error message, use the following syntax:

pact
(expect-failure doc err exp)
pact
(expect-failure doc err exp)

Arguments

Use the following arguments when using the expect-failure Pact function.

ArgumentTypeDescription
docstringSpecifies the documentation string describing the expected failure.
errstringSpecifies the expected error message (optional).
expanySpecifies the expression to evaluate. The expression can be of any Pact type.

Return value

The expect-failure function returns a string indicating the success or failure of the expected failure.

  • If the expression exp throws an error, the function returns a string indicating the success of the expected failure, for example, "Expect failure: success: Enforce fails on false".
  • If the expression exp does not throw an error, the function itself throws an error, indicating that the expected failure did not occur.

Examples

The following example demonstrates how to use the expect-failure function without specifying the expected error message:

pact
pact> (expect-failure "Enforce fails on false" (enforce false "Expected error"))"Expect failure: success: Enforce fails on false"
pact
pact> (expect-failure "Enforce fails on false" (enforce false "Expected error"))"Expect failure: success: Enforce fails on false"

The following example illustrates using the expect-failure function with a specific expected error message:

pact
(expect-failure "mint fails without MINT capability in scope"    "Managed capability not installed: (marmalade-v2.ledger.MINT"    (mint (read-msg 'token-id) (read-msg 'account) (read-keyset 'account-guard) 1.0)))
pact
(expect-failure "mint fails without MINT capability in scope"    "Managed capability not installed: (marmalade-v2.ledger.MINT"    (mint (read-msg 'token-id) (read-msg 'account) (read-keyset 'account-guard) 1.0)))

In this example, the doc argument describes the test being performed. The err argument specifies the error message that is returned when the expression is evaluated.

expect-that

Use expect-that to evaluate an expression and succeed if the resulting value passes a predicate function.

You can use any data type for the exp argument as long as the pred function can take that same data type and return the resulting boolean value . By convention, the data type is used to represent type-bound parameters that serve as input for functions and expressions or for generic arguments.

Basic syntax

To evaluate an expression that returns a predicate function, use the following syntax:

pact
(expect-that doc pred exp)
pact
(expect-that doc pred exp)

Arguments

Use the following arguments when using the expect-that Pact function.

ArgumentTypeDescription
docstringSpecifies the documentation string describing the expectation.
predvalue: -> boolSpecifies the predicate function that takes the result of exp and returns a boolean.
expSpecifies the expression to evaluate. The expression can be of any Pact type.

Return value

The expect-that function returns a string indicating the success or failure of the expectation.

Examples

The following example demonstrates how to use the expect-that function to evaluate an expression that returns the expected result:

pact
pact> (expect-that "addition" (< 2) (+ 1 2))"Expect-that: success: addition"
pact
pact> (expect-that "addition" (< 2) (+ 1 2))"Expect-that: success: addition"

The following example demonstrates how to use the expect-that function to evaluate an expression that returns fails to return the expected result:

pact
pact> (expect-that "addition" (> 2) (+ 1 2))"FAILURE: addition: did not satisfy (> 2) : 3:integer"
pact
pact> (expect-that "addition" (> 2) (+ 1 2))"FAILURE: addition: did not satisfy (> 2) : 3:integer"

expect

Use expect to evaluate an expression and verify that the result equals an expected value.

Basic syntax

To evaluate an expression that returns an expected result, use the following syntax:

pact
(expect doc expected actual)
pact
(expect doc expected actual)

Arguments

Use the following arguments when using the expect Pact function.

ArgumentTypeDescription
docstringSpecifies the documentation string describing the expectation.
expectedanySpecifies the expected value to compare against the result of actual.
actualanySpecifies the expression to evaluate. The expression can be of any Pact type.

Return value

The expect function returns a string indicating the success or failure of the expectation.

Examples

The following example demonstrates how to use the function expect to evaluate an expression that returns an expected result in a Pact REPL:

pact
pact> (expect "Sanity prevails." 4 (+ 2 2))"Expect: success: Sanity prevails."
pact
pact> (expect "Sanity prevails." 4 (+ 2 2))"Expect: success: Sanity prevails."

The following example illustrates using the expect function to verify that the information expected to be inserted into a table is the same as the result of the get-token-info function:

(expect "Token info is inserted into table" { "id": "t:YV6-cQBhE_EoIXAuNV08aGXLfcucBEGy0Gb1Pj6w_Oo" ,"supply": 0.0 ,"precision": 0 ,"uri": "test-uri" ,"policies": [] } (get-token-info "t:YV6-cQBhE_EoIXAuNV08aGXLfcucBEGy0Gb1Pj6w_Oo") )