0xa6::swap_info
SwapInfo
SwapStatus
new
data_swapping
data_estimating
coin_bag
skip_swap
finalize
destroy
use 0x2::bcs;
use 0x2::tx_context;
use 0xa6::coin_bag;
SwapInfo
struct SwapInfo
status: swap_info::SwapStatus
swap_data: vector<vector<u8>>
coin_bag: coin_bag::CoinBag
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
#[error]
const EAlreadySkippingSwaps: vector<u8> = b"trying to skip swaps while swaps are skipped.";
#[error]
const EDoneEstimating: vector<u8> = b"trying to estimate but estimating is finished.";
#[error]
const EDoneSwapping: vector<u8> = b"trying to swap but swapping is finished.";
#[error]
const ENotDone: vector<u8> = b"trying to finalize but SwapInfo is not Done yet.";
#[error]
const ENotDoneEstimating: vector<u8> = b"trying to swap while still swapping.";
#[error]
const ENotEstimating: vector<u8> = b"trying to get an estimate but estimating is done.";
#[error]
const EOutOfEstimates: vector<u8> = b"trying to get make an estimate but there are none left.";
#[error]
const EOutOfSwaps: vector<u8> = b"trying to get make a swap but there are none left.";
new
public(friend) fun new(data: vector<u8>, ctx: &mut tx_context::TxContext): swap_info::SwapInfo
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,
}
}
data_swapping
public(friend) fun data_swapping(self: &mut swap_info::SwapInfo): (vector<u8>, bool)
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)
}
data_estimating
public(friend) fun data_estimating(self: &mut swap_info::SwapInfo): (vector<u8>, bool)
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)
}
coin_bag
public(friend) fun coin_bag(self: &mut swap_info::SwapInfo): &mut coin_bag::CoinBag
public(package) fun coin_bag(self: &mut SwapInfo): &mut CoinBag {
&mut self.coin_bag
}
skip_swap
public(friend) fun skip_swap(self: &mut swap_info::SwapInfo)
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),
};
}
finalize
public(friend) fun finalize(self: swap_info::SwapInfo)
public(package) fun finalize(self: SwapInfo) {
match (self.status) {
SwapStatus::Done => self.destroy(),
_ => abort (ENotDone),
};
}
destroy
fun destroy(self: swap_info::SwapInfo)