Skip to content

Commit

Permalink
Merge pull request #10 from PiVortex/main
Browse files Browse the repository at this point in the history
Reformat tests part 4
  • Loading branch information
matiasbenary authored Aug 13, 2024
2 parents bab6bbf + 6aa8b79 commit bb7c30a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 147 deletions.
2 changes: 1 addition & 1 deletion contract-rs/03-owner-claims-winner-gets-nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Contract {
.with_static_gas(Gas::from_tgas(30))
.with_attached_deposit(NearToken::from_yoctonear(1))
.nft_transfer(self.highest_bid.bidder.clone(), self.token_id.clone());
}
}

pub fn get_highest_bid(&self) -> Bid {
self.highest_bid.clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
let bob = create_subaccount(&root, "bob").await?;
let auctioneer = create_subaccount(&root, "auctioneer").await?;
let contract_account = create_subaccount(&root, "contract").await?;

// Deploy and initialize NFT contract
let nft_wasm = std::fs::read(NFT_WASM_FILEPATH)?;
let nft_contract = sandbox.dev_deploy(&nft_wasm).await?;
Expand Down Expand Up @@ -135,7 +135,7 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

assert!(auctioneer_claim.is_success());

// Checks the auctioneer has the correct balance
// Checks the auctioneer has the correct balance
let auctioneer_balance = auctioneer.view_account().await?.balance;
assert!(auctioneer_balance <= NearToken::from_near(12));
assert!(auctioneer_balance > NearToken::from_millinear(11990));
Expand Down
70 changes: 29 additions & 41 deletions contract-rs/04-ft-owner-claims-winner-gets-nft/tests/test_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ const NFT_WASM_FILEPATH: &str = "./tests/non_fungible_token.wasm";
async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>> {
let sandbox = near_workspaces::sandbox().await?;

let ft_wasm = std::fs::read(FT_WASM_FILEPATH)?;
let ft_contract = sandbox.dev_deploy(&ft_wasm).await?;
let root: near_workspaces::Account = sandbox.root_account()?;

let nft_wasm = std::fs::read(NFT_WASM_FILEPATH)?;
let nft_contract = sandbox.dev_deploy(&nft_wasm).await?;
// Create accounts
let alice = create_subaccount(&root, "alice").await?;
let bob = create_subaccount(&root, "bob").await?;
let auctioneer = create_subaccount(&root, "auctioneer").await?;
let contract_account = create_subaccount(&root, "contract").await?;

let root: near_workspaces::Account = sandbox.root_account()?;
let ft_wasm = std::fs::read(FT_WASM_FILEPATH)?;
let ft_contract = sandbox.dev_deploy(&ft_wasm).await?;

// Initialize FT contract
let res = ft_contract
Expand All @@ -35,7 +38,10 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

assert!(res.is_success());

// Initialize NFT contract
// Deploy and initialize NFT contract
let nft_wasm = std::fs::read(NFT_WASM_FILEPATH)?;
let nft_contract = sandbox.dev_deploy(&nft_wasm).await?;

let res = nft_contract
.call("new_default_meta")
.args_json(serde_json::json!({"owner_id": root.id()}))
Expand All @@ -44,12 +50,6 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

assert!(res.is_success());

// Create accounts
let alice = create_subaccount(&root, "alice").await?;
let bob = create_subaccount(&root, "bob").await?;
let auctioneer = create_subaccount(&root, "auctioneer").await?;
let contract_account = create_subaccount(&root, "contract").await?;

// Mint NFT
let request_payload = json!({
"token_id": "1",
Expand All @@ -70,24 +70,6 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

assert!(res.is_success());

// Deploy and initialize auction contract
let contract_wasm = near_workspaces::compile_project("./").await?;
let contract = contract_account.deploy(&contract_wasm).await?.unwrap();

let now = Utc::now().timestamp();
let a_minute_from_now = (now + 60) * 1000000000;
let starting_price = U128(10_000);

let init: ExecutionFinalResult = contract
.call("init")
.args_json(
json!({"end_time": a_minute_from_now.to_string(),"auctioneer": auctioneer.id(),"ft_contract": ft_contract.id(),"nft_contract":nft_contract.id(),"token_id":"1", "starting_price":starting_price }),
)
.transact()
.await?;

assert!(init.is_success());

// Register accounts
for account in [
alice.clone(),
Expand All @@ -113,16 +95,27 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
let root_transfer_alice =
ft_transfer(&root, alice.clone(), ft_contract.clone(), transfer_amount).await?;
assert!(root_transfer_alice.is_success());

let alice_balance: U128 = ft_balance_of(&ft_contract, alice.id()).await?;
assert_eq!(alice_balance, U128(150_000));

let root_transfer_bob =
ft_transfer(&root, bob.clone(), ft_contract.clone(), transfer_amount).await?;
assert!(root_transfer_bob.is_success());

let bob_balance: U128 = ft_balance_of(&ft_contract, bob.id()).await?;
assert_eq!(bob_balance, U128(150_000));
// Deploy and initialize auction contract
let contract_wasm = near_workspaces::compile_project("./").await?;
let contract = contract_account.deploy(&contract_wasm).await?.unwrap();

let now = Utc::now().timestamp();
let a_minute_from_now = (now + 60) * 1000000000;
let starting_price = U128(10_000);

let init: ExecutionFinalResult = contract
.call("init")
.args_json(
json!({"end_time": a_minute_from_now.to_string(),"auctioneer": auctioneer.id(),"ft_contract": ft_contract.id(),"nft_contract":nft_contract.id(),"token_id":"1", "starting_price":starting_price }),
)
.transact()
.await?;

assert!(init.is_success());

// Alice makes bid less than starting price
let alice_bid = ft_transfer_call(
Expand Down Expand Up @@ -162,7 +155,6 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

let contract_account_balance: U128 = ft_balance_of(&ft_contract, contract_account.id()).await?;
assert_eq!(contract_account_balance, U128(50_000));

let alice_balance_after_bid: U128 = ft_balance_of(&ft_contract, alice.id()).await?;
assert_eq!(alice_balance_after_bid, U128(100_000));

Expand All @@ -178,7 +170,6 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
assert!(bob_bid.is_success());

let highest_bid: Bid = contract.view("get_highest_bid").await?.json()?;

assert_eq!(highest_bid.bid, U128(60_000));
assert_eq!(highest_bid.bidder, *bob.id());

Expand All @@ -205,7 +196,6 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

let contract_account_balance: U128 = ft_balance_of(&ft_contract, contract_account.id()).await?;
assert_eq!(contract_account_balance, U128(60_000));

let alice_balance_after_bid: U128 = ft_balance_of(&ft_contract, alice.id()).await?;
assert_eq!(alice_balance_after_bid, U128(150_000));

Expand All @@ -227,7 +217,6 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

let contract_account_balance: U128 = ft_balance_of(&ft_contract, contract_account.id()).await?;
assert_eq!(contract_account_balance, U128(0));

let auctioneer_balance_after_claim: U128 = ft_balance_of(&ft_contract, auctioneer.id()).await?;
assert_eq!(auctioneer_balance_after_claim, U128(60_000));

Expand Down Expand Up @@ -270,7 +259,6 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

let contract_account_balance: U128 = ft_balance_of(&ft_contract, contract_account.id()).await?;
assert_eq!(contract_account_balance, U128(0));

let alice_balance_after_bid: U128 = ft_balance_of(&ft_contract, alice.id()).await?;
assert_eq!(alice_balance_after_bid, U128(150_000));
let bob_balance_after_bid: U128 = ft_balance_of(&ft_contract, bob.id()).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.beforeEach(async (t) => {
const auctioneer = await root.createSubAccount("auctioneer", { initialBalance: NEAR.parse("10 N").toString() });
const contract = await root.createSubAccount("contract", { initialBalance: NEAR.parse("10 N").toString() });

// Deploy and initalize NFT contract
// Deploy and initialize NFT contract
const nft_contract = await root.devDeploy(NFT_WASM_FILEPATH);
await nft_contract.call(nft_contract, "new_default_meta", { "owner_id": nft_contract.accountId });

Expand Down
Loading

0 comments on commit bb7c30a

Please sign in to comment.