Skip to content

Commit

Permalink
feat: create folders
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenary committed Jun 5, 2024
1 parent c1ac8ce commit ce82065
Show file tree
Hide file tree
Showing 40 changed files with 1,673 additions and 326 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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, Promise};
use near_sdk::{env, near, require, AccountId, Gas, NearToken, PanicOnDefault};

pub mod ext;
pub use crate::ext::*;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions contract-rs/02-owner-claims-money/Cargo.toml
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
39 changes: 39 additions & 0 deletions contract-rs/02-owner-claims-money/README.md
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)
4 changes: 4 additions & 0 deletions contract-rs/02-owner-claims-money/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = ["rustfmt"]
targets = ["wasm32-unknown-unknown"]
16 changes: 16 additions & 0 deletions contract-rs/02-owner-claims-money/src/ext.rs
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;
}
110 changes: 110 additions & 0 deletions contract-rs/02-owner-claims-money/src/lib.rs
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.
Loading

0 comments on commit ce82065

Please sign in to comment.