Skip to content

Commit

Permalink
feat: contract call as future in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ committed Feb 12, 2024
1 parent d97eb7d commit e9ea3e5
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 50 deletions.
4 changes: 2 additions & 2 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 @@ -19,7 +19,7 @@ near-workspaces = "0.10.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-utils.git", rev = "26876acf2c93e29463a83e550ff408f4cf404bd6" }
integration-utils = { git = "https://github.com/sweatco/integration-utils.git", rev = "26876acf2c93e29463a83e550ff408f4cf404bd6" }
integration-trait = { git = "https://github.com/sweatco/integration-utils.git", rev = "b161b8bc9013fee12f78e5d51a64d32628bf5e20" }
integration-utils = { git = "https://github.com/sweatco/integration-utils.git", rev = "b161b8bc9013fee12f78e5d51a64d32628bf5e20" }

sweat-model = { path = "model" }
4 changes: 2 additions & 2 deletions integration-tests/src/callback_attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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()).call().await?;
let alice_balance_before_attack = context.ft_contract().ft_balance_of(alice.to_near()).await?;
let ft_contract_id = context.ft_contract().account();

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

assert!(result.has_panic("Method on_record is private"));

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

Ok(())
Expand Down
8 changes: 2 additions & 6 deletions integration-tests/src/defer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ async fn test_defer() -> anyhow::Result<()> {
let holder_balance = context
.ft_contract()
.ft_balance_of(claim_contract_account.clone())
.call()
.await?;

assert_eq!(holder_balance.0, 0);

let (total_fee, total_for_user) = context
.ft_contract()
.calculate_payout_with_fee_for_batch(BATCH_SIZE, CLAIM_AMOUNT)
.call()
.await?;

let batch: Vec<_> = (0..BATCH_SIZE).map(|_| (alice.to_near(), CLAIM_AMOUNT)).collect();
Expand All @@ -36,19 +34,17 @@ async fn test_defer() -> anyhow::Result<()> {
.ft_contract()
.defer_batch(batch, claim_contract_account.clone())
.with_user(&oracle)
.call()
.await?;

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

let claim_contract_balance = context
.ft_contract()
.ft_balance_of(claim_contract_account.clone())
.call()
.await?;

let oracle_balance = context.ft_contract().ft_balance_of(oracle.to_near()).call().await?;
let oracle_balance = context.ft_contract().ft_balance_of(oracle.to_near()).await?;

assert_eq!(oracle_balance, total_fee);
assert_eq!(claim_contract_balance, total_for_user);
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/src/formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async fn test_formula() -> Result<()> {

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

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

assert_eq!(0, steps.0);

Expand Down Expand Up @@ -43,7 +43,6 @@ async fn test_formula() -> Result<()> {
.ft_contract()
.formula(U64(tge), steps)
.with_user(&oracle)
.call()
.await?
.0;

Expand Down
6 changes: 2 additions & 4 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ async fn happy_flow() -> anyhow::Result<()> {

assert_eq!(
99999995378125008,
context.ft_contract().formula(100_000.into(), 100).call().await?.0
context.ft_contract().formula(100_000.into(), 100).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()).call().await?.0
context.ft_contract().ft_balance_of(alice.to_near()).await?.0
);

context
Expand All @@ -45,7 +44,6 @@ async fn happy_flow() -> anyhow::Result<()> {
context.claim_contract().as_account().to_near(),
)
.with_user(&oracle)
.call()
.await?;

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion integration-tests/src/measure/record_batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg(test)]

use std::future::IntoFuture;

use anyhow::Result;
use integration_utils::measure::outcome_storage::OutcomeStorage;
use near_workspaces::types::Gas;
Expand Down Expand Up @@ -28,7 +30,7 @@ async fn measure_record_batch() -> Result<Gas> {
.ft_contract()
.record_batch(Default::default())
.with_user(&oracle)
.call(),
.into_future(),
)
.await?;

Expand Down
15 changes: 5 additions & 10 deletions integration-tests/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,27 @@ 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().call().await?;
let result = context.ft_contract().get_steps_since_tge().await?;
assert_eq!(result, U64(0));

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

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

let target_payout = Payout::from(TARGET_BALANCE);

let result = context.ft_contract().ft_balance_of(oracle.to_near()).call().await?;
let result = context.ft_contract().ft_balance_of(oracle.to_near()).await?;
assert_eq!(result, U128(target_payout.fee));

let result = context.ft_contract().ft_balance_of(user.to_near()).call().await?;
let result = context.ft_contract().ft_balance_of(user.to_near()).await?;
assert_eq!(result, U128(target_payout.amount_for_user));

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

Ok(())
Expand Down
12 changes: 2 additions & 10 deletions integration-tests/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,24 @@ pub async fn prepare_contract() -> Result<Context> {
let long = context.long_account_name().await?;
let token_account_id = context.ft_contract().contract.as_account().to_near();

context
.ft_contract()
.new(".u.sweat.testnet".to_string().into())
.call()
.await?;
context.ft_contract().new(".u.sweat.testnet".to_string().into()).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()
.storage_deposit(long.to_near().into(), None)
.call()
.await?;

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

let claim_contract_result = context
.claim_contract()
Expand Down Expand Up @@ -127,7 +120,6 @@ pub async fn prepare_contract() -> Result<Context> {
context
.ft_contract()
.storage_deposit(context.claim_contract().as_account().to_near().into(), None)
.call()
.await?;

Ok(context)
Expand Down
15 changes: 4 additions & 11 deletions integration-tests/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,33 @@ async fn test_transfer() -> anyhow::Result<()> {
.ft_contract()
.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()
.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)
.call()
.await;
let res = context.ft_contract().storage_deposit(Some(bob.to_near()), None).await;
assert!(res.is_ok());

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

// Transfer all tokens from alice to new account
context
.ft_contract()
.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()).call().await?;
let alice_balance_updated = context.ft_contract().ft_balance_of(alice.to_near()).await?;
assert_eq!(U128(0), alice_balance_updated);

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

Ok(())
Expand Down
Binary file modified res/sweat.wasm
Binary file not shown.

0 comments on commit e9ea3e5

Please sign in to comment.