Skip to content

Commit

Permalink
Merge pull request #66 from garikbesson/migrate-and-reorganize
Browse files Browse the repository at this point in the history
Fix u/U/NearToken types
  • Loading branch information
bucanero authored Jun 24, 2024
2 parents 84a5d40 + a7b457f commit be5d06b
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 74 deletions.
2 changes: 1 addition & 1 deletion market-contract/src/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait ExtContract {
the price that the token was purchased for. This will be used in conjunction with the royalty percentages
for the token in order to determine how much money should go to which account.
*/
balance: U128,
balance: NearToken,
//the maximum amount of accounts the market can payout at once (this is limited by GAS)
max_len_payout: u32,
);
Expand Down
10 changes: 5 additions & 5 deletions market-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub type ContractAndTokenId = String;
#[derive(Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct Payout {
pub payout: HashMap<AccountId, U128>,
pub payout: HashMap<AccountId, NearToken>,
}

//main contract struct to store all the information
Expand Down Expand Up @@ -176,13 +176,13 @@ impl Contract {

/// views
//return the minimum storage for 1 sale
pub fn storage_minimum_balance(&self) -> U128 {
U128(storage_per_sale().as_yoctonear())
pub fn storage_minimum_balance(&self) -> NearToken {
storage_per_sale()
}

//return how much storage an account has paid for
pub fn storage_balance_of(&self, account_id: AccountId) -> U128 {
U128(self.storage_deposits.get(&account_id).unwrap_or(ZERO_NEAR).as_yoctonear())
pub fn storage_balance_of(&self, account_id: AccountId) -> NearToken {
self.storage_deposits.get(&account_id).unwrap_or(ZERO_NEAR)
}
}

Expand Down
26 changes: 13 additions & 13 deletions market-contract/src/sale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ impl Contract {
//we need to enforce that the user has enough storage for 1 EXTRA sale.

//get the storage for a sale
let storage_amount = self.storage_minimum_balance().0;
let storage_amount = self.storage_minimum_balance();
//get the total storage paid by the owner
let owner_paid_storage = self.storage_deposits.get(&owner_id).unwrap_or(ZERO_NEAR);
//get the storage required which is simply the storage for the number of sales they have + 1
let signer_storage_required = storage_amount.saturating_mul((self.get_supply_by_owner_id(owner_id.clone()).0 + 1).into());

//make sure that the total paid is >= the required storage
assert!(
owner_paid_storage.ge(&NearToken::from_yoctonear(signer_storage_required)),
owner_paid_storage.ge(&signer_storage_required),
"Insufficient storage paid: {}, for {} sales at {} rate of per sale",
owner_paid_storage, signer_storage_required.saturating_div(storage_per_sale().as_yoctonear()), storage_per_sale()
);
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Contract {
&mut self,
nft_contract_id: AccountId,
token_id: String,
price: U128,
price: NearToken,
) {
//assert that the user has attached exactly 1 yoctoNEAR (for security reasons)
assert_one_yocto();
Expand All @@ -112,7 +112,7 @@ impl Contract {
);

//set the sale conditions equal to the passed in price
sale.sale_conditions = NearToken::from_yoctonear(price.0);
sale.sale_conditions = price;
//insert the sale back into the map for the unique sale ID
self.sales.insert(&contract_and_token_id, &sale);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ impl Contract {
self.process_purchase(
contract_id,
token_id,
U128(deposit.as_yoctonear()),
deposit,
buyer_id,
);
}
Expand All @@ -157,7 +157,7 @@ impl Contract {
&mut self,
nft_contract_id: AccountId,
token_id: String,
price: U128,
price: NearToken,
buyer_id: AccountId,
) -> Promise {
//get the sale object by removing the sale
Expand Down Expand Up @@ -203,8 +203,8 @@ impl Contract {
pub fn resolve_purchase(
&mut self,
buyer_id: AccountId,
price: U128,
) -> U128 {
price: NearToken,
) -> NearToken {
// checking for payout information returned from the nft_transfer_payout method
let payout_option = promise_result_as_success().and_then(|value| {
//if we set the payout_option to None, that means something went wrong and we should refund the buyer
Expand All @@ -221,17 +221,17 @@ impl Contract {
//if the payout object is the correct length, we move forward
} else {
//we'll keep track of how much the nft contract wants us to payout. Starting at the full price payed by the buyer
let mut remainder = price.0;
let mut remainder = price;

//loop through the payout and subtract the values from the remainder.
for &value in payout_object.payout.values() {
//checked sub checks for overflow or any errors and returns None if there are problems
remainder = remainder.checked_sub(value.0)?;
remainder = remainder.checked_sub(value)?;
}
//Check to see if the NFT contract sent back a faulty payout that requires us to pay more or too little.
//The remainder will be 0 if the payout summed to the total price. The remainder will be 1 if the royalties
//we something like 3333 + 3333 + 3333.
if remainder == 0 || remainder == 1 {
if remainder.eq(&ZERO_NEAR) || remainder.eq(&NearToken::from_yoctonear(1)) {
//set the payout_option to be the payout because nothing went wrong
Some(payout_object.payout)
} else {
Expand All @@ -247,14 +247,14 @@ impl Contract {
payout_option
//if the payout option was None, we refund the buyer for the price they payed and return
} else {
Promise::new(buyer_id).transfer(NearToken::from_yoctonear(u128::from(price)));
Promise::new(buyer_id).transfer(price);
// leave function and return the price that was refunded
return price;
};

// NEAR payouts
for (receiver_id, amount) in payout {
Promise::new(receiver_id).transfer(NearToken::from_yoctonear(amount.0));
Promise::new(receiver_id).transfer(amount);
}

//return the price payout out
Expand Down
6 changes: 3 additions & 3 deletions market-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn test_storage_balance_of() {
.build());
contract.storage_deposit(Some(accounts(0)));
let balance = contract.storage_balance_of(accounts(0));
assert_eq!(balance, U128(MIN_REQUIRED_STORAGE_YOCTO.as_yoctonear()));
assert_eq!(balance, MIN_REQUIRED_STORAGE_YOCTO);
}

#[test]
Expand All @@ -98,7 +98,7 @@ fn test_storage_withdraw() {
contract.storage_withdraw();

let remaining_amount = contract.storage_balance_of(accounts(0));
assert_eq!(remaining_amount, U128(0))
assert_eq!(remaining_amount, NearToken::from_yoctonear(0))
}

#[test]
Expand Down Expand Up @@ -195,7 +195,7 @@ fn test_update_price() {
.attached_deposit(ONE_YOCTONEAR)
.predecessor_account_id(accounts(0)) // bob to buy NFT from alice
.build());
contract.update_price(nft_contract_id, token_id, U128(new_price.as_yoctonear()));
contract.update_price(nft_contract_id, token_id, new_price);

// test update price success
let sale = contract.sales.get(&contract_and_token_id).expect("No sale");
Expand Down
2 changes: 1 addition & 1 deletion nft-contract-approval/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub type TokenId = String;
#[derive(Serialize, Deserialize, NearSchema)]
#[serde(crate = "near_sdk::serde")]
pub struct Payout {
pub payout: HashMap<AccountId, U128>,
pub payout: HashMap<AccountId, NearToken>,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, NearSchema)]
Expand Down
8 changes: 4 additions & 4 deletions nft-contract-approval/src/royalty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::*;

pub trait NonFungibleTokenCore {
//calculates the payout for a token given the passed in balance. This is a view method
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout;
fn nft_payout(&self, token_id: TokenId, balance: NearToken, max_len_payout: u32) -> Payout;

//transfers the token to the receiver ID and returns the payout object that should be payed given the passed in balance.
fn nft_transfer_payout(
Expand All @@ -11,7 +11,7 @@ pub trait NonFungibleTokenCore {
token_id: TokenId,
approval_id: u32,
memo: Option<String>,
balance: U128,
balance: NearToken,
max_len_payout: u32,
) -> Payout;
}
Expand All @@ -20,7 +20,7 @@ pub trait NonFungibleTokenCore {
impl NonFungibleTokenCore for Contract {

//calculates the payout for a token given the passed in balance. This is a view method
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout {
fn nft_payout(&self, token_id: TokenId, balance: NearToken, max_len_payout: u32) -> Payout {
/*
FILL THIS IN
*/
Expand All @@ -35,7 +35,7 @@ impl NonFungibleTokenCore for Contract {
token_id: TokenId,
approval_id: u32,
memo: Option<String>,
balance: U128,
balance: NearToken,
max_len_payout: u32,
) -> Payout {
/*
Expand Down
2 changes: 1 addition & 1 deletion nft-contract-basic/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub type TokenId = String;
#[derive(Serialize, Deserialize, NearSchema)]
#[serde(crate = "near_sdk::serde")]
pub struct Payout {
pub payout: HashMap<AccountId, U128>,
pub payout: HashMap<AccountId, NearToken>,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, NearSchema)]
Expand Down
8 changes: 4 additions & 4 deletions nft-contract-basic/src/royalty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::*;

pub trait NonFungibleTokenCore {
//calculates the payout for a token given the passed in balance. This is a view method
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout;
fn nft_payout(&self, token_id: TokenId, balance: NearToken, max_len_payout: u32) -> Payout;

//transfers the token to the receiver ID and returns the payout object that should be payed given the passed in balance.
fn nft_transfer_payout(
Expand All @@ -11,7 +11,7 @@ pub trait NonFungibleTokenCore {
token_id: TokenId,
approval_id: u32,
memo: Option<String>,
balance: U128,
balance: NearToken,
max_len_payout: u32,
) -> Payout;
}
Expand All @@ -20,7 +20,7 @@ pub trait NonFungibleTokenCore {
impl NonFungibleTokenCore for Contract {

//calculates the payout for a token given the passed in balance. This is a view method
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout {
fn nft_payout(&self, token_id: TokenId, balance: NearToken, max_len_payout: u32) -> Payout {
/*
FILL THIS IN
*/
Expand All @@ -35,7 +35,7 @@ impl NonFungibleTokenCore for Contract {
token_id: TokenId,
approval_id: u32,
memo: Option<String>,
balance: U128,
balance: NearToken,
max_len_payout: u32,
) -> Payout {
/*
Expand Down
2 changes: 1 addition & 1 deletion nft-contract-events/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub type TokenId = String;
#[derive(Serialize, Deserialize, NearSchema)]
#[serde(crate = "near_sdk::serde")]
pub struct Payout {
pub payout: HashMap<AccountId, U128>,
pub payout: HashMap<AccountId, NearToken>,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, NearSchema)]
Expand Down
8 changes: 4 additions & 4 deletions nft-contract-events/src/royalty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::*;

pub trait NonFungibleTokenCore {
//calculates the payout for a token given the passed in balance. This is a view method
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout;
fn nft_payout(&self, token_id: TokenId, balance: NearToken, max_len_payout: u32) -> Payout;

//transfers the token to the receiver ID and returns the payout object that should be payed given the passed in balance.
fn nft_transfer_payout(
Expand All @@ -11,7 +11,7 @@ pub trait NonFungibleTokenCore {
token_id: TokenId,
approval_id: u32,
memo: Option<String>,
balance: U128,
balance: NearToken,
max_len_payout: u32,
) -> Payout;
}
Expand All @@ -20,7 +20,7 @@ pub trait NonFungibleTokenCore {
impl NonFungibleTokenCore for Contract {

//calculates the payout for a token given the passed in balance. This is a view method
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout {
fn nft_payout(&self, token_id: TokenId, balance: NearToken, max_len_payout: u32) -> Payout {
/*
FILL THIS IN
*/
Expand All @@ -35,7 +35,7 @@ impl NonFungibleTokenCore for Contract {
token_id: TokenId,
approval_id: u32,
memo: Option<String>,
balance: U128,
balance: NearToken,
max_len_payout: u32,
) -> Payout {
/*
Expand Down
4 changes: 2 additions & 2 deletions nft-contract-royalty/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use near_sdk::{CryptoHash};
use std::mem::size_of;

//convert the royalty percentage and amount to pay into a payout
pub(crate) fn royalty_to_payout(royalty_percentage: u128, amount_to_pay: u128) -> U128 {
U128(amount_to_pay.saturating_mul(royalty_percentage).saturating_div(10000))
pub(crate) fn royalty_to_payout(royalty_percentage: u128, amount_to_pay: NearToken) -> NearToken {
amount_to_pay.saturating_mul(royalty_percentage).saturating_div(10000)
}

//calculate how many bytes the account ID is taking up
Expand Down
2 changes: 1 addition & 1 deletion nft-contract-royalty/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub type TokenId = String;
#[derive(Serialize, Deserialize, NearSchema)]
#[serde(crate = "near_sdk::serde")]
pub struct Payout {
pub payout: HashMap<AccountId, U128>,
pub payout: HashMap<AccountId, NearToken>,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, NearSchema)]
Expand Down
Loading

0 comments on commit be5d06b

Please sign in to comment.