Skip to content

Commit be5d06b

Browse files
authored
Merge pull request #66 from garikbesson/migrate-and-reorganize
Fix u/U/NearToken types
2 parents 84a5d40 + a7b457f commit be5d06b

19 files changed

Lines changed: 66 additions & 74 deletions

File tree

market-contract/src/external.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait ExtContract {
1616
the price that the token was purchased for. This will be used in conjunction with the royalty percentages
1717
for the token in order to determine how much money should go to which account.
1818
*/
19-
balance: U128,
19+
balance: NearToken,
2020
//the maximum amount of accounts the market can payout at once (this is limited by GAS)
2121
max_len_payout: u32,
2222
);

market-contract/src/lib.rs

Lines changed: 5 additions & 5 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, U128>,
42+
pub payout: HashMap<AccountId, NearToken>,
4343
}
4444

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

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

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

market-contract/src/sale.rs

Lines changed: 13 additions & 13 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().0;
46+
let storage_amount = self.storage_minimum_balance();
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(&NearToken::from_yoctonear(signer_storage_required)),
54+
owner_paid_storage.ge(&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: U128,
95+
price: NearToken,
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 = NearToken::from_yoctonear(price.0);
115+
sale.sale_conditions = price;
116116
//insert the sale back into the map for the unique sale ID
117117
self.sales.insert(&contract_and_token_id, &sale);
118118
}
@@ -145,7 +145,7 @@ impl Contract {
145145
self.process_purchase(
146146
contract_id,
147147
token_id,
148-
U128(deposit.as_yoctonear()),
148+
deposit,
149149
buyer_id,
150150
);
151151
}
@@ -157,7 +157,7 @@ impl Contract {
157157
&mut self,
158158
nft_contract_id: AccountId,
159159
token_id: String,
160-
price: U128,
160+
price: NearToken,
161161
buyer_id: AccountId,
162162
) -> Promise {
163163
//get the sale object by removing the sale
@@ -203,8 +203,8 @@ impl Contract {
203203
pub fn resolve_purchase(
204204
&mut self,
205205
buyer_id: AccountId,
206-
price: U128,
207-
) -> U128 {
206+
price: NearToken,
207+
) -> NearToken {
208208
// checking for payout information returned from the nft_transfer_payout method
209209
let payout_option = promise_result_as_success().and_then(|value| {
210210
//if we set the payout_option to None, that means something went wrong and we should refund the buyer
@@ -221,17 +221,17 @@ impl Contract {
221221
//if the payout object is the correct length, we move forward
222222
} else {
223223
//we'll keep track of how much the nft contract wants us to payout. Starting at the full price payed by the buyer
224-
let mut remainder = price.0;
224+
let mut remainder = price;
225225

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.0)?;
229+
remainder = remainder.checked_sub(value)?;
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
233233
//we something like 3333 + 3333 + 3333.
234-
if remainder == 0 || remainder == 1 {
234+
if remainder.eq(&ZERO_NEAR) || remainder.eq(&NearToken::from_yoctonear(1)) {
235235
//set the payout_option to be the payout because nothing went wrong
236236
Some(payout_object.payout)
237237
} else {
@@ -247,14 +247,14 @@ impl Contract {
247247
payout_option
248248
//if the payout option was None, we refund the buyer for the price they payed and return
249249
} else {
250-
Promise::new(buyer_id).transfer(NearToken::from_yoctonear(u128::from(price)));
250+
Promise::new(buyer_id).transfer(price);
251251
// leave function and return the price that was refunded
252252
return price;
253253
};
254254

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

260260
//return the price payout out

market-contract/src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn test_storage_balance_of() {
7272
.build());
7373
contract.storage_deposit(Some(accounts(0)));
7474
let balance = contract.storage_balance_of(accounts(0));
75-
assert_eq!(balance, U128(MIN_REQUIRED_STORAGE_YOCTO.as_yoctonear()));
75+
assert_eq!(balance, MIN_REQUIRED_STORAGE_YOCTO);
7676
}
7777

7878
#[test]
@@ -98,7 +98,7 @@ fn test_storage_withdraw() {
9898
contract.storage_withdraw();
9999

100100
let remaining_amount = contract.storage_balance_of(accounts(0));
101-
assert_eq!(remaining_amount, U128(0))
101+
assert_eq!(remaining_amount, NearToken::from_yoctonear(0))
102102
}
103103

104104
#[test]
@@ -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, U128(new_price.as_yoctonear()));
198+
contract.update_price(nft_contract_id, token_id, new_price);
199199

200200
// test update price success
201201
let sale = contract.sales.get(&contract_and_token_id).expect("No sale");

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, U128>,
7+
pub payout: HashMap<AccountId, NearToken>,
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: U128, max_len_payout: u32) -> Payout;
5+
fn nft_payout(&self, token_id: TokenId, balance: NearToken, 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: u32,
1313
memo: Option<String>,
14-
balance: U128,
14+
balance: NearToken,
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: U128, max_len_payout: u32) -> Payout {
23+
fn nft_payout(&self, token_id: TokenId, balance: NearToken, 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: u32,
3737
memo: Option<String>,
38-
balance: U128,
38+
balance: NearToken,
3939
max_len_payout: u32,
4040
) -> Payout {
4141
/*

nft-contract-basic/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, U128>,
7+
pub payout: HashMap<AccountId, NearToken>,
88
}
99

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

nft-contract-basic/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: U128, max_len_payout: u32) -> Payout;
5+
fn nft_payout(&self, token_id: TokenId, balance: NearToken, 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: u32,
1313
memo: Option<String>,
14-
balance: U128,
14+
balance: NearToken,
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: U128, max_len_payout: u32) -> Payout {
23+
fn nft_payout(&self, token_id: TokenId, balance: NearToken, 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: u32,
3737
memo: Option<String>,
38-
balance: U128,
38+
balance: NearToken,
3939
max_len_payout: u32,
4040
) -> Payout {
4141
/*

nft-contract-events/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, U128>,
7+
pub payout: HashMap<AccountId, NearToken>,
88
}
99

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

nft-contract-events/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: U128, max_len_payout: u32) -> Payout;
5+
fn nft_payout(&self, token_id: TokenId, balance: NearToken, 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: u32,
1313
memo: Option<String>,
14-
balance: U128,
14+
balance: NearToken,
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: U128, max_len_payout: u32) -> Payout {
23+
fn nft_payout(&self, token_id: TokenId, balance: NearToken, 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: u32,
3737
memo: Option<String>,
38-
balance: U128,
38+
balance: NearToken,
3939
max_len_payout: u32,
4040
) -> Payout {
4141
/*

0 commit comments

Comments
 (0)