Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(CL): merge main into concentrated-liquidity-main #3211

Merged
merged 38 commits into from
Nov 2, 2022
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
42d73f1
Add servers to openapi spec file (#2980)
daniel-farina Oct 19, 2022
ec7a7a8
Chain.schema.json: Added ibc data, fix genesis name, formatting (#3077)
JeremyParish69 Oct 20, 2022
fdbdc78
labels (#3082)
czarcas7ic Oct 21, 2022
4a67cdd
[x/gamm][stableswap]: Expand inverse relation tests to multi assets a…
AlpinYukseloglu Oct 21, 2022
d812334
remove all uses of two-asset binary search solver (#3084)
AlpinYukseloglu Oct 21, 2022
edfb19b
[stableswap]: Implement simplified direct multi-asset CFMM solver (#3…
AlpinYukseloglu Oct 21, 2022
c51a248
refactor: remove PokePool from the PoolI interface, define on extensi…
p0mvn Oct 21, 2022
f0f31d0
ci(CL): run tests on CL branch and add backport config (#3095)
p0mvn Oct 21, 2022
7f62b6d
After create pool test (#2783)
hieuvubk Oct 22, 2022
b552bab
fix GetModuleToDistributeCoins (#2957)
catShaark Oct 24, 2022
975aeb9
update comment in gamm module (#3103)
ThanhNhann Oct 24, 2022
9176a51
chore(deps): Bump github.com/tendermint/tendermint (#3106)
dependabot[bot] Oct 24, 2022
18f4afa
chore(deps): Bump github.com/golangci/golangci-lint (#3104)
dependabot[bot] Oct 24, 2022
6548902
chore(deps): Bump github.com/stretchr/testify from 1.8.0 to 1.8.1 (#3…
dependabot[bot] Oct 24, 2022
7119419
chore: update concentrated liquidity backport label (#3115)
p0mvn Oct 24, 2022
8590b80
feat(osmomath): log2 approximation (#2788)
p0mvn Oct 24, 2022
ced84c1
[stableswap]: Cap number of assets and post-scaled asset amounts to e…
AlpinYukseloglu Oct 24, 2022
0aa0263
Remove unused versions in mergify (#3121)
niccoloraspa Oct 24, 2022
df9102e
Query lockup params (#3098)
hieuvubk Oct 24, 2022
6274617
CI: Delete failing step (#3124)
ValarDragon Oct 24, 2022
184a85c
[x/gamm][stableswap]: Add inverse join/exit tests, fix single asset j…
AlpinYukseloglu Oct 24, 2022
805f80c
Stableswap implement JoinPoolNoSwap (#2942)
ValarDragon Oct 24, 2022
2ce796c
Make testing suite to ensure queries never alter state (#3001)
hieuvubk Oct 25, 2022
1e80a2a
Remove streamswap (#3146)
hieuvubk Oct 25, 2022
20c72cc
updated the contract to cosmwasm 1.1 and Uint256 for amounts (#2950)
nicolaslara Oct 29, 2022
46d0053
chore: use environment variable instead of build tags to control e2e …
p0mvn Oct 31, 2022
bb5c1c9
Rate limit - Cleaner tests (#3183)
nicolaslara Oct 31, 2022
d8b6d54
changed lints to stable so they change less often (#3184)
nicolaslara Oct 31, 2022
5abf7a4
update version numbers (#3168)
doggystylez Oct 31, 2022
434b42f
chore(deps): Bump github.com/spf13/cobra from 1.6.0 to 1.6.1 (#3187)
dependabot[bot] Oct 31, 2022
a3d0110
chore(deps): Bump github.com/mattn/go-sqlite3 from 1.14.15 to 1.14.16…
dependabot[bot] Oct 31, 2022
5c9bc1b
chore(e2e): add vscode debug configurations (#3180)
p0mvn Oct 31, 2022
5c408f9
osmomath(log/CL): ln(x), log_1.0001(x), log_custom(x) (#3169)
pysel Nov 1, 2022
8cd07fd
feat(release): Automated post-upgrade tasks by code generating upgrad…
pysel Nov 1, 2022
2620afd
chore: Tx post-handler example snippet #3194
alexanderbez Nov 1, 2022
d874d6d
Progress on IBC rate limit spec (#3190)
ValarDragon Nov 1, 2022
29c8561
fix(e2e): various e2e build issues breaking debugging on linux amd64 …
p0mvn Nov 2, 2022
a7acb4f
Merge branch 'main' into roman/merge-main
p0mvn Nov 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updated the contract to cosmwasm 1.1 and Uint256 for amounts (#2950)
nicolaslara authored Oct 29, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 20c72cced1696418046ffe43ff0d0b859e1397bf
17 changes: 3 additions & 14 deletions x/ibc-rate-limit/contracts/rate-limiter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -15,17 +15,6 @@ exclude = [
[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
@@ -43,14 +32,14 @@ optimize = """docker run --rm -v "$(pwd)":/code \
"""

[dependencies]
cosmwasm-std = "1.0.0"
cosmwasm-storage = "1.0.0"
cosmwasm-std = "1.1.0"
cosmwasm-storage = "1.1.0"
cosmwasm-schema = "1.1.0"
cw-storage-plus = "0.13.2"
cw2 = "0.13.2"
schemars = "0.8.8"
serde = { version = "1.0.137", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.31" }

[dev-dependencies]
cosmwasm-schema = "1.0.0"
cw-multi-test = "0.13.2"
13 changes: 13 additions & 0 deletions x/ibc-rate-limit/contracts/rate-limiter/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cosmwasm_schema::write_api;

use rate_limiter::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, SudoMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,
sudo: SudoMsg,
migrate: MigrateMsg,
}
}
71 changes: 37 additions & 34 deletions x/ibc-rate-limit/contracts/rate-limiter/src/contract_tests.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

use crate::{contract::*, ContractError};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{from_binary, Addr, Attribute};
use cosmwasm_std::{from_binary, Addr, Attribute, Uint256};

use crate::helpers::tests::verify_query_response;
use crate::msg::{InstantiateMsg, PathMsg, QueryMsg, QuotaMsg, SudoMsg};
@@ -52,8 +52,8 @@ fn consume_allowance() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), msg).unwrap();

@@ -64,8 +64,8 @@ fn consume_allowance() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let err = sudo(deps.as_mut(), mock_env(), msg).unwrap_err();
assert!(matches!(err, ContractError::RateLimitExceded { .. }));
@@ -91,14 +91,14 @@ fn symetric_flows_dont_consume_allowance() {
let send_msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let recv_msg = SudoMsg::RecvPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};

let res = sudo(deps.as_mut(), mock_env(), send_msg.clone()).unwrap();
@@ -154,8 +154,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 60,
channel_value: 3_000_u32.into(),
funds: 60_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), msg).unwrap();
let Attribute { key, value } = &res.attributes[4];
@@ -166,8 +166,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 60,
channel_value: 3_000_u32.into(),
funds: 60_u32.into(),
};

let res = sudo(deps.as_mut(), mock_env(), msg).unwrap();
@@ -180,8 +180,8 @@ fn asymetric_quotas() {
let recv_msg = SudoMsg::RecvPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 30,
channel_value: 3_000_u32.into(),
funds: 30_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), recv_msg).unwrap();
let Attribute { key, value } = &res.attributes[3];
@@ -195,8 +195,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 60,
channel_value: 3_000_u32.into(),
funds: 60_u32.into(),
};
let err = sudo(deps.as_mut(), mock_env(), msg.clone()).unwrap_err();
assert!(matches!(err, ContractError::RateLimitExceded { .. }));
@@ -205,8 +205,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 30,
channel_value: 3_000_u32.into(),
funds: 30_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), msg.clone()).unwrap();
let Attribute { key, value } = &res.attributes[3];
@@ -246,8 +246,8 @@ fn query_state() {
assert_eq!(value[0].quota.max_percentage_send, 10);
assert_eq!(value[0].quota.max_percentage_recv, 10);
assert_eq!(value[0].quota.duration, RESET_TIME_WEEKLY);
assert_eq!(value[0].flow.inflow, 0);
assert_eq!(value[0].flow.outflow, 0);
assert_eq!(value[0].flow.inflow, Uint256::from(0_u32));
assert_eq!(value[0].flow.outflow, Uint256::from(0_u32));
assert_eq!(
value[0].flow.period_end,
env.block.time.plus_seconds(RESET_TIME_WEEKLY)
@@ -256,16 +256,16 @@ fn query_state() {
let send_msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
sudo(deps.as_mut(), mock_env(), send_msg.clone()).unwrap();

let recv_msg = SudoMsg::RecvPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 30,
channel_value: 3_000_u32.into(),
funds: 30_u32.into(),
};
sudo(deps.as_mut(), mock_env(), recv_msg.clone()).unwrap();

@@ -277,8 +277,8 @@ fn query_state() {
"weekly",
(10, 10),
RESET_TIME_WEEKLY,
30,
300,
30_u32.into(),
300_u32.into(),
env.block.time.plus_seconds(RESET_TIME_WEEKLY),
);
}
@@ -317,8 +317,8 @@ fn bad_quotas() {
"bad_quota",
(100, 100),
200,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(200),
);
}
@@ -343,21 +343,24 @@ fn undo_send() {
let send_msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let undo_msg = SudoMsg::UndoSend {
channel_id: format!("channel"),
denom: format!("denom"),
funds: 300,
funds: 300_u32.into(),
};

sudo(deps.as_mut(), mock_env(), send_msg.clone()).unwrap();

let trackers = RATE_LIMIT_TRACKERS
.load(&deps.storage, ("channel".to_string(), "denom".to_string()))
.unwrap();
assert_eq!(trackers.first().unwrap().flow.outflow, 300);
assert_eq!(
trackers.first().unwrap().flow.outflow,
Uint256::from(300_u32)
);
let period_end = trackers.first().unwrap().flow.period_end;
let channel_value = trackers.first().unwrap().quota.channel_value;

@@ -366,7 +369,7 @@ fn undo_send() {
let trackers = RATE_LIMIT_TRACKERS
.load(&deps.storage, ("channel".to_string(), "denom".to_string()))
.unwrap();
assert_eq!(trackers.first().unwrap().flow.outflow, 0);
assert_eq!(trackers.first().unwrap().flow.outflow, Uint256::from(0_u32));
assert_eq!(trackers.first().unwrap().flow.period_end, period_end);
assert_eq!(trackers.first().unwrap().quota.channel_value, channel_value);
}
12 changes: 6 additions & 6 deletions x/ibc-rate-limit/contracts/rate-limiter/src/execute.rs
Original file line number Diff line number Diff line change
@@ -159,8 +159,8 @@ mod tests {
"daily",
(3, 5),
1600,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(1600),
);

@@ -208,8 +208,8 @@ mod tests {
"daily",
(3, 5),
1600,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(1600),
);

@@ -241,8 +241,8 @@ mod tests {
"different",
(50, 30),
5000,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(5000),
);
}
6 changes: 3 additions & 3 deletions x/ibc-rate-limit/contracts/rate-limiter/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ impl RateLimitingContract {
}

pub mod tests {
use cosmwasm_std::Timestamp;
use cosmwasm_std::{Timestamp, Uint256};

use crate::state::RateLimit;

@@ -46,8 +46,8 @@ pub mod tests {
quota_name: &str,
send_recv: (u32, u32),
duration: u64,
inflow: u128,
outflow: u128,
inflow: Uint256,
outflow: Uint256,
period_end: Timestamp,
) {
assert_eq!(value.quota.name, quota_name);
76 changes: 38 additions & 38 deletions x/ibc-rate-limit/contracts/rate-limiter/src/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ fn mock_app() -> App {
// Instantiate the contract
fn proper_instantiate(paths: Vec<PathMsg>) -> (App, RateLimitingContract) {
let mut app = mock_app();
let cw_template_id = app.store_code(contract_template());
let cw_code_id = app.store_code(contract_template());

let msg = InstantiateMsg {
gov_module: Addr::unchecked(GOV_ADDR),
@@ -52,7 +52,7 @@ fn proper_instantiate(paths: Vec<PathMsg>) -> (App, RateLimitingContract) {

let cw_rate_limit_contract_addr = app
.instantiate_contract(
cw_template_id,
cw_code_id,
Addr::unchecked(GOV_ADDR),
&msg,
&[],
@@ -82,8 +82,8 @@ fn expiration() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
let res = app.sudo(cosmos_msg).unwrap();
@@ -105,8 +105,8 @@ fn expiration() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
let _err = app.sudo(cosmos_msg).unwrap_err();
@@ -123,8 +123,8 @@ fn expiration() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};

let cosmos_msg = cw_rate_limit_contract.sudo(msg);
@@ -162,8 +162,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap();
@@ -172,8 +172,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap_err();
@@ -188,8 +188,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};

let cosmos_msg = cw_rate_limit_contract.sudo(msg);
@@ -207,8 +207,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap();
@@ -224,8 +224,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap_err();
@@ -240,8 +240,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap_err();
@@ -257,8 +257,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap_err();
@@ -272,8 +272,8 @@ fn multiple_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap();
@@ -296,8 +296,8 @@ fn channel_value_cached() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 1,
channel_value: 100_u32.into(),
funds: 1_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap();
@@ -306,8 +306,8 @@ fn channel_value_cached() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100,
funds: 3,
channel_value: 100_u32.into(),
funds: 3_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap_err();
@@ -316,8 +316,8 @@ fn channel_value_cached() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 100000,
funds: 3,
channel_value: 100000_u32.into(),
funds: 3_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg);
app.sudo(cosmos_msg).unwrap_err();
@@ -336,8 +336,8 @@ fn channel_value_cached() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 10_000,
funds: 100,
channel_value: 10_000_u32.into(),
funds: 100_u32.into(),
};

let cosmos_msg = cw_rate_limit_contract.sudo(msg);
@@ -353,8 +353,8 @@ fn channel_value_cached() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 10_000,
funds: 100,
channel_value: 10_000_u32.into(),
funds: 100_u32.into(),
};

let cosmos_msg = cw_rate_limit_contract.sudo(msg);
@@ -364,8 +364,8 @@ fn channel_value_cached() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 1,
funds: 75,
channel_value: 1_u32.into(),
funds: 75_u32.into(),
};

let cosmos_msg = cw_rate_limit_contract.sudo(msg);
@@ -380,8 +380,8 @@ fn add_paths_later() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let cosmos_msg = cw_rate_limit_contract.sudo(msg.clone());
let res = app.sudo(cosmos_msg).unwrap();
29 changes: 14 additions & 15 deletions x/ibc-rate-limit/contracts/rate-limiter/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_std::Addr;
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Uint256};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

@@ -44,7 +45,7 @@ impl QuotaMsg {

/// Initialize the contract with the address of the IBC module and any existing channels.
/// Only the ibc module is allowed to execute actions on this contract
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[cw_serde]
pub struct InstantiateMsg {
pub gov_module: Addr,
pub ibc_module: Addr,
@@ -53,8 +54,7 @@ pub struct InstantiateMsg {

/// The caller (IBC module) is responsible for correctly calculating the funds
/// being sent through the channel
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum ExecuteMsg {
AddPath {
channel_id: String,
@@ -72,34 +72,33 @@ pub enum ExecuteMsg {
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(Vec<crate::state::RateLimit>)]
GetQuotas { channel_id: String, denom: String },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum SudoMsg {
SendPacket {
channel_id: String,
denom: String,
channel_value: u128,
funds: u128,
channel_value: Uint256,
funds: Uint256,
},
RecvPacket {
channel_id: String,
denom: String,
channel_value: u128,
funds: u128,
channel_value: Uint256,
funds: Uint256,
},
UndoSend {
channel_id: String,
denom: String,
funds: u128,
funds: Uint256,
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum MigrateMsg {}
55 changes: 27 additions & 28 deletions x/ibc-rate-limit/contracts/rate-limiter/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, Timestamp};
use cosmwasm_std::{Addr, Timestamp, Uint256};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::cmp;
@@ -62,16 +62,15 @@ pub enum FlowType {
/// This is a design decision to avoid the period calculations and thus reduce gas consumption
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, Copy)]
pub struct Flow {
// Q: Do we have edge case issues with inflow/outflow being u128, e.g. what if a token has super high precision.
pub inflow: u128,
pub outflow: u128,
pub inflow: Uint256,
pub outflow: Uint256,
pub period_end: Timestamp,
}

impl Flow {
pub fn new(
inflow: impl Into<u128>,
outflow: impl Into<u128>,
inflow: impl Into<Uint256>,
outflow: impl Into<Uint256>,
now: Timestamp,
duration: u64,
) -> Self {
@@ -87,15 +86,15 @@ impl Flow {
/// (balance_in, balance_out) where balance_in in is how much has been
/// transferred into the flow, and balance_out is how much value transferred
/// out.
pub fn balance(&self) -> (u128, u128) {
pub fn balance(&self) -> (Uint256, Uint256) {
(
self.inflow.saturating_sub(self.outflow),
self.outflow.saturating_sub(self.inflow),
)
}

/// checks if the flow, in the current state, has exceeded a max allowance
pub fn exceeds(&self, direction: &FlowType, max_inflow: u128, max_outflow: u128) -> bool {
pub fn exceeds(&self, direction: &FlowType, max_inflow: Uint256, max_outflow: Uint256) -> bool {
let (balance_in, balance_out) = self.balance();
match direction {
FlowType::In => balance_in > max_inflow,
@@ -113,21 +112,21 @@ impl Flow {
/// Expire resets the Flow to start tracking the value transfer from the
/// moment this method is called.
pub fn expire(&mut self, now: Timestamp, duration: u64) {
self.inflow = 0;
self.outflow = 0;
self.inflow = Uint256::from(0_u32);
self.outflow = Uint256::from(0_u32);
self.period_end = now.plus_seconds(duration);
}

/// Updates the current flow incrementing it by a transfer of value.
pub fn add_flow(&mut self, direction: FlowType, value: u128) {
pub fn add_flow(&mut self, direction: FlowType, value: Uint256) {
match direction {
FlowType::In => self.inflow = self.inflow.saturating_add(value),
FlowType::Out => self.outflow = self.outflow.saturating_add(value),
}
}

/// Updates the current flow reducing it by a transfer of value.
pub fn undo_flow(&mut self, direction: FlowType, value: u128) {
pub fn undo_flow(&mut self, direction: FlowType, value: Uint256) {
match direction {
FlowType::In => self.inflow = self.inflow.saturating_sub(value),
FlowType::Out => self.outflow = self.outflow.saturating_sub(value),
@@ -139,7 +138,7 @@ impl Flow {
fn apply_transfer(
&mut self,
direction: &FlowType,
funds: u128,
funds: Uint256,
now: Timestamp,
quota: &Quota,
) -> bool {
@@ -166,21 +165,21 @@ pub struct Quota {
pub max_percentage_send: u32,
pub max_percentage_recv: u32,
pub duration: u64,
pub channel_value: Option<u128>,
pub channel_value: Option<Uint256>,
}

impl Quota {
/// Calculates the max capacity (absolute value in the same unit as
/// total_value) in each direction based on the total value of the denom in
/// the channel. The result tuple represents the max capacity when the
/// transfer is in directions: (FlowType::In, FlowType::Out)
pub fn capacity(&self) -> (u128, u128) {
pub fn capacity(&self) -> (Uint256, Uint256) {
match self.channel_value {
Some(total_value) => (
total_value * (self.max_percentage_recv as u128) / 100_u128,
total_value * (self.max_percentage_send as u128) / 100_u128,
total_value * Uint256::from(self.max_percentage_recv) / Uint256::from(100_u32),
total_value * Uint256::from(self.max_percentage_send) / Uint256::from(100_u32),
),
None => (0, 0), // This should never happen, but ig the channel value is not set, we disallow any transfer
None => (0_u32.into(), 0_u32.into()), // This should never happen, but ig the channel value is not set, we disallow any transfer
}
}
}
@@ -221,8 +220,8 @@ impl RateLimit {
&mut self,
path: &Path,
direction: &FlowType,
funds: u128,
channel_value: u128,
funds: Uint256,
channel_value: Uint256,
now: Timestamp,
) -> Result<Self, ContractError> {
let expired = self.flow.apply_transfer(direction, funds, now, &self.quota);
@@ -292,18 +291,18 @@ pub mod tests {
assert!(!flow.is_expired(epoch.plus_seconds(RESET_TIME_WEEKLY)));
assert!(flow.is_expired(epoch.plus_seconds(RESET_TIME_WEEKLY).plus_nanos(1)));

assert_eq!(flow.balance(), (0_u128, 0_u128));
flow.add_flow(FlowType::In, 5);
assert_eq!(flow.balance(), (5_u128, 0_u128));
flow.add_flow(FlowType::Out, 2);
assert_eq!(flow.balance(), (3_u128, 0_u128));
assert_eq!(flow.balance(), (0_u32.into(), 0_u32.into()));
flow.add_flow(FlowType::In, 5_u32.into());
assert_eq!(flow.balance(), (5_u32.into(), 0_u32.into()));
flow.add_flow(FlowType::Out, 2_u32.into());
assert_eq!(flow.balance(), (3_u32.into(), 0_u32.into()));
// Adding flow doesn't affect expiration
assert!(!flow.is_expired(epoch.plus_seconds(RESET_TIME_DAILY)));

flow.expire(epoch.plus_seconds(RESET_TIME_WEEKLY), RESET_TIME_WEEKLY);
assert_eq!(flow.balance(), (0_u128, 0_u128));
assert_eq!(flow.inflow, 0_u128);
assert_eq!(flow.outflow, 0_u128);
assert_eq!(flow.balance(), (0_u32.into(), 0_u32.into()));
assert_eq!(flow.inflow, Uint256::from(0_u32));
assert_eq!(flow.outflow, Uint256::from(0_u32));
assert_eq!(flow.period_end, epoch.plus_seconds(RESET_TIME_WEEKLY * 2));

// Expiration has moved
8 changes: 4 additions & 4 deletions x/ibc-rate-limit/contracts/rate-limiter/src/sudo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{DepsMut, Response, Timestamp};
use cosmwasm_std::{DepsMut, Response, Timestamp, Uint256};

use crate::{
state::{FlowType, Path, RateLimit, RATE_LIMIT_TRACKERS},
@@ -14,8 +14,8 @@ use crate::{
pub fn try_transfer(
deps: DepsMut,
path: &Path,
channel_value: u128,
funds: u128,
channel_value: Uint256,
funds: Uint256,
direction: FlowType,
now: Timestamp,
) -> Result<Response, ContractError> {
@@ -96,7 +96,7 @@ fn add_rate_limit_attributes(response: Response, result: &RateLimit) -> Response

// This function manually injects an inflow. This is used when reverting a
// packet that failed ack or timed-out.
pub fn undo_send(deps: DepsMut, path: &Path, funds: u128) -> Result<Response, ContractError> {
pub fn undo_send(deps: DepsMut, path: &Path, funds: Uint256) -> Result<Response, ContractError> {
// Sudo call. Only go modules should be allowed to access this
let trackers = RATE_LIMIT_TRACKERS.may_load(deps.storage, path.into())?;

Binary file modified x/ibc-rate-limit/testdata/rate_limiter.wasm
Binary file not shown.