Skip to content

Commit

Permalink
feat:added contract-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenary committed Jun 7, 2024
1 parent ce82065 commit 9024875
Show file tree
Hide file tree
Showing 37 changed files with 1,386 additions and 1,135 deletions.
16 changes: 0 additions & 16 deletions contract-rs/01-basic-auction/src/ext.rs

This file was deleted.

113 changes: 46 additions & 67 deletions contract-rs/01-basic-auction/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,89 @@
// Find all our documentation at https://docs.near.org
use near_sdk::json_types::{U128, U64};
use near_sdk::{env, near, require, AccountId, Gas, NearToken, PanicOnDefault};

pub mod ext;
pub use crate::ext::*;
use near_sdk::json_types::U64;
use near_sdk::{env, near,require, AccountId, NearToken, PanicOnDefault, Promise};

#[near(serializers = [json, borsh])]
#[derive(Clone)]
pub struct Bid {
pub bidder: AccountId,
pub bid: U128,
pub bid: NearToken,
}

pub type TokenId = String;

#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct Contract {
highest_bid: Bid,
auction_end_time: U64,
auctioneer: AccountId,
auction_was_claimed: bool,
ft_contract: AccountId,
nft_contract: AccountId,
token_id: TokenId,
}

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

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

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

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

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

ft_contract::ext(self.ft_contract.clone())
.with_attached_deposit(NearToken::from_yoctonear(1))
.with_static_gas(Gas::from_tgas(30))
.ft_transfer(auctioneer, 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());
}

pub fn ft_on_transfer(&mut self, sender_id: AccountId, amount: U128, msg: String) -> U128 {
#[payable]
pub fn bid(&mut self) -> Promise {
// Assert the auction is still ongoing
require!(
env::block_timestamp() < self.auction_end_time.into(),
"Auction has ended"
);

let ft = env::predecessor_account_id();
require!(ft == self.ft_contract, "The token is not supported");
// current bid
let bid = env::attached_deposit();
let bidder = env::predecessor_account_id();

// 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!(bid > last_bid, "You must place a higher bid");

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

if last_bid > U128(0) {
ft_contract::ext(self.ft_contract.clone())
.with_attached_deposit(NearToken::from_yoctonear(1))
.with_static_gas(Gas::from_tgas(30))
.ft_transfer(last_bidder, last_bid);
}
// Transfer tokens back to the last bidder
Promise::new(last_bidder).transfer(last_bid)
}

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

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

/*
* 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 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));
}
}
Loading

0 comments on commit 9024875

Please sign in to comment.