create-near-app
Version:
Quickly scaffold your dApp on NEAR Blockchain
72 lines (58 loc) • 1.97 kB
text/typescript
// Find all our documentation at https://docs.near.org
import { NearBindgen, near, call, view, AccountId, NearPromise, initialize, assert } from "near-sdk-js";
class Bid {
bidder: AccountId;
bid: bigint;
}
class AuctionContract {
highest_bid: Bid = { bidder: '', bid: BigInt(0) };
auction_end_time: bigint = BigInt(0);
auctioneer: AccountId = "";
claimed: boolean = false;
init({ end_time, auctioneer}: { end_time: bigint, auctioneer: AccountId}) {
this.auction_end_time = end_time;
this.highest_bid = { bidder: near.currentAccountId(), bid: BigInt(1) };
this.auctioneer = auctioneer;
}
bid(): NearPromise {
// Assert the auction is still ongoing
assert(this.auction_end_time > near.blockTimestamp(), "Auction has ended");
// Current bid
const bid = near.attachedDeposit();
const bidder = near.predecessorAccountId();
// Last bid
const { bidder: lastBidder, bid: lastBid } = this.highest_bid;
// Check if the deposit is higher than the current bid
assert(bid > lastBid, "You must place a higher bid");
// Update the highest bid
this.highest_bid = { bidder, bid }; // Save the new bid
// Transfer tokens back to the last bidder
return NearPromise.new(lastBidder).transfer(lastBid);
}
claim() {
assert(this.auction_end_time <= near.blockTimestamp(), "Auction has not ended yet");
assert(!this.claimed, "Auction has been claimed");
this.claimed = true;
return NearPromise.new(this.auctioneer).transfer(this.highest_bid.bid)
}
get_highest_bid(): Bid {
return this.highest_bid;
}
get_auction_end_time(): BigInt {
return this.auction_end_time;
}
get_auctioneer(): AccountId {
return this.auctioneer;
}
get_claimed(): boolean {
return this.claimed;
}
}