-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
257 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,255 +1,6 @@ | ||
#[cfg(test)] | ||
mod farm; | ||
|
||
#[cfg(test)] | ||
mod psp22; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::farm::{self, Farm as _, Instance as Farm}; | ||
use crate::psp22::{self, Instance as PSP22, PSP22 as _}; | ||
|
||
use anyhow::Result; | ||
use assert2::assert; | ||
use drink::{runtime::MinimalRuntime, session::Session, AccountId32}; | ||
use ink_primitives::AccountId; | ||
use ink_wrapper_types::{util::ToAccountId, Connection}; | ||
|
||
const ALICE: drink::AccountId32 = AccountId32::new([2u8; 32]); | ||
const BOB: drink::AccountId32 = AccountId32::new([1u8; 32]); | ||
|
||
fn alice() -> ink_primitives::AccountId { | ||
AsRef::<[u8; 32]>::as_ref(&ALICE).clone().into() | ||
} | ||
|
||
fn bob() -> ink_primitives::AccountId { | ||
AsRef::<[u8; 32]>::as_ref(&BOB).clone().into() | ||
} | ||
|
||
/// Uploads and creates a PSP22 instance with 1B*10^18 issuance and given names. | ||
/// Returns its AccountId casted to PSP22 interface. | ||
fn setup_psp22( | ||
session: &mut Session<MinimalRuntime>, | ||
name: String, | ||
symbol: String, | ||
caller: drink::AccountId32, | ||
) -> PSP22 { | ||
let _code_hash = session.upload_code(psp22::upload()).unwrap(); | ||
|
||
let _ = session.set_actor(caller); | ||
|
||
let instance = PSP22::new( | ||
1_000_000_000u128 * 10u128.pow(18), | ||
Some(name), | ||
Some(symbol), | ||
18, | ||
); | ||
|
||
session | ||
.instantiate(instance) | ||
.unwrap() | ||
.result | ||
.to_account_id() | ||
.into() | ||
} | ||
|
||
/// Uploads and creates a Farm instance with given pool_id and rewards. | ||
/// Returns its AccountId casted to Farm interface. | ||
fn setup_farm( | ||
session: &mut Session<MinimalRuntime>, | ||
pool_id: AccountId, | ||
rewards: Vec<AccountId>, | ||
caller: drink::AccountId32, | ||
) -> Farm { | ||
let _code_hash = session.upload_code(farm::upload()).unwrap(); | ||
|
||
let _ = session.set_actor(caller); | ||
|
||
let instance = Farm::new(pool_id, rewards); | ||
|
||
session | ||
.instantiate(instance) | ||
.unwrap() | ||
.result | ||
.to_account_id() | ||
.into() | ||
} | ||
|
||
/// Increases allowance of given token to given spender by given amount. | ||
fn increase_allowance( | ||
session: &mut Session<MinimalRuntime>, | ||
token: AccountId, | ||
spender: AccountId, | ||
amount: u128, | ||
caller: drink::AccountId32, | ||
) -> Result<()> { | ||
let _ = session.set_actor(caller); | ||
|
||
session | ||
.execute(PSP22::increase_allowance(&token.into(), spender, amount)) | ||
.unwrap() | ||
.result | ||
.unwrap() | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn balance_of( | ||
session: &mut Session<MinimalRuntime>, | ||
token: AccountId, | ||
account: AccountId, | ||
) -> u128 { | ||
session | ||
.query(PSP22::balance_of(&token.into(), account)) | ||
.unwrap() | ||
.result | ||
.unwrap() | ||
} | ||
|
||
fn get_timestamp(session: &mut Session<MinimalRuntime>) -> u64 { | ||
session.sandbox().get_timestamp() | ||
} | ||
|
||
fn set_timestamp(session: &mut Session<MinimalRuntime>, timestamp: u64) { | ||
session.sandbox().set_timestamp(timestamp); | ||
} | ||
|
||
/// Returns farm details. | ||
fn get_farm_details(session: &mut Session<MinimalRuntime>, farm: &Farm) -> farm::FarmDetails { | ||
session | ||
.query(farm.view_farm_details()) | ||
.unwrap() | ||
.result | ||
.unwrap() | ||
} | ||
|
||
/// Starts a farm with given start and end timestamps and rewards. | ||
fn setup_farm_start( | ||
session: &mut Session<MinimalRuntime>, | ||
farm: &Farm, | ||
start: u64, | ||
end: u64, | ||
rewards: Vec<u128>, | ||
caller: drink::AccountId32, | ||
) -> Result<()> { | ||
let _ = session.set_actor(caller); | ||
|
||
session | ||
.execute(farm.owner_start_new_farm(start, end, rewards)) | ||
.unwrap() | ||
.result | ||
.unwrap() | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
const ICE: &str = "ICE"; | ||
const WOOD: &str = "WOOD"; | ||
const SAND: &str = "SAND"; | ||
|
||
#[test] | ||
fn farm_start() { | ||
let mut session: Session<MinimalRuntime> = Session::new().expect("Init new Session"); | ||
|
||
let ice = setup_psp22(&mut session, ICE.to_string(), ICE.to_string(), BOB); | ||
|
||
let wood = { setup_psp22(&mut session, WOOD.to_string(), WOOD.to_string(), BOB) }; | ||
|
||
let sand = { setup_psp22(&mut session, SAND.to_string(), SAND.to_string(), BOB) }; | ||
|
||
let farm = setup_farm( | ||
&mut session, | ||
ice.into(), | ||
vec![wood.into(), sand.into()], | ||
BOB, | ||
); | ||
|
||
let farm_details: farm::FarmDetails = get_farm_details(&mut session, &farm); | ||
|
||
let expected_details = farm::FarmDetails { | ||
pool_id: ice.into(), | ||
reward_tokens: vec![wood.into(), sand.into()], | ||
reward_rates: vec![0, 0], | ||
start: 0, | ||
end: 0, | ||
}; | ||
|
||
assert!(farm_details == expected_details); | ||
|
||
let now = get_timestamp(&mut session); | ||
set_timestamp(&mut session, now); | ||
let now_plus_100 = now + 100; | ||
let farm_start = now_plus_100; | ||
let farm_end = farm_start + 100; | ||
|
||
let call_result = session | ||
.query(farm.owner_start_new_farm(farm_start, farm_end, vec![100, 100])) | ||
.unwrap() | ||
.result | ||
.unwrap(); | ||
|
||
let insufficient_allowance = | ||
farm::FarmError::PSP22Error(farm::PSP22Error::InsufficientAllowance()); | ||
|
||
assert!( | ||
call_result == Err(insufficient_allowance), | ||
"Caller hasn't increased allowance to spend reward tokens for the farm" | ||
); | ||
|
||
let rewards_amount = 100; | ||
|
||
increase_allowance(&mut session, wood.into(), farm.into(), rewards_amount, BOB).unwrap(); | ||
increase_allowance(&mut session, sand.into(), farm.into(), rewards_amount, BOB).unwrap(); | ||
|
||
let bob_wood_balance_before = balance_of(&mut session, wood.into(), bob()); | ||
let bob_sand_balance_before = balance_of(&mut session, sand.into(), bob()); | ||
let farm_wood_balance_before = balance_of(&mut session, wood.into(), farm.into()); | ||
let farm_sand_balance_before = balance_of(&mut session, sand.into(), farm.into()); | ||
|
||
let call_result = setup_farm_start( | ||
&mut session, | ||
&farm, | ||
farm_start, | ||
farm_end, | ||
vec![rewards_amount, rewards_amount], | ||
BOB, | ||
); | ||
|
||
assert!(call_result.is_ok()); | ||
|
||
let expected_details = farm::FarmDetails { | ||
pool_id: ice.into(), | ||
reward_tokens: vec![wood.into(), sand.into()], | ||
reward_rates: vec![1, 1], | ||
start: farm_start, | ||
end: farm_end, | ||
}; | ||
|
||
let farm_details: farm::FarmDetails = get_farm_details(&mut session, &farm); | ||
|
||
assert!(farm_details == expected_details); | ||
|
||
let bob_wood_balance_after = balance_of(&mut session, wood.into(), bob()); | ||
let bob_sand_balance_after = balance_of(&mut session, sand.into(), bob()); | ||
assert!( | ||
bob_wood_balance_after == bob_wood_balance_before - rewards_amount, | ||
"Farm start must deduct rewards from the caller" | ||
); | ||
assert!( | ||
bob_sand_balance_after == bob_sand_balance_before - rewards_amount, | ||
"Farm start must deduct rewards from the caller" | ||
); | ||
let farm_wood_balance_after = balance_of(&mut session, wood.into(), farm.into()); | ||
let farm_sand_balance_after = balance_of(&mut session, sand.into(), farm.into()); | ||
assert!( | ||
farm_wood_balance_after == farm_wood_balance_before + rewards_amount, | ||
"Farm start must transfer rewards to the farm" | ||
); | ||
assert!( | ||
farm_sand_balance_after == farm_sand_balance_before + rewards_amount, | ||
"Farm start must transfer rewards to the farm" | ||
); | ||
} | ||
} | ||
mod tests; | ||
#[cfg(test)] | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use crate::*; | ||
use utils::*; | ||
|
||
use farm::Farm as _; | ||
|
||
use drink::{runtime::MinimalRuntime, session::Session}; | ||
use ink_wrapper_types::Connection; | ||
|
||
#[test] | ||
fn farm_start() { | ||
let mut session: Session<MinimalRuntime> = Session::new().expect("Init new Session"); | ||
|
||
let ice = setup_psp22(&mut session, ICE.to_string(), ICE.to_string(), BOB); | ||
|
||
let wood = { setup_psp22(&mut session, WOOD.to_string(), WOOD.to_string(), BOB) }; | ||
|
||
let sand = { setup_psp22(&mut session, SAND.to_string(), SAND.to_string(), BOB) }; | ||
|
||
let farm = setup_farm( | ||
&mut session, | ||
ice.into(), | ||
vec![wood.into(), sand.into()], | ||
BOB, | ||
); | ||
|
||
let farm_details: farm::FarmDetails = get_farm_details(&mut session, &farm); | ||
|
||
let expected_details = farm::FarmDetails { | ||
pool_id: ice.into(), | ||
reward_tokens: vec![wood.into(), sand.into()], | ||
reward_rates: vec![0, 0], | ||
start: 0, | ||
end: 0, | ||
}; | ||
|
||
assert!(farm_details == expected_details); | ||
|
||
let now = get_timestamp(&mut session); | ||
set_timestamp(&mut session, now); | ||
let now_plus_100 = now + 100; | ||
let farm_start = now_plus_100; | ||
let farm_end = farm_start + 100; | ||
|
||
let call_result = session | ||
.query(farm.owner_start_new_farm(farm_start, farm_end, vec![100, 100])) | ||
.unwrap() | ||
.result | ||
.unwrap(); | ||
|
||
let insufficient_allowance = | ||
farm::FarmError::PSP22Error(farm::PSP22Error::InsufficientAllowance()); | ||
|
||
assert!( | ||
call_result == Err(insufficient_allowance), | ||
"Caller hasn't increased allowance to spend reward tokens for the farm" | ||
); | ||
|
||
let rewards_amount = 100; | ||
|
||
increase_allowance(&mut session, wood.into(), farm.into(), rewards_amount, BOB).unwrap(); | ||
increase_allowance(&mut session, sand.into(), farm.into(), rewards_amount, BOB).unwrap(); | ||
|
||
let bob_wood_balance_before = balance_of(&mut session, wood.into(), bob()); | ||
let bob_sand_balance_before = balance_of(&mut session, sand.into(), bob()); | ||
let farm_wood_balance_before = balance_of(&mut session, wood.into(), farm.into()); | ||
let farm_sand_balance_before = balance_of(&mut session, sand.into(), farm.into()); | ||
|
||
let call_result = setup_farm_start( | ||
&mut session, | ||
&farm, | ||
farm_start, | ||
farm_end, | ||
vec![rewards_amount, rewards_amount], | ||
BOB, | ||
); | ||
|
||
assert!(call_result.is_ok()); | ||
|
||
let expected_details = farm::FarmDetails { | ||
pool_id: ice.into(), | ||
reward_tokens: vec![wood.into(), sand.into()], | ||
reward_rates: vec![1, 1], | ||
start: farm_start, | ||
end: farm_end, | ||
}; | ||
|
||
let farm_details: farm::FarmDetails = get_farm_details(&mut session, &farm); | ||
|
||
assert!(farm_details == expected_details); | ||
|
||
let bob_wood_balance_after = balance_of(&mut session, wood.into(), bob()); | ||
let bob_sand_balance_after = balance_of(&mut session, sand.into(), bob()); | ||
assert!( | ||
bob_wood_balance_after == bob_wood_balance_before - rewards_amount, | ||
"Farm start must deduct rewards from the caller" | ||
); | ||
assert!( | ||
bob_sand_balance_after == bob_sand_balance_before - rewards_amount, | ||
"Farm start must deduct rewards from the caller" | ||
); | ||
let farm_wood_balance_after = balance_of(&mut session, wood.into(), farm.into()); | ||
let farm_sand_balance_after = balance_of(&mut session, sand.into(), farm.into()); | ||
assert!( | ||
farm_wood_balance_after == farm_wood_balance_before + rewards_amount, | ||
"Farm start must transfer rewards to the farm" | ||
); | ||
assert!( | ||
farm_sand_balance_after == farm_sand_balance_before + rewards_amount, | ||
"Farm start must transfer rewards to the farm" | ||
); | ||
} |
Oops, something went wrong.