Skip to content

Instantly share code, notes, and snippets.

@kalloc
Created June 22, 2022 15:23
Show Gist options
  • Save kalloc/6d2ee29f65be8ff0d367391d6734911b to your computer and use it in GitHub Desktop.
Save kalloc/6d2ee29f65be8ff0d367391d6734911b to your computer and use it in GitHub Desktop.
running 1 test
test test_claim ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 23.04s
use near_sdk::json_types::U128;
use near_units::{parse_gas, parse_near};
use serde_json::json;
use workspaces::{operations::Function, prelude::*, AccountId};
const NFT_CONTRACT: &[u8] = include_bytes!("../../target/wasm32-unknown-unknown/release/nft.wasm");
const FT_CONTRACT: &[u8] = include_bytes!("../../target/wasm32-unknown-unknown/release/ft.wasm");
#[tokio::test]
async fn test_claim() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let ft_contract = worker.dev_deploy(&FT_CONTRACT).await?;
let nft_contract = worker.dev_deploy(&NFT_CONTRACT).await?;
let root_account = worker.root_account();
let root_id = root_account.id();
let nft_contract_id = nft_contract.as_account().id();
let ft_contract_id = ft_contract.as_account().id();
// Init FT&NFT contracts
let outcome = root_account
.batch(&worker, ft_contract_id)
.call(Function::new("new_default_meta").args_json(json!({}))?)
.call(
Function::new("storage_deposit")
.args_json(json!({"account_id": nft_contract.id()}))?
.deposit(parse_near!("1 N")),
)
.transact()
.await?;
assert!(outcome.is_success());
println!("init ft outcome: {:#?}", outcome);
root_account
.call(&worker, nft_contract_id, "new_default_meta")
.args_json(json!({
"ft": ft_contract.id(),
}))?
.transact()
.await?;
let outcome = root_account
.call(&worker, ft_contract_id, "sudo_extend_operators")
.args_json(json!({"operators": [nft_contract.id()]}))?
.deposit(1)
.transact()
.await?;
assert!(outcome.is_success());
println!("sudo_extend_operators outcome: {:#?}", outcome);
// Explore new tile, build and launch Oil rig
let outcome = root_account
.batch(&worker, nft_contract_id)
.call(
Function::new("explore")
.args_json(json!({"tile_id": 31337u64}))?
.deposit(parse_near!("1 N")),
)
.call(Function::new("build_oil_rig").args_json(json!({"tile_id": 31337u64}))?)
.call(Function::new("launch").args_json(json!({"tile_id": 31337u64}))?)
.transact()
.await?;
println!("launch oil rig outcome: {:#?}", outcome);
// Move to the future and claim available tokens
worker.fast_forward(100).await?;
let outcome = root_account
.call(&worker, nft_contract_id, "claim")
.args_json(json!({"tile_id": 31337u64}))?
.gas(parse_gas!("200 Tgas") as u64)
.transact()
.await?;
println!("claim outcome: {:#?}", outcome);
let outcome = nft_contract.call(&worker, "top").view().await?;
println!("{:?}", outcome.result);
let top5: Vec<(AccountId, U128)> = outcome.json()?;
println!("top5 outcome: {:#?}", top5);
assert_eq!(top5.len(), 1);
assert_eq!(top5.get(0).unwrap().0, worker.root_account().id().clone());
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment