ex_plasma v0.1.0 ExPlasma.Utxo

A Utxo is an unspent transaction output. They come in two distinct forms:

  • An output: An output utxo is created whenever a transaction has value that is

             designated to someone(marked as the new `owner`). In order to 
             form a output utxo, you'll need 4 fields specifically:
    • output_type: This is the integer value of the output_type. This is set on the contract

               but also currently hard-coded to `1`, as there is only one output type
               currently.
    • owner: this is the new owner of the output. You can think of this as who

               the value(or `amount`) is be re-attributed to.
    • currency: This is an address/address hash that resolves to the currency/token's

               address on the network. Ether is designated at <<0::160>>.
    • amount: This is the amount that is contained in the utxo and who it's being

               given to.(see `owner`).
  • An input: An input utxo is used/spent whenever you are making a transaction to

            another party. Most transactions(except Deposit) expects you to have
            an `input` in order to create an `output` on a transaction. Since these
            were previously `outputs` but have been stored, you'll need these fields 
            to specify an `input`:
    • blknum: The block number at which this input utxo was created.

           (See the transaction which this `input` was first created as an `output`).
    • txindex: The transaction index for the given utxo.
    • oindex: The offset index for the given utxo. TODO

Link to this section Summary

Functions

Builds a new Utxo.

Returns the Utxo position(pos) number.

Convert a given utxo into an RLP-encodable input list.

Convert a given Utxo into an RLP-encodable output list.

Converts a Utxo into an RLP-encodable list. If your Utxo contains both sets of input/output data, use the to_input_rlp or to_output_rlp methods instead.

Link to this section Types

Link to this type

address_binary()

address_binary() :: <<_::160>>
Link to this type

address_hex()

address_hex() :: <<_::336>>
Link to this type

input_map()

input_map() :: %{
  blknum: non_neg_integer(),
  txindex: non_neg_integer(),
  oindex: non_neg_integer()
}
Link to this type

input_rlp()

input_rlp() :: non_neg_integer() | binary()
Link to this type

output_map()

output_map() :: %{
  owner: address_binary() | address_hex(),
  currency: address_binary() | address_hex(),
  amount: non_neg_integer()
}
Link to this type

output_rlp()

output_rlp() :: [...]
Link to this type

t()

t() :: %ExPlasma.Utxo{
  amount: non_neg_integer() | nil,
  blknum: non_neg_integer() | nil,
  currency: address_binary() | address_hex() | nil,
  oindex: non_neg_integer() | nil,
  output_type: term(),
  owner: address_binary() | address_hex() | nil,
  txindex: non_neg_integer() | nil
}
Link to this type

validation_tuples()

validation_tuples() ::
  {:blknum, :cannot_be_nil}
  | {:blknum, :exceeds_maximum}
  | {:oindex, :cannot_be_nil}
  | {:txindex, :cannot_be_nil}
  | {:txindex, :exceeds_maximum}
  | {:amount, :cannot_be_nil}
  | {:amount, :cannot_be_zero}
  | {:currency, :cannot_be_nil}
  | {:owner, :cannot_be_nil}
  | {:owner, :cannot_be_zero}

Link to this section Functions

Link to this function

new(data)

new(map() | t() | input_rlp() | output_rlp() | input_map() | output_map()) ::
  {:ok, t()} | {:error, validation_tuples()}

Builds a new Utxo.

Examples

# Create a Utxo from an Output RLP list
iex> alias ExPlasma.Utxo
iex> Utxo.new([<<1>>, [<<205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214, 50, 125, 202, 87, 133, 226, 40, 180>>, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>, <<13, 224, 182, 179, 167, 100, 0, 0>>]])
{:ok, %ExPlasma.Utxo{
  amount: 1000000000000000000,
  blknum: nil,
  currency: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
  oindex: nil,
  output_type: 1,
  owner: <<205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214, 50, 125,
    202, 87, 133, 226, 40, 180>>,
  txindex: nil
}}

# Create a Utxo from an Input RLP Item (encoded utxo position)
iex> alias ExPlasma.Utxo
iex> Utxo.new(<<233, 16, 63, 218, 0>>)
{:ok, %ExPlasma.Utxo{
  amount: nil,
  blknum: 1001,
  currency: nil,
  oindex: 0,
  owner: nil,
  txindex: 0
}}

# Create a Utxo from a Utxo position.
iex> alias ExPlasma.Utxo
iex> pos = 2000010001
iex> Utxo.new(pos)
{:ok, %ExPlasma.Utxo{
  amount: nil,
  blknum: 2,
  currency: nil,
  oindex: 1,
  owner: nil,
  txindex: 1
}}

# Create a Utxo from a map
iex> alias ExPlasma.Utxo
iex> Utxo.new(%{blknum: 2, txindex: 1, oindex: 1})
{:ok, %ExPlasma.Utxo{
  amount: nil,
  blknum: 2,
  currency: nil,
  oindex: 1,
  owner: nil,
  txindex: 1
}}

Returns the Utxo position(pos) number.

Examples

iex> alias ExPlasma.Utxo iex> utxo = %Utxo{blknum: 2, oindex: 1, txindex: 1} iex> Utxo.pos(utxo) 2000010001

Link to this function

to_input_rlp(utxo)

to_input_rlp(ExPlasma.Utxo.t() | input_map()) :: binary()

Convert a given utxo into an RLP-encodable input list.

Examples

iex> alias ExPlasma.Utxo iex> utxo = %Utxo{blknum: 2, oindex: 1, txindex: 1} iex> Utxo.to_input_rlp(utxo) <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 119, 53, 187, 17>>
Link to this function

to_output_rlp(utxo)

to_output_rlp(struct()) :: list()

Convert a given Utxo into an RLP-encodable output list.

Examples

iex> alias ExPlasma.Utxo iex> Utxo.to_output_rlp(%Utxo{amount: 0, currency: <<0::160>>, owner: <<0::160>>}) [

<<1>>,
[
  <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
  <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
  <<0>>
]

]

# Produces list with address hashes iex> alias ExPlasma.Utxo iex> address = "0x0000000000000000000000000000000000000000" iex> Utxo.to_output_rlp(%Utxo{owner: address, currency: address, amount: 1}) [

<<1>>,
[
  <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
  <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
  <<1>>
]

]

Link to this function

to_rlp(utxo)

to_rlp(input_map() | output_map()) :: binary()

Converts a Utxo into an RLP-encodable list. If your Utxo contains both sets of input/output data, use the to_input_rlp or to_output_rlp methods instead.

Example

# Convert from an input Utxo iex> alias ExPlasma.Utxo iex> utxo = %Utxo{blknum: 2, oindex: 1, txindex: 1} iex> Utxo.to_rlp(utxo) <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 119, 53, 187, 17>>

# Convert from an output Utxo iex> alias ExPlasma.Utxo iex> utxo = %Utxo{amount: 2, currency: <<0::160>>, owner: <<0::160>>} iex> Utxo.to_rlp(utxo) [<<1>>, [<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,

<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
<<2>>]

]