Skip to content

Commit

Permalink
workspaces setup 🧪
Browse files Browse the repository at this point in the history
* mint
* transfers
* formula
  • Loading branch information
intmainreturn00 committed May 9, 2022
1 parent 7d05ebd commit 58c69c0
Show file tree
Hide file tree
Showing 8 changed files with 2,474 additions and 73 deletions.
2,034 changes: 1,967 additions & 67 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"./sweat",
"./examples"
]


Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@
```shell
### run unit tests
cargo test -- --nocapture
### add target
rustup target add wasm32-unknown-unknown

### run workspace tests
cargo run --example mint
cargo run --example transfer
cargo run --example formula
###### if thats not works, try:
softwareupdate --install-rosetta
rustup default stable-x86_64-apple-darwin

## compile to wasm
./sweat/build.sh
```
Expand Down
30 changes: 30 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "examples"
version = "0.0.0"
publish = false
edition = "2021"

[dev-dependencies]
anyhow = "1.0"
# borsh = "0.9"
# maplit = "1.0"
near-units = "0.2.0"
# arbitrary_precision enabled for u128 types that workspaces requires for Balance types
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
tokio = { version = "1.10.0", features = ["full"] }
# tracing = "0.1"
# tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }
workspaces = "0.2.0"
near-sdk = "4.0.0-pre.7"

[[example]]
name = "mint"
path = "src/mint.rs"

[[example]]
name = "transfer"
path = "src/transfer.rs"

[[example]]
name = "formula"
path = "src/formula.rs"
238 changes: 238 additions & 0 deletions examples/src/formula.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
use serde_json::json;

use near_sdk::json_types::{U128, U64};
use workspaces::prelude::*;

const SWEAT_WASM_FILEPATH: &str = "./target/wasm32-unknown-unknown/release/sweat.wasm";

const EPS: f64 = 0.00001;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// can't use sandbox on M1 because of
// https://github.com/near/workspaces-rs/issues/110
// ☹️
let worker = workspaces::testnet().await?;
let wasm = std::fs::read(SWEAT_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;
let oracle1 = worker.dev_create_account().await?;
let oracle2 = worker.dev_create_account().await?;

let result = contract
.call(&worker, "new")
.args_json(json!({
"oracles_vec": vec![oracle1.id(), oracle2.id(), contract.id()],
}))?
.transact()
.await?;
println!("deploy & init: {:#?}", result);

let result = worker
.view(contract.id(), "get_steps_from_tge", Vec::new())
.await?
.json::<U64>()?;
assert_eq!(result, U64(0));

let steps_to_convert = vec![
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
];
let steps_from_tge = vec![
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000u64,
999999999000,
];
let mut test_number = 0;
for tge in 0..steps_from_tge.len() {
for steps in 0..steps_to_convert.len() {
let formula_res = oracle1
.call(&worker, contract.id(), "formula")
.args_json((U64(steps_from_tge[tge]), steps_to_convert[steps]))?
.max_gas()
.transact()
.await?
.json::<U128>()?
.0 as f64
/ 1e+18;
println!(
"formula ({} {}) = {}",
steps_from_tge[tge], steps_to_convert[steps], formula_res
);

let diff = formula_res - TEST_RESULTS[test_number];
assert_eq!(true, diff.abs() < EPS);
test_number = test_number + 1;
}
}

Ok(())
}

const TEST_RESULTS: [f64; 153] = [
0.0009999999999997387,
0.009999999999989545,
0.09999999999911131,
0.9999999999126997,
9.999999991285653,
99.99999912872224,
999.9999128737927,
9999.991287394952,
99999.12873965199,
0.0009999999999981703,
0.009999999999973861,
0.09999999999895448,
0.9999999999111314,
9.999999991269972,
99.99999912856542,
999.9999128722244,
9999.99128737927,
99999.12873949517,
0.0009999999999824878,
0.009999999999817035,
0.09999999999738622,
0.9999999998954487,
9.999999991113144,
99.99999912699715,
999.9999128565418,
9999.991287222441,
99999.1287379269,
0.0009999999998256607,
0.009999999998248767,
0.09999999998170353,
0.9999999997386219,
9.999999989544875,
99.99999911131447,
999.9999126997149,
9999.991285654174,
99999.12872224422,
0.0009999999982573922,
0.009999999982566081,
0.09999999982487667,
0.9999999981703533,
9.99999997386219,
99.99999895448761,
999.9999111314463,
9999.991269971488,
99999.12856541736,
0.0009999999825747062,
0.00999999982573922,
0.09999999825660807,
0.9999999824876673,
9.99999981703533,
99.99999738621901,
999.9998954487603,
9999.991113144628,
99999.12699714876,
0.0009999998257478467,
0.009999998257470626,
0.09999998257392213,
0.9999998256608078,
9.999998248766735,
99.99998170353305,
999.9997386219009,
9999.989544876033,
99999.1113144628,
0.0009999982574792517,
0.009999982574784676,
0.09999982574706262,
0.9999982573922128,
9.999982566080785,
99.99982487667356,
999.9981703533058,
9999.973862190083,
99998.9544876033,
0.0009999825747933012,
0.009999825747925172,
0.09999825747846758,
0.9999825747062624,
9.999825739221281,
99.9982566080785,
999.9824876673554,
9999.817035330578,
99997.38621900826,
0.0009998257479337971,
0.009998257479330131,
0.09998257479251717,
0.9998257478467583,
9.998257470626239,
99.9825739221281,
999.8256608078512,
9998.248766735538,
99981.70353305785,
0.0009982574793387558,
0.009982574793379717,
0.09982574793301303,
0.9982574792517169,
9.982574784675826,
99.82574706262396,
998.2573922128099,
9982.566080785124,
99824.87667355372,
0.0009825747933883426,
0.009825747933875585,
0.09825747933797171,
0.9825747933013037,
9.825747925171694,
98.25747846758264,
982.5747062623967,
9825.739221280992,
98256.60807851239,
0.0008257479338842365,
0.00825747933883687,
0.08257479338781916,
0.8257479338232387,
8.257479332737093,
82.5747927778415,
825.7478728254714,
8257.473232960365,
82574.18280016878,
0.00032230806451611884,
0.003223080645160268,
0.03223080645151066,
0.32230806450590477,
3.223080644138863,
32.23080634937016,
322.3080542918551,
3223.0796227338956,
32230.704208873405,
4.541613636363615e-05,
0.0004541613636363422,
0.0045416136363614894,
0.04541613636342166,
0.45416136361489273,
4.541613634216543,
45.41613614892703,
454.1613421654302,
4541.611489270292,
4.741577501003273e-06,
4.739512354008062e-05,
0.00047393471422484454,
0.004739338881660464,
0.047393368165134696,
0.473933648608995,
4.739336490220244,
47.393364695687744,
473.9336257065148,
0.0008257479340584625,
0.008257479340576784,
0.08257479340498369,
0.8257479339714235,
8.257479333984337,
82.57479279007933,
825.7478729476152,
8257.473234181569,
82574.18281238058,
];
95 changes: 95 additions & 0 deletions examples/src/mint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use serde_json::json;

use near_sdk::json_types::{U128, U64};
use workspaces::prelude::*;

const SWEAT_WASM_FILEPATH: &str = "./target/wasm32-unknown-unknown/release/sweat.wasm";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// can't use sandbox on M1 because of
// https://github.com/near/workspaces-rs/issues/110
// ☹️
let worker = workspaces::testnet().await?;
let wasm = std::fs::read(SWEAT_WASM_FILEPATH)?;
let contract = worker.dev_deploy(&wasm).await?;
let oracle1 = worker.dev_create_account().await?;
let oracle2 = worker.dev_create_account().await?;

let result = contract
.call(&worker, "new")
.args_json(json!({
"oracles_vec": vec![oracle1.id(), oracle2.id(), contract.id()],
}))?
.transact()
.await?;
println!("deploy & init: {:#?}", result);

let result = worker
.view(contract.id(), "get_steps_from_tge", Vec::new())
.await?
.json::<U64>()?;
assert_eq!(result, U64(0));

let result = contract
.view(
&worker,
"formula",
json!({
"steps_from_tge": U64(0),
"steps" : 10_000u32,
})
.to_string()
.into_bytes(),
)
.await?
.json::<U128>()?;
assert_eq!(result, U128(9999999991287398400));

let result = contract
.call(&worker, "record_batch")
.args_json(json!({
"steps_batch": vec![(oracle1.id(), 10_000u32)],
}))?
.transact()
.await?;
println!("{:#?}", result);

let result = contract
.view(
&worker,
"ft_balance_of",
json!({
"account_id": oracle1.id(),
})
.to_string()
.into_bytes(),
)
.await?
.json::<U128>()?;
assert_eq!(result, U128(9499999991723028480));
assert_eq!(result, U128(9999999991287398400 * 95 / 100));

let result = contract
.view(
&worker,
"ft_balance_of",
json!({
"account_id": contract.id(),
})
.to_string()
.into_bytes(),
)
.await?
.json::<U128>()?;
assert_eq!(result, U128(499999999564369920));
assert_eq!(result, U128(9999999991287398400 * 5 / 100));

let result = worker
.view(contract.id(), "get_steps_from_tge", Vec::new())
.await?
.json::<U64>()?;
assert_eq!(result, U64(10_000));

Ok(())
}
Loading

0 comments on commit 58c69c0

Please sign in to comment.