Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Jun 7, 2024
1 parent 9024875 commit d6dbbb2
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 181 deletions.
4 changes: 2 additions & 2 deletions contract-rs/01-basic-auction/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Find all our documentation at https://docs.near.org
use near_sdk::json_types::U64;
use near_sdk::{env, near,require, AccountId, NearToken, PanicOnDefault, Promise};
use near_sdk::{env, near, require, AccountId, NearToken, PanicOnDefault, Promise};

#[near(serializers = [json, borsh])]
#[derive(Clone)]
Expand Down Expand Up @@ -86,4 +86,4 @@ mod tests {
let end_time = contract.get_auction_end_time();
assert_eq!(end_time, U64::from(1000));
}
}
}
34 changes: 12 additions & 22 deletions contract-rs/01-basic-auction/tests/test_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,48 +60,38 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
let highest_bid_json = contract.view("get_highest_bid").await?;
let highest_bid: Bid = highest_bid_json.json::<Bid>()?;

assert_eq!(
highest_bid.bid,
NearToken::from_near(1)
);
assert_eq!(highest_bid.bid, NearToken::from_near(1));
assert_eq!(highest_bid.bidder, *alice.id());


// Bob makes second bid
let bob_bid = bob
.call(contract.id(), "bid")
.deposit(NearToken::from_near(2))
.transact()
.await?;
.call(contract.id(), "bid")
.deposit(NearToken::from_near(2))
.transact()
.await?;

assert!(bob_bid.is_success());

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

assert_eq!(
highest_bid.bid,
NearToken::from_near(2)
);
assert_eq!(highest_bid.bid, NearToken::from_near(2));
assert_eq!(highest_bid.bidder, *bob.id());

// Alice makes the third bid but fails
let alice_bid = alice
.call(contract.id(), "bid")
.deposit(NearToken::from_near(1))
.transact()
.await?;
.call(contract.id(), "bid")
.deposit(NearToken::from_near(1))
.transact()
.await?;

assert!(alice_bid.is_failure());

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

assert_eq!(
highest_bid.bid,
NearToken::from_near(2)
);
assert_eq!(highest_bid.bid, NearToken::from_near(2));
assert_eq!(highest_bid.bidder, *bob.id());

Ok(())
}
}
17 changes: 8 additions & 9 deletions contract-rs/02-owner-claims-money/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Contract {
highest_bid: Bid,
auction_end_time: U64,
auctioneer: AccountId,
auction_was_claimed: bool,
claimed: bool,
}

#[near]
Expand All @@ -26,10 +26,10 @@ impl Contract {
Self {
highest_bid: Bid {
bidder: env::current_account_id(),
bid: NearToken::from_yoctonear(0),
bid: NearToken::from_yoctonear(1),
},
auction_end_time: end_time,
auction_was_claimed: false,
claimed: false,
auctioneer,
}
}
Expand Down Expand Up @@ -73,12 +73,11 @@ impl Contract {
pub fn claim(&mut self) -> Promise {
require!(
env::block_timestamp() > self.auction_end_time.into(),
"Auction has ended"
"Auction has not ended yet"
);

require!(!self.auction_was_claimed, "Auction has been claimed");
self.auction_was_claimed = true;
let auctioneer = self.auctioneer.clone();
Promise::new(auctioneer).transfer(self.highest_bid.bid)

require!(!self.claimed, "Auction has already been claimed");
self.claimed = true;
Promise::new(self.auctioneer.clone()).transfer(self.highest_bid.bid)
}
}
80 changes: 36 additions & 44 deletions contract-rs/02-owner-claims-money/tests/test_basics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::Utc;
use contract_rs::Bid;
use near_sdk::{log, NearToken, Gas};
use near_sdk::{log, Gas, NearToken};
use serde_json::json;

const FIVE_NEAR: NearToken = NearToken::from_near(5);
Expand All @@ -27,11 +27,11 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
.unwrap();

let auctioneer = root
.create_subaccount("auctioneer")
.initial_balance(FIVE_NEAR)
.transact()
.await?
.unwrap();
.create_subaccount("auctioneer")
.initial_balance(FIVE_NEAR)
.transact()
.await?
.unwrap();

let contract_account = root
.create_subaccount("contract")
Expand Down Expand Up @@ -67,56 +67,46 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
let highest_bid_json = contract.view("get_highest_bid").await?;
let highest_bid: Bid = highest_bid_json.json::<Bid>()?;

assert_eq!(
highest_bid.bid,
NearToken::from_near(1)
);
assert_eq!(highest_bid.bid, NearToken::from_near(1));
assert_eq!(highest_bid.bidder, *alice.id());


// Bob makes second bid
let bob_bid = bob
.call(contract.id(), "bid")
.deposit(NearToken::from_near(2))
.transact()
.await?;
.call(contract.id(), "bid")
.deposit(NearToken::from_near(2))
.transact()
.await?;

assert!(bob_bid.is_success());

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

assert_eq!(
highest_bid.bid,
NearToken::from_near(2)
);
assert_eq!(highest_bid.bid, NearToken::from_near(2));
assert_eq!(highest_bid.bidder, *bob.id());

// Alice makes the third bid but fails
let alice_bid = alice
.call(contract.id(), "bid")
.deposit(NearToken::from_near(1))
.transact()
.await?;
.call(contract.id(), "bid")
.deposit(NearToken::from_near(1))
.transact()
.await?;

assert!(alice_bid.is_failure());

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

assert_eq!(
highest_bid.bid,
NearToken::from_near(2)
);
assert_eq!(highest_bid.bid, NearToken::from_near(2));
assert_eq!(highest_bid.bidder, *bob.id());

// Auctioneer claims auction but did not finish
let auctioneer_claim = auctioneer
.call(contract_account.id(), "claim")
.args_json(json!({}))
.gas(Gas::from_tgas(300))
.transact()
.await?;
.call(contract_account.id(), "claim")
.args_json(json!({}))
.gas(Gas::from_tgas(300))
.transact()
.await?;
assert!(auctioneer_claim.is_failure());

// Fast forward
Expand All @@ -125,23 +115,25 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
sandbox.fast_forward(blocks_to_advance).await?;

let auctioneer_claim = auctioneer
.call(contract_account.id(), "claim")
.args_json(json!({}))
.gas(Gas::from_tgas(300))
.transact()
.await?;
.call(contract_account.id(), "claim")
.args_json(json!({}))
.gas(Gas::from_tgas(300))
.transact()
.await?;

assert!(auctioneer_claim.is_success());

// TODO: Assert the auctioneer got the money

// Auctioneer claims auction back but fails
let auctioneer_claim =auctioneer
.call(contract_account.id(), "claim")
.args_json(json!({}))
.gas(Gas::from_tgas(300))
.transact()
.await?;
let auctioneer_claim = auctioneer
.call(contract_account.id(), "claim")
.args_json(json!({}))
.gas(Gas::from_tgas(300))
.transact()
.await?;

assert!(auctioneer_claim.is_failure());

Ok(())
}
}
17 changes: 8 additions & 9 deletions contract-rs/03-owner-claims-winner-gets-nft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Find all our documentation at https://docs.near.org
use near_sdk::json_types::{U64};
use near_sdk::{env, near, require, AccountId, Gas, NearToken, PanicOnDefault,Promise};
use near_sdk::json_types::U64;
use near_sdk::{env, near, require, AccountId, Gas, NearToken, PanicOnDefault, Promise};

pub mod ext;
pub use crate::ext::*;
Expand All @@ -20,7 +20,7 @@ pub struct Contract {
highest_bid: Bid,
auction_end_time: U64,
auctioneer: AccountId,
auction_was_claimed: bool,
claimed: bool,
nft_contract: AccountId,
token_id: TokenId,
}
Expand All @@ -38,11 +38,11 @@ impl Contract {
Self {
highest_bid: Bid {
bidder: env::current_account_id(),
bid: NearToken::from_yoctonear(0),
bid: NearToken::from_yoctonear(1),
},
auction_end_time: end_time,
auctioneer,
auction_was_claimed: false,
claimed: false,
nft_contract,
token_id,
}
Expand Down Expand Up @@ -86,12 +86,11 @@ impl Contract {
"Auction has not ended yet"
);

assert!(!self.auction_was_claimed, "Auction has been claimed");
assert!(!self.claimed, "Auction has already been claimed");

self.auction_was_claimed = true;
let auctioneer = self.auctioneer.clone();
self.claimed = true;

Promise::new(auctioneer).transfer(self.highest_bid.bid);
Promise::new(self.auctioneer.clone()).transfer(self.highest_bid.bid);

nft_contract::ext(self.nft_contract.clone())
.with_static_gas(Gas::from_tgas(30))
Expand Down
Loading

0 comments on commit d6dbbb2

Please sign in to comment.