Module 0x2::pay
This module provides handy functionality for wallets and sui::Coin management.
- Constants
- Function
keep
- Function
split
- Function
split_vec
- Function
split_and_transfer
- Function
divide_and_keep
- Function
join
- Function
join_vec
- Function
join_vec_and_transfer
use 0x2::coin;
use 0x2::transfer;
use 0x2::tx_context;
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: coin::Coin<T>, ctx: &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 coin::Coin<T>, split_amount: u64, ctx: &mut tx_context::TxContext)
Implementation
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 coin::Coin<T>, split_amounts: vector<u64>, ctx: &mut tx_context::TxContext)
Implementation
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 coin::Coin<T>, amount: u64, recipient: address, ctx: &mut 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 coin::Coin<T>, n: u64, ctx: &mut 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
Join coin into self. Re-exports coin::join function. Deprecated: you should call coin.join(other) directly.
public entry fun join<T>(self: &mut coin::Coin<T>, coin: coin::Coin<T>)
Function join_vec
Join everything in coins with self
public entry fun join_vec<T>(self: &mut coin::Coin<T>, coins: vector<coin::Coin<T>>)
Implementation
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<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)
}