Module 0xa5::flow_limit
use 0x2::clock;
Struct FlowLimit
struct FlowLimit has copy, drop, store
Fields
-
flow_limit: 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: 0,
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 == 0) return;
update_epoch(self, clock);
assert!(
self.flow_in + (amount as u128) < (self.flow_limit as u128) + 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 == 0) return;
update_epoch(self, clock);
assert!(
self.flow_out + (amount as u128) < (self.flow_limit as u128) + 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: u64)
Implementation
public(package) fun set_flow_limit(self: &mut FlowLimit, flow_limit: u64) {
self.flow_limit = flow_limit;
}