-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1ac8ce
commit ce82065
Showing
40 changed files
with
1,673 additions
and
326 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
contract-rs/src/lib.rs → contract-rs/01-basic-auction/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[package] | ||
name = "contract-rs" | ||
description = "Hello Near Example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
# TODO: Fill out the repository field to help NEAR ecosystem tools to discover your project. | ||
# NEP-0330 is automatically implemented for all contracts built with https://github.com/near/cargo-near. | ||
# Link to the repository will be available via `contract_source_metadata` view-function. | ||
#repository = "https://github.com/xxx/xxx" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[dependencies] | ||
near-sdk = { version = "5.1.0", features = ["legacy"] } | ||
chrono = "0.4.38" | ||
tracing = "0.1" | ||
tracing-subscriber = "0.3" | ||
|
||
[dev-dependencies] | ||
near-sdk = { version = "5.1.0", features = ["unit-testing"] } | ||
near-workspaces = { version = "0.10.0", features = ["unstable"] } | ||
tokio = { version = "1.12.0", features = ["full"] } | ||
serde_json = "1.0.117" | ||
|
||
[profile.release] | ||
codegen-units = 1 | ||
# Tell `rustc` to optimize for small code size. | ||
opt-level = "z" | ||
lto = true | ||
debug = false | ||
panic = "abort" | ||
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801 | ||
overflow-checks = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# contract-rs | ||
|
||
cargo-near-new-project-description | ||
|
||
## How to Build Locally? | ||
|
||
Install [`cargo-near`](https://github.com/near/cargo-near) and run: | ||
|
||
```bash | ||
cargo near build | ||
``` | ||
|
||
## How to Test Locally? | ||
|
||
```bash | ||
cargo test | ||
``` | ||
|
||
## How to Deploy? | ||
|
||
To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run: | ||
|
||
```bash | ||
# Create a new account | ||
cargo near create-dev-account | ||
|
||
# Deploy the contract on it | ||
cargo near deploy <account-id> | ||
``` | ||
## Useful Links | ||
|
||
- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust | ||
- [near CLI](https://near.cli.rs) - Iteract with NEAR blockchain from command line | ||
- [NEAR Rust SDK Documentation](https://docs.near.org/sdk/rust/introduction) | ||
- [NEAR Documentation](https://docs.near.org) | ||
- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol) | ||
- [NEAR Discord](https://near.chat) | ||
- [NEAR Telegram Developers Community Group](https://t.me/neardev) | ||
- NEAR DevHub: [Telegram](https://t.me/neardevhub), [Twitter](https://twitter.com/neardevhub) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[toolchain] | ||
channel = "stable" | ||
components = ["rustfmt"] | ||
targets = ["wasm32-unknown-unknown"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Find all our documentation at https://docs.near.org | ||
use near_sdk::json_types::U128; | ||
use near_sdk::{ext_contract, AccountId}; | ||
|
||
use crate::TokenId; | ||
|
||
// Validator interface, for cross-contract calls | ||
#[ext_contract(ft_contract)] | ||
trait FT { | ||
fn ft_transfer(&self, receiver_id: AccountId, amount: U128) -> String; | ||
} | ||
|
||
#[ext_contract(nft_contract)] | ||
trait NFT { | ||
fn nft_transfer(&self, receiver_id: AccountId, token_id: TokenId) -> String; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// 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::*; | ||
|
||
#[near(serializers = [json, borsh])] | ||
#[derive(Clone)] | ||
pub struct Bid { | ||
pub bidder: AccountId, | ||
pub bid: U128, | ||
} | ||
|
||
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 { | ||
Self { | ||
highest_bid: Bid { | ||
bidder: env::current_account_id(), | ||
bid: U128(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 { | ||
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"); | ||
|
||
let Bid { | ||
bidder: last_bidder, | ||
bid: last_bid, | ||
} = self.highest_bid.clone(); | ||
|
||
require!(amount >= last_bid, "You must place a higher bid"); | ||
|
||
self.highest_bid = Bid { | ||
bidder: sender_id, | ||
bid: amount, | ||
}; | ||
|
||
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); | ||
} | ||
|
||
U128(0) | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.