Skip to content

Commit

Permalink
Update Integration utils (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ authored Jan 11, 2024
1 parent 975f9ae commit 1c99ccf
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
run: make build

unit-tests:
runs-on: ubuntu-latest
runs-on: macos-latest
steps:
- uses: actions/checkout@v3

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run: make build

unit-tests:
runs-on: ubuntu-latest
runs-on: macos-latest
steps:
- uses: actions/checkout@v3

Expand Down
19 changes: 5 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ near-workspaces = "0.9.0"

near-sdk = { git = "https://github.com/sweatco/near-sdk-rs", rev = "8c48b26cc48d969c1e5f3162141fe9c824fccecd" }
near-contract-standards = { git = "https://github.com/sweatco/near-sdk-rs", rev = "8c48b26cc48d969c1e5f3162141fe9c824fccecd" }
integration-trait = { git = "https://github.com/sweatco/integration-trait.git", rev = "8dd8b63cef5e60448629a8903ea43b642bbf9f45" }
integration-utils = { git = "https://github.com/sweatco/integration-utils.git", rev = "9fe6398c6064dfe8a26c323a6d0ea3c117a0b4a5" }
integration-trait = { git = "https://github.com/sweatco/integration-utils.git", rev = "9a455faf70702e285eea39ae69a73a4d123b523f" }
integration-utils = { git = "https://github.com/sweatco/integration-utils.git", rev = "9a455faf70702e285eea39ae69a73a4d123b523f" }

sweat-model = { path = "model" }
sweat-integration = { path = "sweat-integration" }
4 changes: 2 additions & 2 deletions integration-tests/src/callback_attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn test_call_on_record_in_callback() -> anyhow::Result<()> {

let alice = context.alice().await?;

let alice_balance_before_attack = context.ft_contract().ft_balance_of(alice.to_near()).await?;
let alice_balance_before_attack = context.ft_contract().ft_balance_of(alice.to_near()).call().await?;

let target_amount = U128(1_000_000);
let result = alice
Expand All @@ -33,7 +33,7 @@ async fn test_call_on_record_in_callback() -> anyhow::Result<()> {

assert!(result.has_panic("The operation can be only initiated by an oracle"));

let alice_balance_after_attack = context.ft_contract().ft_balance_of(alice.to_near()).await?;
let alice_balance_after_attack = context.ft_contract().ft_balance_of(alice.to_near()).call().await?;
assert_eq!(alice_balance_before_attack, alice_balance_after_attack);

Ok(())
Expand Down
17 changes: 11 additions & 6 deletions integration-tests/src/defer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(test)]

use integration_utils::{integration_contract::IntegrationContract, misc::ToNear};
use integration_utils::misc::ToNear;
use near_sdk::json_types::U64;
use sweat_model::{FungibleTokenCoreIntegration, SweatApiIntegration, SweatDeferIntegration};

Expand All @@ -13,22 +13,27 @@ async fn test_defer() -> anyhow::Result<()> {
let alice = context.alice().await?;
let holding_account = context.holding_contract().as_account().to_near();

let target_amount = context.ft_contract().formula(U64(0), 10_000).await?;
let target_amount = context.ft_contract().formula(U64(0), 10_000).call().await?;
assert_ne!(0, target_amount.0);

context
.ft_contract()
.with_user(&oracle)
.defer_batch(vec![(alice.to_near(), 10_000)], holding_account.clone())
.with_user(&oracle)
.call()
.await?;

let alice_balance = context.ft_contract().ft_balance_of(alice.to_near()).await?;
let alice_balance = context.ft_contract().ft_balance_of(alice.to_near()).call().await?;
assert_eq!(0, alice_balance.0);

let holder_balance = context.ft_contract().ft_balance_of(holding_account.clone()).await?;
let holder_balance = context
.ft_contract()
.ft_balance_of(holding_account.clone())
.call()
.await?;
assert_eq!(target_amount.0 * 95 / 100, holder_balance.0);

let oracle_balance = context.ft_contract().ft_balance_of(oracle.to_near()).await?;
let oracle_balance = context.ft_contract().ft_balance_of(oracle.to_near()).call().await?;
assert_eq!(target_amount.0 * 5 / 100, oracle_balance.0);

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/src/formula.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use integration_utils::integration_contract::IntegrationContract;
use near_sdk::json_types::U64;
use sweat_model::SweatApiIntegration;

Expand All @@ -13,7 +12,7 @@ async fn test_formula() -> Result<()> {

let oracle = context.oracle().await?;

let steps = context.ft_contract().get_steps_since_tge().await?;
let steps = context.ft_contract().get_steps_since_tge().call().await?;

assert_eq!(0, steps.0);

Expand Down Expand Up @@ -42,8 +41,9 @@ async fn test_formula() -> Result<()> {
for steps in 0..steps_to_convert.len() {
let formula_res = context
.ft_contract()
.with_user(&oracle)
.formula(U64(steps_from_tge[tge]), steps_to_convert[steps])
.with_user(&oracle)
.call()
.await?
.0;

Expand Down
10 changes: 6 additions & 4 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(test)]

use integration_utils::{integration_contract::IntegrationContract, misc::ToNear};
use integration_utils::misc::ToNear;
use sweat_model::{FungibleTokenCoreIntegration, SweatApiIntegration, SweatDeferIntegration};

use crate::prepare::{prepare_contract, IntegrationContext};
Expand All @@ -23,26 +23,28 @@ async fn happy_flow() -> anyhow::Result<()> {

assert_eq!(
99999995378125008,
context.ft_contract().formula(100_000.into(), 100).await?.0
context.ft_contract().formula(100_000.into(), 100).call().await?.0
);

context
.ft_contract()
.tge_mint(&alice.to_near(), 100_000_000.into())
.call()
.await?;

assert_eq!(
100_000_000,
context.ft_contract().ft_balance_of(alice.to_near()).await?.0
context.ft_contract().ft_balance_of(alice.to_near()).call().await?.0
);

context
.ft_contract()
.with_user(&oracle)
.defer_batch(
vec![(alice.to_near(), 1000)],
context.holding_contract().as_account().to_near(),
)
.with_user(&oracle)
.call()
.await?;

Ok(())
Expand Down
19 changes: 12 additions & 7 deletions integration-tests/src/mint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use integration_utils::{integration_contract::IntegrationContract, misc::ToNear};
use integration_utils::misc::ToNear;
use near_sdk::json_types::{U128, U64};
use sweat_model::{FungibleTokenCoreIntegration, SweatApiIntegration};

Expand All @@ -13,25 +13,30 @@ async fn test_mint() -> anyhow::Result<()> {
let user = context.alice().await?;
let oracle = context.oracle().await?;

let result = context.ft_contract().get_steps_since_tge().await?;
let result = context.ft_contract().get_steps_since_tge().call().await?;
assert_eq!(result, U64(0));

let result = context.ft_contract().formula(U64(0), TARGET_STEPS_SINCE_TGE).await?;
let result = context
.ft_contract()
.formula(U64(0), TARGET_STEPS_SINCE_TGE)
.call()
.await?;
assert_eq!(result, U128(TARGET_BALANCE));

context
.ft_contract()
.with_user(&oracle)
.record_batch(vec![(user.to_near(), 10_000u32)])
.with_user(&oracle)
.call()
.await?;

let result = context.ft_contract().ft_balance_of(oracle.to_near()).await?;
let result = context.ft_contract().ft_balance_of(oracle.to_near()).call().await?;
assert_eq!(result, U128(TARGET_BALANCE * 5 / 100));

let result = context.ft_contract().ft_balance_of(user.to_near()).await?;
let result = context.ft_contract().ft_balance_of(user.to_near()).call().await?;
assert_eq!(result, U128(TARGET_BALANCE * 95 / 100));

let result = context.ft_contract().get_steps_since_tge().await?;
let result = context.ft_contract().get_steps_since_tge().call().await?;
assert_eq!(result, U64(TARGET_STEPS_SINCE_TGE as u64));

Ok(())
Expand Down
11 changes: 9 additions & 2 deletions integration-tests/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,25 @@ pub async fn prepare_contract() -> anyhow::Result<Context> {
let oracle = context.oracle().await?;
let alice = context.alice().await?;

context.ft_contract().new(".u.sweat.testnet".to_string().into()).await?;
context
.ft_contract()
.new(".u.sweat.testnet".to_string().into())
.call()
.await?;

context
.ft_contract()
.storage_deposit(oracle.to_near().into(), None)
.call()
.await?;

context
.ft_contract()
.storage_deposit(alice.to_near().into(), None)
.call()
.await?;

context.ft_contract().add_oracle(&oracle.to_near()).await?;
context.ft_contract().add_oracle(&oracle.to_near()).call().await?;

let holding_contract_init_result = context
.holding_contract()
Expand All @@ -72,6 +78,7 @@ pub async fn prepare_contract() -> anyhow::Result<Context> {
context
.ft_contract()
.storage_deposit(context.holding_contract().as_account().to_near().into(), None)
.call()
.await?;

Ok(context)
Expand Down
23 changes: 15 additions & 8 deletions integration-tests/src/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use integration_utils::{integration_contract::IntegrationContract, misc::ToNear};
use integration_utils::misc::ToNear;
use near_sdk::json_types::U128;
use sweat_model::{FungibleTokenCoreIntegration, StorageManagementIntegration, SweatApiIntegration};

Expand All @@ -13,35 +13,42 @@ async fn test_transfer() -> anyhow::Result<()> {

context
.ft_contract()
.with_user(&oracle)
.record_batch(vec![(alice.to_near(), 10_000)])
.with_user(&oracle)
.call()
.await?;

// This will fail because storage is not registered for this new account
let res = context
.ft_contract()
.with_user(&alice)
.ft_transfer(bob.to_near(), U128(9499999991723028480), None)
.with_user(&alice)
.call()
.await;
assert!(res.is_err());

let res = context.ft_contract().storage_deposit(Some(bob.to_near()), None).await;
let res = context
.ft_contract()
.storage_deposit(Some(bob.to_near()), None)
.call()
.await;
assert!(res.is_ok());

let alice_balance = context.ft_contract().ft_balance_of(alice.to_near()).await?;
let alice_balance = context.ft_contract().ft_balance_of(alice.to_near()).call().await?;
assert_ne!(U128(0), alice_balance);

// Transfer all tokens from alice to new account
context
.ft_contract()
.with_user(&alice)
.ft_transfer(bob.to_near(), alice_balance, None)
.with_user(&alice)
.call()
.await?;

let alice_balance_updated = context.ft_contract().ft_balance_of(alice.to_near()).await?;
let alice_balance_updated = context.ft_contract().ft_balance_of(alice.to_near()).call().await?;
assert_eq!(U128(0), alice_balance_updated);

let bob_balance = context.ft_contract().ft_balance_of(bob.to_near()).await?;
let bob_balance = context.ft_contract().ft_balance_of(bob.to_near()).call().await?;
assert_eq!(alice_balance, bob_balance);

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ integration-trait = { workspace = true }

near-sdk = { workspace = true }
near-contract-standards = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
integration-utils = { workspace = true }
Binary file modified res/sweat.wasm
Binary file not shown.
Loading

0 comments on commit 1c99ccf

Please sign in to comment.