axelar-cgp-sui

Module 0xa5::flow_limit

use 0x1::option;
use 0x2::clock;

Struct FlowLimit

struct FlowLimit has copy, drop, store
Fields
flow_limit: option::Option<u64>
flow_in: u128
flow_out: u128
current_epoch: u64

Constants

#[error]
const EFlowLimitExceeded: vector<u8> = b"flow limit exceeded";

const EPOCH_TIME: u64 = 21600000;

Function new

public(friend) fun new(): flow_limit::FlowLimit
Implementation
public(package) fun new(): FlowLimit {
    FlowLimit {
        flow_limit: option::none(),
        flow_in: 0,
        flow_out: 0,
        current_epoch: 0,
    }
}

Function update_epoch

fun update_epoch(self: &mut flow_limit::FlowLimit, clock: &clock::Clock)
Implementation
fun update_epoch(self: &mut FlowLimit, clock: &Clock) {
    let epoch = clock.timestamp_ms() / EPOCH_TIME;
    if (epoch > self.current_epoch) {
        self.current_epoch = epoch;
        self.flow_in = 0;
        self.flow_out = 0;
    }
}

Function add_flow_in

public(friend) fun add_flow_in(self: &mut flow_limit::FlowLimit, amount: u64, clock: &clock::Clock)
Implementation
public(package) fun add_flow_in(self: &mut FlowLimit, amount: u64, clock: &Clock) {
    if (self.flow_limit.is_none()) {
        return
    };
    let flow_limit = *self.flow_limit.borrow() as u128;

    update_epoch(self, clock);
    assert!(self.flow_in + (amount as u128) < flow_limit + self.flow_out, EFlowLimitExceeded);
    self.flow_in = self.flow_in + (amount as u128);
}

Function add_flow_out

public(friend) fun add_flow_out(self: &mut flow_limit::FlowLimit, amount: u64, clock: &clock::Clock)
Implementation
public(package) fun add_flow_out(self: &mut FlowLimit, amount: u64, clock: &Clock) {
    if (self.flow_limit.is_none()) {
        return
    };
    let flow_limit = *self.flow_limit.borrow() as u128;

    update_epoch(self, clock);
    assert!(self.flow_out + (amount as u128) < flow_limit + self.flow_in, EFlowLimitExceeded);
    self.flow_out = self.flow_out + (amount as u128);
}

Function set_flow_limit

public(friend) fun set_flow_limit(self: &mut flow_limit::FlowLimit, flow_limit: option::Option<u64>)
Implementation
public(package) fun set_flow_limit(self: &mut FlowLimit, flow_limit: Option<u64>) {
    self.flow_limit = flow_limit;
}