Skip to content

Commit

Permalink
Merge pull request #3 from PiVortex/main
Browse files Browse the repository at this point in the history
Rust contract tweaks
  • Loading branch information
matiasbenary authored Jul 11, 2024
2 parents 7fae9c1 + a72cc9f commit ad570cb
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 94 deletions.
22 changes: 6 additions & 16 deletions contract-rs/01-basic-auction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,17 @@ pub struct Bid {
pub bid: NearToken,
}

#[near(contract_state,serializers = [json, borsh])]
#[near(contract_state, serializers = [json, borsh])]
#[derive(PanicOnDefault)]
pub struct Contract {
highest_bid: Bid,
auction_end_time: U64,
}

#[near(serializers = [json])]
#[derive(Clone)]
pub struct InfoContract {
highest_bid: Bid,
auction_end_time: U64,
}

#[near]
impl Contract {
#[init]
#[private] // only callable by the contract's account
#[private] // Only callable by the contract's account
pub fn init(end_time: U64) -> Self {
Self {
highest_bid: Bid {
Expand All @@ -45,11 +38,11 @@ impl Contract {
"Auction has ended"
);

// current bid
// Current bid
let bid = env::attached_deposit();
let bidder = env::predecessor_account_id();

// last bid
// Last bid
let Bid {
bidder: last_bidder,
bid: last_bid,
Expand All @@ -73,11 +66,8 @@ impl Contract {
self.auction_end_time
}

pub fn get_info(&self) -> InfoContract {
InfoContract {
highest_bid: self.highest_bid.clone(),
auction_end_time: self.auction_end_time.clone(),
}
pub fn get_auction_info(&self) -> &Contract {
self
}
}

Expand Down
6 changes: 1 addition & 5 deletions contract-rs/01-basic-auction/tests/test_basics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::Utc;
use contract_rs::{Bid,InfoContract};
use contract_rs::Bid;
use near_sdk::{log, NearToken};
use serde_json::json;

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

assert!(init.is_success());

let info_json = contract.view("get_info").await?;
let info: InfoContract = info_json.json::<InfoContract>()?;


// Alice makes first bid
let alice_bid = alice
.call(contract.id(), "bid")
Expand Down
37 changes: 17 additions & 20 deletions contract-rs/02-owner-claims-money/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Bid {
pub bid: NearToken,
}

#[near(contract_state,serializers = [json, borsh])]
#[near(contract_state, serializers = [json, borsh])]
#[derive(PanicOnDefault)]
pub struct Contract {
highest_bid: Bid,
Expand Down Expand Up @@ -42,11 +42,11 @@ impl Contract {
"Auction has ended"
);

// current bid
// Current bid
let bid = env::attached_deposit();
let bidder = env::predecessor_account_id();

// last bid
// Last bid
let Bid {
bidder: last_bidder,
bid: last_bid,
Expand All @@ -62,23 +62,6 @@ impl Contract {
Promise::new(last_bidder).transfer(last_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_info(&self) -> Contract {
Contract {
highest_bid: self.highest_bid.clone(),
auction_end_time: self.auction_end_time.clone(),
auctioneer: self.auctioneer.clone(),
claimed: self.claimed.clone(),
}
}

pub fn claim(&mut self) -> Promise {
require!(
env::block_timestamp() > self.auction_end_time.into(),
Expand All @@ -87,6 +70,20 @@ impl Contract {

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_auction_info(&self) -> &Contract {
self
}
}
2 changes: 1 addition & 1 deletion contract-rs/03-owner-claims-winner-gets-nft/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use near_sdk::{ext_contract, AccountId};

use crate::TokenId;

// Validator interface, for cross-contract calls
// NFT interface for cross-contract calls
#[ext_contract(nft_contract)]
trait NFT {
fn nft_transfer(&self, receiver_id: AccountId, token_id: TokenId) -> String;
Expand Down
33 changes: 16 additions & 17 deletions contract-rs/03-owner-claims-winner-gets-nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,6 @@ impl Contract {
}
}

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

pub fn get_info(&self) -> Contract {
Contract {
highest_bid: self.highest_bid.clone(),
auction_end_time: self.auction_end_time.clone(),
auctioneer: self.auctioneer.clone(),
claimed: self.claimed.clone(),
nft_contract: self.nft_contract.clone(),
token_id: self.token_id.clone(),
}
}

#[payable]
pub fn bid(&mut self) -> Promise {
// Assert the auction is still ongoing
Expand All @@ -71,11 +56,11 @@ impl Contract {
"Auction has ended"
);

// current bid
// Current bid
let bid = env::attached_deposit();
let bidder = env::predecessor_account_id();

// last bid
// Last bid
let Bid {
bidder: last_bidder,
bid: last_bid,
Expand All @@ -101,11 +86,25 @@ impl Contract {

self.claimed = true;

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

// Transfer the NFT to the highest bidder
nft_contract::ext(self.nft_contract.clone())
.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()
}

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

pub fn get_auction_info(&self) -> &Contract {
self
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::Utc;
use contract_rs::{Bid, TokenId};
use near_sdk::{json_types::U128, log, NearToken,Gas};
use contract_rs::Bid;
use near_sdk::{log, NearToken,Gas};
use near_workspaces::result::ExecutionFinalResult;
use serde_json::json;

Expand Down
3 changes: 2 additions & 1 deletion contract-rs/04-ft-owner-claims-winner-gets-nft/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use near_sdk::{ext_contract, AccountId};

use crate::TokenId;

// Validator interface, for cross-contract calls
// FT interface for cross-contract calls
#[ext_contract(ft_contract)]
trait FT {
fn ft_transfer(&self, receiver_id: AccountId, amount: U128) -> String;
}

// NFT interface for cross-contract calls
#[ext_contract(nft_contract)]
trait NFT {
fn nft_transfer(&self, receiver_id: AccountId, token_id: TokenId) -> String;
Expand Down
70 changes: 40 additions & 30 deletions contract-rs/04-ft-owner-claims-winner-gets-nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,6 @@ impl Contract {
}
}

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

pub fn get_info(&self) -> &Contract {
self
}

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

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

self.claimed = true;

ft_contract::ext(self.ft_contract.clone())
.with_attached_deposit(NearToken::from_yoctonear(1))
.with_static_gas(Gas::from_tgas(30))
.ft_transfer(self.auctioneer.clone(), self.highest_bid.bid);

nft_contract::ext(self.nft_contract.clone())
.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());
}

// Users bid by transferring FT tokens
pub fn ft_on_transfer(&mut self, sender_id: AccountId, amount: U128, msg: String) -> U128 {
require!(
Expand All @@ -90,18 +61,22 @@ impl Contract {
let ft = env::predecessor_account_id();
require!(ft == self.ft_contract, "The token is not supported");

// Last bid
let Bid {
bidder: last_bidder,
bid: last_bid,
} = self.highest_bid.clone();

require!(amount >= last_bid, "You must place a higher bid");
// Check if the deposit is higher than the current bid
require!(amount > last_bid, "You must place a higher bid");

// Update the highest bid
self.highest_bid = Bid {
bidder: sender_id,
bid: amount,
};

// Transfer FTs back to the last bidder
if last_bid > U128(0) {
ft_contract::ext(self.ft_contract.clone())
.with_attached_deposit(NearToken::from_yoctonear(1))
Expand All @@ -111,4 +86,39 @@ impl Contract {

U128(0)
}

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

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

self.claimed = true;

// Transfer FTs to the auctioneer
ft_contract::ext(self.ft_contract.clone())
.with_attached_deposit(NearToken::from_yoctonear(1))
.with_static_gas(Gas::from_tgas(30))
.ft_transfer(self.auctioneer.clone(), self.highest_bid.bid);

// Transfer the NFT to the highest bidder
nft_contract::ext(self.nft_contract.clone())
.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()
}

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

pub fn get_auction_info(&self) -> &Contract {
self
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::Utc;
use contract_rs::{Bid, TokenId};
use contract_rs::Bid;
use near_sdk::{json_types::U128, log, NearToken};
use near_sdk::{AccountId, Gas};
use near_workspaces::result::ExecutionFinalResult;
Expand Down Expand Up @@ -92,7 +92,8 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
bob.clone(),
contract_account.clone(),
auctioneer.clone(),
].iter()
]
.iter()
{
let register = account
.call(ft_contract.id(), "storage_deposit")
Expand Down

0 comments on commit ad570cb

Please sign in to comment.