Skip to content

Commit 51a2868

Browse files
committed
using U types for payouts
1 parent 04e517c commit 51a2868

36 files changed

Lines changed: 72 additions & 64 deletions
-207 KB
Binary file not shown.

market-contract/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub type ContractAndTokenId = String;
3939
#[derive(Serialize, Deserialize)]
4040
#[serde(crate = "near_sdk::serde")]
4141
pub struct Payout {
42-
pub payout: HashMap<AccountId, NearToken>,
42+
pub payout: HashMap<AccountId, U128>,
4343
}
4444

4545
//main contract struct to store all the information
@@ -176,8 +176,8 @@ impl Contract {
176176

177177
/// views
178178
//return the minimum storage for 1 sale
179-
pub fn storage_minimum_balance(&self) -> NearToken {
180-
storage_per_sale()
179+
pub fn storage_minimum_balance(&self) -> U128 {
180+
U128(storage_per_sale().as_yoctonear())
181181
}
182182

183183
//return how much storage an account has paid for

market-contract/src/sale.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ impl Contract {
4343
//we need to enforce that the user has enough storage for 1 EXTRA sale.
4444

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

5252
//make sure that the total paid is >= the required storage
5353
assert!(
54-
owner_paid_storage.ge(&signer_storage_required),
54+
owner_paid_storage.ge(&NearToken::from_yoctonear(signer_storage_required)),
5555
"Insufficient storage paid: {}, for {} sales at {} rate of per sale",
5656
owner_paid_storage, signer_storage_required.saturating_div(storage_per_sale().as_yoctonear()), storage_per_sale()
5757
);
@@ -92,7 +92,7 @@ impl Contract {
9292
&mut self,
9393
nft_contract_id: AccountId,
9494
token_id: String,
95-
price: NearToken,
95+
price: U128,
9696
) {
9797
//assert that the user has attached exactly 1 yoctoNEAR (for security reasons)
9898
assert_one_yocto();
@@ -112,7 +112,7 @@ impl Contract {
112112
);
113113

114114
//set the sale conditions equal to the passed in price
115-
sale.sale_conditions = price;
115+
sale.sale_conditions = NearToken::from_yoctonear(price.0);
116116
//insert the sale back into the map for the unique sale ID
117117
self.sales.insert(&contract_and_token_id, &sale);
118118
}
@@ -226,7 +226,7 @@ impl Contract {
226226
//loop through the payout and subtract the values from the remainder.
227227
for &value in payout_object.payout.values() {
228228
//checked sub checks for overflow or any errors and returns None if there are problems
229-
remainder = remainder.checked_sub(value.as_yoctonear())?;
229+
remainder = remainder.checked_sub(value.0)?;
230230
}
231231
//Check to see if the NFT contract sent back a faulty payout that requires us to pay more or too little.
232232
//The remainder will be 0 if the payout summed to the total price. The remainder will be 1 if the royalties
@@ -254,7 +254,7 @@ impl Contract {
254254

255255
// NEAR payouts
256256
for (receiver_id, amount) in payout {
257-
Promise::new(receiver_id).transfer(amount);
257+
Promise::new(receiver_id).transfer(NearToken::from_yoctonear(amount.0));
258258
}
259259

260260
//return the price payout out

market-contract/src/sale_views.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Contract {
4848
let keys = sales.as_vector();
4949

5050
//where to start pagination - if we have a from_index, we'll use that - otherwise start from 0 index
51-
let start = u128::from(from_index.unwrap_or(0));
51+
let start = from_index.unwrap_or(0);
5252

5353
//iterate through the keys vector
5454
keys.iter()

market-contract/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ fn test_update_price() {
195195
.attached_deposit(ONE_YOCTONEAR)
196196
.predecessor_account_id(accounts(0)) // bob to buy NFT from alice
197197
.build());
198-
contract.update_price(nft_contract_id, token_id, new_price);
198+
contract.update_price(nft_contract_id, token_id, U128(new_price.as_yoctonear()));
199199

200200
// test update price success
201201
let sale = contract.sales.get(&contract_and_token_id).expect("No sale");
-207 KB
Binary file not shown.

nft-contract-approval/src/enumeration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl Contract {
1111
//Query for nft tokens on the contract regardless of the owner using pagination
1212
pub fn nft_tokens(&self, from_index: Option<u128>, limit: Option<u64>) -> Vec<JsonToken> {
1313
//where to start pagination - if we have a from_index, we'll use that - otherwise start from 0 index
14-
let start = u128::from(from_index.unwrap_or(0));
14+
let start = from_index.unwrap_or(0);
1515

1616
//iterate through each token using an iterator
1717
self.token_metadata_by_id.keys()

nft-contract-approval/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use near_sdk::borsh::{BorshSerialize, BorshDeserialize};
33
use near_sdk::collections::{LazyOption, LookupMap, UnorderedMap, UnorderedSet};
4-
use near_sdk::json_types::{Base64VecU8, U64};
4+
use near_sdk::json_types::{Base64VecU8, U64, U128};
55
use near_sdk::serde::{Deserialize, Serialize};
66
use near_sdk::{
77
near_bindgen, env, AccountId, NearToken, CryptoHash, PanicOnDefault, Promise, PromiseOrValue, BorshStorageKey, NearSchema

nft-contract-approval/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub type TokenId = String;
44
#[derive(Serialize, Deserialize, NearSchema)]
55
#[serde(crate = "near_sdk::serde")]
66
pub struct Payout {
7-
pub payout: HashMap<AccountId, NearToken>,
7+
pub payout: HashMap<AccountId, U128>,
88
}
99

1010
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, NearSchema)]

nft-contract-approval/src/royalty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::*;
22

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

77
//transfers the token to the receiver ID and returns the payout object that should be payed given the passed in balance.
88
fn nft_transfer_payout(
@@ -11,7 +11,7 @@ pub trait NonFungibleTokenCore {
1111
token_id: TokenId,
1212
approval_id: u64,
1313
memo: Option<String>,
14-
balance: NearToken,
14+
balance: U128,
1515
max_len_payout: u32,
1616
) -> Payout;
1717
}
@@ -20,7 +20,7 @@ pub trait NonFungibleTokenCore {
2020
impl NonFungibleTokenCore for Contract {
2121

2222
//calculates the payout for a token given the passed in balance. This is a view method
23-
fn nft_payout(&self, token_id: TokenId, balance: NearToken, max_len_payout: u32) -> Payout {
23+
fn nft_payout(&self, token_id: TokenId, balance: U128, max_len_payout: u32) -> Payout {
2424
/*
2525
FILL THIS IN
2626
*/
@@ -35,7 +35,7 @@ impl NonFungibleTokenCore for Contract {
3535
token_id: TokenId,
3636
approval_id: u64,
3737
memo: Option<String>,
38-
balance: NearToken,
38+
balance: U128,
3939
max_len_payout: u32,
4040
) -> Payout {
4141
/*

0 commit comments

Comments
 (0)