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

add simulated test #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2,367 changes: 2,166 additions & 201 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions contracts/test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "test"
version = "0.1.0"
authors = ["nghuyenthevinh2000 <[email protected]>"]
description = "Simulated chain test on Rust! No need for node! Feel the speed and convenience"
edition = "2018"
license = "GPL-3.0-or-later"
repository = "https://github.com/erisprotocol/contracts-terra-classic"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
cosmwasm-std = { version = "1.1", features = ["stargate"] }
cosmwasm-schema = "1.0.4"
eris = { path = "../../packages/eris" }
thiserror = "1"
classic-bindings = "0.1.1"

[dev-dependencies]
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
classic-test-tube = { version = "0.1.0", git = "https://github.com/classic-terra/classic-rust.git", package = "classic-test-tube", rev="4b035dd53ef7010c12fc74728f3b1671066f67bd"}
11 changes: 11 additions & 0 deletions contracts/test/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cosmwasm_schema::write_api;

use tax::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
}
}
132 changes: 132 additions & 0 deletions contracts/test/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, to_binary, Uint128};

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg, TaxResponse};
use classic_bindings::TerraQuery;
use eris::asset::{Asset, AssetInfo};

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> Result<Response, ContractError> {
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: ExecuteMsg,
) -> Result<Response, ContractError> {
unimplemented!()
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<TerraQuery>, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Tax { denom, amount } => to_binary(&tax(deps, denom, amount)?),
}
}

fn tax(
deps: Deps<TerraQuery>,
denom: String,
amount: Uint128
) -> StdResult<TaxResponse> {
let asset = Asset {
info: AssetInfo::NativeToken {
denom
},
amount
};

let tax = asset.compute_tax(&deps.querier).unwrap();
Ok(TaxResponse { tax })
}

#[cfg(test)]
mod tests {
use classic_test_tube::{Module, Account, Treasury, classic_proto::classic::treasury::{QueryTaxCapRequest, QueryTaxRateRequest}};
use cosmwasm_std::{Uint128, Decimal};

#[test]
fn test_wasm_query_tax() {
use classic_test_tube::TerraTestApp;
use classic_test_tube::Wasm;
use cosmwasm_std::Coin;
use crate::msg::{InstantiateMsg, QueryMsg, TaxResponse};
static DECIMAL_FRACTION: Uint128 = Uint128::new(1_000_000_000_000_000_000u128);

let app = TerraTestApp::default();
let wasm = Wasm::new(&app);
let treasury = Treasury::new(&app);

let accs = app
.init_accounts(
&[
Coin::new(1_000_000_000_000, "uluna"),
Coin::new(1_000_000_000_000, "uusd"),
],
2,
)
.unwrap();

let admin = &accs[0];

// store wasm code
let wasm_byte_code = std::fs::read("./testdata/test-aarch64.wasm").unwrap();
let code_id = wasm
.store_code(&wasm_byte_code, None, admin)
.unwrap()
.data
.code_id;
assert_eq!(code_id, 1);

// initialize admins and check if the state is correct
let contract_addr = wasm
.instantiate(
code_id,
&InstantiateMsg {},
Some(&admin.address()),
None,
&[],
admin,
)
.unwrap()
.data
.address;

// calculate tax from querying to chain
let tax_cap = treasury.query_tax_cap(&QueryTaxCapRequest{
denom: "uluna".to_string()
}).unwrap();

let tax_rate = treasury.query_tax_rate(&QueryTaxRateRequest{}).unwrap();
let dec_tax_rate = Decimal::from_ratio(Uint128::new(tax_rate.tax_rate.parse::<u128>().unwrap()), DECIMAL_FRACTION);

let amount = Uint128::new(1000000000);

let tax = std::cmp::min(
(amount.checked_sub(amount.multiply_ratio(
DECIMAL_FRACTION,
DECIMAL_FRACTION * dec_tax_rate + DECIMAL_FRACTION,
))).unwrap(),
Uint128::new(tax_cap.tax_cap.parse::<u128>().unwrap()),
);

// query tax from contract
let query = QueryMsg::Tax {
denom: "uluna".to_string(),
amount,
};

let res = wasm.query::<QueryMsg, TaxResponse>(contract_addr.as_str(), &query).unwrap();
assert_eq!(tax, res.tax)
}
}
8 changes: 8 additions & 0 deletions contracts/test/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
StdError(#[from] StdError),
}
3 changes: 3 additions & 0 deletions contracts/test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod contract;
pub mod msg;
pub mod error;
24 changes: 24 additions & 0 deletions contracts/test/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Uint128;

#[cw_serde]
pub struct InstantiateMsg {}

#[cw_serde]
pub enum ExecuteMsg {}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {

#[returns(TaxResponse)]
Tax {
denom: String,
amount: Uint128,
}
}

#[cw_serde]
pub struct TaxResponse {
pub tax: Uint128
}
Binary file added contracts/test/testdata/test-aarch64.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/eris/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ repository = "https://github.com/erisprotocol/contracts-terra-classic"
[dependencies]
cosmwasm-std = { version = "1.1", features = ["stargate"] }
cw20 = "0.13.2"
schemars = "0.8.1"
schemars = "0.8.12"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
classic-bindings = "0.1.1"