axelar-cgp-sui

Module 0xa6::swap_info

use 0x2::bcs;
use 0x2::tx_context;
use 0xa6::coin_bag;

Struct SwapInfo

struct SwapInfo
Fields
status: swap_info::SwapStatus
swap_data: vector<vector<u8>>
coin_bag: coin_bag::CoinBag

Enum SwapStatus

Swapping: Curently performing swaps, should happen only after all estimation is finished. Estimating: Currently performing estimates. Once all estimates are done and the output is satisfactory then we can swap. Done: Done swapping and can be destroyed.

public enum SwapStatus has copy, drop, store
Variants
Variant Swapping
index: u64
fallback: bool
Variant Estimating
index: u64
fallback: bool
Variant Done

Constants

const EAlreadySkippingSwaps: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 115, 107, 105, 112, 32, 115, 119, 97, 112, 115, 32, 119, 104, 105, 108, 101, 32, 115, 119, 97, 112, 115, 32, 97, 114, 101, 32, 115, 107, 105, 112, 112, 101, 100, 46];

const EDoneEstimating: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 101, 115, 116, 105, 109, 97, 116, 101, 32, 98, 117, 116, 32, 101, 115, 116, 105, 109, 97, 116, 105, 110, 103, 32, 105, 115, 32, 102, 105, 110, 105, 115, 104, 101, 100, 46];

const EDoneSwapping: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 115, 119, 97, 112, 32, 98, 117, 116, 32, 115, 119, 97, 112, 112, 105, 110, 103, 32, 105, 115, 32, 102, 105, 110, 105, 115, 104, 101, 100, 46];

const ENotDone: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 102, 105, 110, 97, 108, 105, 122, 101, 32, 98, 117, 116, 32, 83, 119, 97, 112, 73, 110, 102, 111, 32, 105, 115, 32, 110, 111, 116, 32, 68, 111, 110, 101, 32, 121, 101, 116, 46];

const ENotDoneEstimating: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 115, 119, 97, 112, 32, 119, 104, 105, 108, 101, 32, 115, 116, 105, 108, 108, 32, 115, 119, 97, 112, 112, 105, 110, 103, 46];

const ENotEstimating: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 103, 101, 116, 32, 97, 110, 32, 101, 115, 116, 105, 109, 97, 116, 101, 32, 98, 117, 116, 32, 101, 115, 116, 105, 109, 97, 116, 105, 110, 103, 32, 105, 115, 32, 100, 111, 110, 101, 46];

const EOutOfEstimates: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 103, 101, 116, 32, 109, 97, 107, 101, 32, 97, 110, 32, 101, 115, 116, 105, 109, 97, 116, 101, 32, 98, 117, 116, 32, 116, 104, 101, 114, 101, 32, 97, 114, 101, 32, 110, 111, 110, 101, 32, 108, 101, 102, 116, 46];

const EOutOfSwaps: vector<u8> = [116, 114, 121, 105, 110, 103, 32, 116, 111, 32, 103, 101, 116, 32, 109, 97, 107, 101, 32, 97, 32, 115, 119, 97, 112, 32, 98, 117, 116, 32, 116, 104, 101, 114, 101, 32, 97, 114, 101, 32, 110, 111, 110, 101, 32, 108, 101, 102, 116, 46];

Function new

public(friend) fun new(data: vector<u8>, ctx: &mut tx_context::TxContext): swap_info::SwapInfo
Implementation
public(package) fun new(data: vector<u8>, ctx: &mut TxContext): SwapInfo {
    let swap_data = bcs::new(data).peel_vec_vec_u8();
    SwapInfo {
        status: SwapStatus::Estimating { index: 0, fallback: false },
        coin_bag: coin_bag::new(ctx),
        swap_data,
    }
}

Function data_swapping

public(friend) fun data_swapping(self: &mut swap_info::SwapInfo): (vector<u8>, bool)
Implementation
public(package) fun data_swapping(self: &mut SwapInfo): (vector<u8>, bool) {
    let (index, fallback) = match (self.status) {
        SwapStatus::Swapping { index, fallback } => (index, fallback),
        SwapStatus::Estimating { .. } => abort (
            ENotDoneEstimating,
        ),
        SwapStatus::Done => abort (EDoneSwapping),
    };

    assert!(index < self.swap_data.length(), EOutOfSwaps);

    self.status = if (index + 1 < self.swap_data.length()) {
            SwapStatus::Swapping { index: index + 1, fallback }
        } else {
            SwapStatus::Done
        };

    (self.swap_data[index], fallback)
}

Function data_estimating

public(friend) fun data_estimating(self: &mut swap_info::SwapInfo): (vector<u8>, bool)
Implementation
public(package) fun data_estimating(
    self: &mut SwapInfo,
): (vector<u8>, bool) {
    let (index, fallback) = match (self.status) {
        SwapStatus::Estimating { index, fallback } => (index, fallback),
        _ => abort (EDoneEstimating),
    };

    assert!(index < self.swap_data.length(), EOutOfEstimates);

    self.status = if (index + 1 < self.swap_data.length()) {
            SwapStatus::Estimating { index: index + 1, fallback }
        } else {
            SwapStatus::Swapping { index: 0, fallback }
        };

    (self.swap_data[index], fallback)
}

Function coin_bag

public(friend) fun coin_bag(self: &mut swap_info::SwapInfo): &mut coin_bag::CoinBag
Implementation
public(package) fun coin_bag(self: &mut SwapInfo): &mut CoinBag {
    &mut self.coin_bag
}

Function skip_swap

public(friend) fun skip_swap(self: &mut swap_info::SwapInfo)
Implementation
public(package) fun skip_swap(self: &mut SwapInfo) {
    self.status =
        match (self.status) {
            SwapStatus::Estimating { index, fallback: false } => SwapStatus::Estimating { index, fallback: true },
            SwapStatus::Estimating { .. } => abort (EAlreadySkippingSwaps),
            _ => abort (ENotEstimating),
        };
}

Function finalize

public(friend) fun finalize(self: swap_info::SwapInfo)
Implementation
public(package) fun finalize(self: SwapInfo) {
    match (self.status) {
        SwapStatus::Done => self.destroy(),
        _ => abort (ENotDone),
    };
}

Function destroy

fun destroy(self: swap_info::SwapInfo)
Implementation
fun destroy(self: SwapInfo) {
    let SwapInfo {
        status: _,
        swap_data: _,
        coin_bag,
    } = self;
    coin_bag.destroy();
}