BSV.Contract.Helpers (BSV v2.1.0) View Source

Base helper module containing helper functions for use in BSV.Contract modules.

Using BSV.Contract.Helpers will import itself and all related helper modules into your context.

use BSV.Contract.Helpers

Alternative, helper modules can be imported individually.

import BSV.Contract.Helpers
import BSV.Contract.OpCodeHelpers
import BSV.Contract.VarIntHelpers

Link to this section Summary

Functions

Assuming the top stack element is an unsigned integer, casts it to a BSV.ScriptNum.t() encoded number.

Iterates over the given enumerable, invoking the handle_each function on each.

Pushes the given data onto the script. If a list of data elements is given, each will be pushed to the script as seperate pushdata elements.

Iterates the given number of times, invoking the handle_each function on each iteration.

Reverses the top item on the stack.

Signs the transaction context and pushes the signature onto the script.

Extracts the bytes from top item on the stack, starting on the given start index for length bytes. The stack item is replaced with the sliced value.

Trims the given number of leading or trailing bytes from the top item on the stack. The stack item is replaced with the trimmed value.

Link to this section Functions

Link to this function

decode_uint(contract, endianess \\ :little)

View Source

Specs

decode_uint(BSV.Contract.t(), atom()) :: BSV.Contract.t()

Assuming the top stack element is an unsigned integer, casts it to a BSV.ScriptNum.t() encoded number.

Link to this function

each(contract, enum, handle_each)

View Source

Specs

Iterates over the given enumerable, invoking the handle_each function on each.

Example

contract
|> each(["foo", "bar", "baz"], fn el, c ->
  c
  |> push(el)
  |> op_cat()
end)

Specs

push(
  BSV.Contract.t(),
  atom() | binary() | integer() | [atom() | binary() | integer()]
) :: BSV.Contract.t()

Pushes the given data onto the script. If a list of data elements is given, each will be pushed to the script as seperate pushdata elements.

Link to this function

repeat(contract, loops, handle_each)

View Source

Specs

Iterates the given number of times, invoking the handle_each function on each iteration.

Example

contract
|> repeat(5, fn _i, c ->
  c
  |> op_5()
  |> op_add()
end)
Link to this function

reverse(contract, length)

View Source

Specs

Reverses the top item on the stack.

This helper function pushes op codes on to the script that will reverse a binary of the given length.

Specs

Signs the transaction context and pushes the signature onto the script.

A list of private keys can be given, in which case each is used to sign and multiple signatures are added.

If no context is available in the contract, then 71 bytes of zeros are pushed onto the script for each private key.

Link to this function

slice(contract, start, length)

View Source

Specs

Extracts the bytes from top item on the stack, starting on the given start index for length bytes. The stack item is replaced with the sliced value.

Binaries are zero indexed. If start is a negative integer, then the start index is counted from the end.

Specs

Trims the given number of leading or trailing bytes from the top item on the stack. The stack item is replaced with the trimmed value.

When the given length is a positive integer, leading bytes are trimmed. When a negative integer is given, trailing bytes are trimmed.