Skip to main content

Module iota::pay

This module provides handy functionality for wallets and iota::Coin management.

use iota::address; use iota::bag; use iota::balance; use iota::coin; use iota::config; use iota::deny_list; use iota::dynamic_field; use iota::dynamic_object_field; use iota::event; use iota::hex; use iota::object; use iota::transfer; use iota::tx_context; use iota::types; use iota::url; use std::address; use std::ascii; use std::bcs; use std::option; use std::string; use std::type_name; use std::vector;

Constants

For when empty vector is supplied into join function.

const ENoCoins: u64 = 0;

Function keep

Transfer c to the sender of the current transaction

public fun keep<T>(c: iota::coin::Coin<T>, ctx: &iota::tx_context::TxContext)

Implementation

public fun keep<T>(c: Coin<T>, ctx: &TxContext) { transfer::public_transfer(c, ctx.sender()) }

Function split

Split coin self to two coins, one with balance split_amount, and the remaining balance is left is self.

public entry fun split<T>(coin: &mut iota::coin::Coin<T>, split_amount: u64, ctx: &mut iota::tx_context::TxContext)

Implementation

public entry fun split<T>(coin: &mut Coin<T>, split_amount: u64, ctx: &mut TxContext) { keep(coin.split(split_amount, ctx), ctx) }

Function split_vec

Split coin self into multiple coins, each with balance specified in split_amounts. Remaining balance is left in self.

public entry fun split_vec<T>(self: &mut iota::coin::Coin<T>, split_amounts: vector<u64>, ctx: &mut iota::tx_context::TxContext)

Implementation

public entry fun split_vec<T>(self: &mut Coin<T>, split_amounts: vector<u64>, ctx: &mut TxContext) { let (mut i, len) = (0, split_amounts.length()); while (i < len) { split(self, split_amounts[i], ctx); i = i + 1; }; }

Function split_and_transfer

Send amount units of c to recipient Aborts with EVALUE if amount is greater than or equal to amount

public entry fun split_and_transfer<T>(c: &mut iota::coin::Coin<T>, amount: u64, recipient: address, ctx: &mut iota::tx_context::TxContext)

Implementation

public entry fun split_and_transfer<T>( c: &mut Coin<T>, amount: u64, recipient: address, ctx: &mut TxContext, ) { transfer::public_transfer(c.split(amount, ctx), recipient) }

Function divide_and_keep

Divide coin self into n - 1 coins with equal balances. If the balance is not evenly divisible by n, the remainder is left in self.

public entry fun divide_and_keep<T>(self: &mut iota::coin::Coin<T>, n: u64, ctx: &mut iota::tx_context::TxContext)

Implementation

public entry fun divide_and_keep<T>(self: &mut Coin<T>, n: u64, ctx: &mut TxContext) { let mut vec: vector<Coin<T>> = self.divide_into_n(n, ctx); let (mut i, len) = (0, vec.length()); while (i < len) { transfer::public_transfer(vec.pop_back(), ctx.sender()); i = i + 1; }; vec.destroy_empty(); }

Function join_vec

Join everything in coins with self

public entry fun join_vec<T>(self: &mut iota::coin::Coin<T>, coins: vector<iota::coin::Coin<T>>)

Implementation

public entry fun join_vec<T>(self: &mut Coin<T>, mut coins: vector<Coin<T>>) { let (mut i, len) = (0, coins.length()); while (i < len) { let coin = coins.pop_back(); self.join(coin); i = i + 1 }; // safe because we've drained the vector coins.destroy_empty() }

Function join_vec_and_transfer

Join a vector of Coin into a single object and transfer it to receiver.

public entry fun join_vec_and_transfer<T>(coins: vector<iota::coin::Coin<T>>, receiver: address)

Implementation

public entry fun join_vec_and_transfer<T>(mut coins: vector<Coin<T>>, receiver: address) { assert!(coins.length() > 0, ENoCoins); let mut self = coins.pop_back(); join_vec(&mut self, coins); transfer::public_transfer(self, receiver) }