Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorg auction #16

Merged
merged 11 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests-rs-02.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jobs:
- uses: actions/checkout@v4
- name: Install and test modules
run: |
cd ./contract-rs/02-owner-claims-money
cd ./contract-rs/02-winner-gets-nft
cargo test
2 changes: 1 addition & 1 deletion .github/workflows/tests-rs-03.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jobs:
- uses: actions/checkout@v4
- name: Install and test modules
run: |
cd ./contract-rs/03-owner-claims-winner-gets-nft
cd ./contract-rs/03-bid-with-fts
cargo test
14 changes: 0 additions & 14 deletions .github/workflows/tests-rs-04.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/tests-ts-02.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install and test modules
run: |
cd ./contract-ts/02-owner-claims-money
cd ./contract-ts/02-winner-gets-nft
yarn
yarn test
2 changes: 1 addition & 1 deletion .github/workflows/tests-ts-03.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install and test modules
run: |
cd ./contract-ts/03-owner-claims-winner-gets-nft
cd ./contract-ts/03-bid-with-fts
yarn
yarn test
19 changes: 0 additions & 19 deletions .github/workflows/tests-ts-04.yml

This file was deleted.

47 changes: 38 additions & 9 deletions contract-rs/01-basic-auction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ pub struct Bid {
pub struct Contract {
highest_bid: Bid,
auction_end_time: U64,
auctioneer: AccountId,
claimed: bool,
}

#[near]
impl Contract {
#[init]
#[private] // Only callable by the contract's account
pub fn init(end_time: U64) -> Self {
#[private] // only callable by the contract's account
pub fn init(end_time: U64, auctioneer: AccountId) -> Self {
Self {
highest_bid: Bid {
bidder: env::current_account_id(),
bid: NearToken::from_yoctonear(1),
},
auction_end_time: end_time,
claimed: false,
auctioneer,
}
}

Expand Down Expand Up @@ -58,32 +62,57 @@ impl Contract {
Promise::new(last_bidder).transfer(last_bid)
}

pub fn claim(&mut self) -> Promise {
require!(
env::block_timestamp() > self.auction_end_time.into(),
"Auction has not ended yet"
);

require!(!self.claimed, "Auction has already been claimed");
self.claimed = true;

// Transfer tokens to the auctioneer
Promise::new(self.auctioneer.clone()).transfer(self.highest_bid.bid)
}

pub fn get_highest_bid(&self) -> Bid {
self.highest_bid.clone()
}

pub fn get_auction_end_time(&self) -> U64 {
self.auction_end_time
}

pub fn get_auctioneer(&self) -> AccountId {
self.auctioneer.clone()
}

pub fn get_claimed(&self) -> bool {
self.claimed
}
}

/*
* The rest of this file holds the inline tests for the code above
* Learn more about Rust tests: https://doc.rust-lang.org/book/ch11-01-writing-tests.html
*/
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn init_contract() {
let contract = Contract::init(U64::from(1000));
let end_time: U64 = U64::from(1000);
let alice: AccountId = "alice.near".parse().unwrap();
let contract = Contract::init(end_time.clone(), alice.clone());

let default_bid = contract.get_highest_bid();
assert_eq!(default_bid.bidder, env::current_account_id());
assert_eq!(default_bid.bid, NearToken::from_yoctonear(1));

let end_time = contract.get_auction_end_time();
assert_eq!(end_time, U64::from(1000));
let auction_end_time = contract.get_auction_end_time();
assert_eq!(auction_end_time, end_time);

let auctioneer = contract.get_auctioneer();
assert_eq!(auctioneer, alice);

let claimed = contract.get_claimed();
assert_eq!(claimed, false);
}
}
48 changes: 42 additions & 6 deletions contract-rs/01-basic-auction/tests/test_basics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use chrono::Utc;
use near_workspaces::types::{NearToken, AccountId};
use serde_json::json;
use near_sdk::near;

const TEN_NEAR: NearToken = NearToken::from_near(10);
use near_workspaces::types::{AccountId, Gas, NearToken};
use serde_json::json;

#[near(serializers = [json])]
#[derive(Clone)]
Expand All @@ -12,6 +10,8 @@ pub struct Bid {
pub bid: NearToken,
}

const TEN_NEAR: NearToken = NearToken::from_near(10);

#[tokio::test]
async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>> {
let sandbox = near_workspaces::sandbox().await?;
Expand All @@ -21,6 +21,7 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
// 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?;

// Deploy and initialize contract
Expand All @@ -32,7 +33,7 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

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

Expand Down Expand Up @@ -68,7 +69,7 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
assert_eq!(highest_bid.bid, NearToken::from_near(2));
assert_eq!(highest_bid.bidder, *bob.id());

// Check that alice was returned her bid
// Check that Alice was returned her bid
let new_alice_balance = alice.view_account().await?.balance;
assert!(new_alice_balance == alice_balance.saturating_add(NearToken::from_near(1)));

Expand All @@ -81,10 +82,45 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>

assert!(alice_bid.is_failure());

// 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?;

assert!(auctioneer_claim.is_failure());

// Fast forward 200 blocks
let blocks_to_advance = 200;
sandbox.fast_forward(blocks_to_advance).await?;

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

assert!(auctioneer_claim.is_success());

// 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));

// Auctioneer tries to claim the auction again
let auctioneer_claim = auctioneer
.call(contract_account.id(), "claim")
.args_json(json!({}))
.gas(Gas::from_tgas(300))
.transact()
.await?;

assert!(auctioneer_claim.is_failure());

// Alice tries to make a bid when the auction is over
let alice_bid = alice
.call(contract.id(), "bid")
Expand Down
110 changes: 0 additions & 110 deletions contract-rs/02-owner-claims-money/src/lib.rs

This file was deleted.

Loading