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
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];
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)