Skip to content

Commit

Permalink
chore: updated near-sdk to 5.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ committed Apr 2, 2024
1 parent 1fee5d9 commit 3256dc7
Show file tree
Hide file tree
Showing 12 changed files with 762 additions and 989 deletions.
1,570 changes: 646 additions & 924 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ async-trait = "0.1.74"
near-units = "0.2.0"
near-workspaces = "0.10.0"

near-sdk = { git = "https://github.com/sweatco/near-sdk-rs", rev = "8c48b26cc48d969c1e5f3162141fe9c824fccecd" }
near-contract-standards = { git = "https://github.com/sweatco/near-sdk-rs", rev = "8c48b26cc48d969c1e5f3162141fe9c824fccecd" }
near-sdk = { git = "https://github.com/sweatco/near-sdk-rs", rev = "d5e975e3b224b5758745ab46c25d586a8e0db473" }
near-contract-standards = { git = "https://github.com/sweatco/near-sdk-rs", rev = "d5e975e3b224b5758745ab46c25d586a8e0db473" }

nitka = "0.1.1"
nitka-proc = "0.1.1"
nitka = "0.2.2"
nitka-proc = "0.2.2"

sweat-model = { path = "model" }
4 changes: 3 additions & 1 deletion integration-tests/src/interface/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use near_sdk::AccountId;
use sweat_model::SweatContract;

Expand All @@ -7,6 +9,6 @@ pub(crate) trait ContractAccount {

impl ContractAccount for SweatContract<'_> {
fn account(&self) -> AccountId {
AccountId::new_unchecked(self.contract.as_account().id().to_string())
AccountId::from_str(&self.contract.as_account().id().to_string()).unwrap()
}
}
19 changes: 7 additions & 12 deletions integration-tests/src/measure/record_batch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#![cfg(test)]

use std::future::IntoFuture;

use anyhow::Result;
use near_workspaces::types::Gas;
use nitka::measure::outcome_storage::OutcomeStorage;
use sweat_model::SweatApiIntegration;

use crate::{prepare::IntegrationContext, prepare_contract};
Expand All @@ -24,15 +21,13 @@ async fn measure_record_batch() -> Result<Gas> {

let oracle = context.oracle().await?;

let (gas, _) = OutcomeStorage::measure_total(
&oracle,
context
.ft_contract()
.record_batch(Default::default())
.with_user(&oracle)
.into_future(),
)
.await?;
let gas = context
.ft_contract()
.record_batch(Default::default())
.with_user(&oracle)
.result()
.await?
.total_gas_burnt;

Ok(gas)
}
2 changes: 0 additions & 2 deletions model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ integration-api = ["dep:nitka", "dep:near-workspaces"]
release-api = []

[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
nitka-proc = { workspace = true }

near-sdk = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub trait SweatApi {
fn get_oracles(&self) -> Vec<AccountId>;
fn tge_mint(&mut self, account_id: &AccountId, amount: U128);
fn tge_mint_batch(&mut self, batch: Vec<(AccountId, U128)>);
fn burn(&mut self, amount: &U128);
fn burn(&mut self, amount: U128);
fn get_steps_since_tge(&self) -> U64;
fn record_batch(&mut self, steps_batch: Vec<(AccountId, u32)>);
fn formula(&self, steps_since_tge: U64, steps: u32) -> U128;
Expand Down Expand Up @@ -56,7 +56,7 @@ pub trait FungibleTokenCore {
pub trait StorageManagement {
// if `registration_only=true` MUST refund above the minimum balance if the account didn't exist and
// refund full deposit if the account exists.
#[deposit_yocto = near_sdk::env::storage_byte_cost() * 125]
#[deposit_yocto = near_sdk::env::storage_byte_cost().checked_mul(125).unwrap().as_yoctonear()]
fn storage_deposit(&mut self, account_id: Option<AccountId>, registration_only: Option<bool>) -> StorageBalance;

/// Withdraw specified amount of available Ⓝ for predecessor account.
Expand Down
Binary file modified res/sweat.wasm
Binary file not shown.
3 changes: 1 addition & 2 deletions sweat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ integration-api = ["sweat-model/integration-api"]
[dependencies]
sweat-model = { workspace = true }

static_assertions = "1.1.0"
near-sdk = { workspace = true }
near-sdk = { workspace = true, features = ["unit-testing"] }
near-contract-standards = { workspace = true }
60 changes: 60 additions & 0 deletions sweat/src/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use near_contract_standards::{
fungible_token::FungibleTokenCore,
storage_management::{StorageBalance, StorageBalanceBounds, StorageManagement},
};
use near_sdk::{json_types::U128, near_bindgen, AccountId, NearToken, PromiseOrValue};

use crate::{Contract, ContractExt};

#[near_bindgen]
impl FungibleTokenCore for Contract {
#[payable]
fn ft_transfer(&mut self, receiver_id: AccountId, amount: U128, memo: Option<String>) {
self.token.ft_transfer(receiver_id, amount, memo);
}

#[payable]
fn ft_transfer_call(
&mut self,
receiver_id: AccountId,
amount: U128,
memo: Option<String>,
msg: String,
) -> PromiseOrValue<U128> {
self.token.ft_transfer_call(receiver_id, amount, memo, msg)
}

fn ft_total_supply(&self) -> U128 {
self.token.ft_total_supply()
}

fn ft_balance_of(&self, account_id: AccountId) -> U128 {
self.token.ft_balance_of(account_id)
}
}

#[near_bindgen]
impl StorageManagement for Contract {
#[payable]
fn storage_deposit(&mut self, account_id: Option<AccountId>, registration_only: Option<bool>) -> StorageBalance {
self.token.storage_deposit(account_id, registration_only)
}

#[payable]
fn storage_withdraw(&mut self, amount: Option<NearToken>) -> StorageBalance {
self.token.storage_withdraw(amount)
}

#[payable]
fn storage_unregister(&mut self, force: Option<bool>) -> bool {
self.token.internal_storage_unregister(force).is_some()
}

fn storage_balance_bounds(&self) -> StorageBalanceBounds {
self.token.storage_balance_bounds()
}

fn storage_balance_of(&self, account_id: AccountId) -> Option<StorageBalance> {
self.token.storage_balance_of(account_id)
}
}
19 changes: 9 additions & 10 deletions sweat/src/defer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use near_contract_standards::fungible_token::events::FtMint;
use near_sdk::{
env, env::panic_str, ext_contract, is_promise_success, json_types::U128, near_bindgen, require, serde_json::json,
AccountId, Gas, Promise, PromiseOrValue,
AccountId, Gas, NearToken, Promise, PromiseOrValue,
};
use sweat_model::SweatDefer;

use crate::{internal_deposit, Contract, ContractExt};

const GAS_FOR_DEFER_CALLBACK: Gas = Gas(5 * Gas::ONE_TERA.0);
const GAS_FOR_DEFER: Gas = Gas(30 * Gas::ONE_TERA.0);
const GAS_FOR_DEFER_CALLBACK: Gas = Gas::from_tgas(5);
const GAS_FOR_DEFER: Gas = Gas::from_tgas(30);

#[near_bindgen]
impl SweatDefer for Contract {
Expand Down Expand Up @@ -40,16 +40,15 @@ impl SweatDefer for Contract {
"amounts": accounts_tokens,
});

let record_batch_for_hold_gas = Gas(env::prepaid_gas()
.0
.checked_sub(GAS_FOR_DEFER.0)
.unwrap_or_else(|| panic_str("Prepaid gas overflow")));
let record_batch_for_hold_gas = env::prepaid_gas()
.checked_sub(GAS_FOR_DEFER)
.unwrap_or_else(|| panic_str("Prepaid gas overflow"));

Promise::new(holding_account_id.clone())
.function_call(
"record_batch_for_hold".to_string(),
hold_arguments.to_string().into_bytes(),
0,
NearToken::from_yoctonear(0),
record_batch_for_hold_gas,
)
.then(
Expand Down Expand Up @@ -84,14 +83,14 @@ impl FungibleTokenTransferCallback for Contract {
internal_deposit(&mut self.token, &fee_account_id, fee.0);
events.push(FtMint {
owner_id: &fee_account_id,
amount: &fee,
amount: fee,
memo: None,
});

internal_deposit(&mut self.token, &receiver_id, amount.0);
events.push(FtMint {
owner_id: &receiver_id,
amount: &amount,
amount,
memo: None,
});

Expand Down
51 changes: 25 additions & 26 deletions sweat/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
#[macro_use]
extern crate static_assertions;

use near_contract_standards::fungible_token::{
events::{FtBurn, FtMint},
metadata::{FungibleTokenMetadata, FungibleTokenMetadataProvider},
FungibleToken,
Balance, FungibleToken,
};
use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
collections::UnorderedSet,
env,
json_types::{U128, U64},
near_bindgen, require, AccountId, Balance, PanicOnDefault, PromiseOrValue,
near, near_bindgen, require, AccountId, PanicOnDefault,
};
use sweat_model::{Payout, SweatApi};

mod core;
mod defer;
mod integration;
mod math;

#[near_bindgen]
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct Contract {
oracles: UnorderedSet<AccountId>,
token: FungibleToken,
Expand Down Expand Up @@ -67,7 +64,7 @@ impl SweatApi for Contract {
internal_deposit(&mut self.token, account_id, amount.0);
FtMint {
owner_id: account_id,
amount: &amount,
amount,
memo: None,
}
.emit();
Expand All @@ -79,13 +76,13 @@ impl SweatApi for Contract {
"Unauthorized access! Only token owner can do TGE!"
);
let mut events = Vec::with_capacity(batch.len());
for (account_id, steps_count) in &batch {
for (owner_id, steps_count) in &batch {
// let steps_count = steps_count.0;
internal_deposit(&mut self.token, account_id, steps_count.0);
internal_deposit(&mut self.token, owner_id, steps_count.0);

let event = FtMint {
owner_id: account_id,
amount: steps_count,
owner_id,
amount: *steps_count,
memo: None,
};
events.push(event);
Expand All @@ -95,7 +92,7 @@ impl SweatApi for Contract {
}
}

fn burn(&mut self, amount: &U128) {
fn burn(&mut self, amount: U128) {
self.token.internal_withdraw(&env::predecessor_account_id(), amount.0);
FtBurn {
amount,
Expand Down Expand Up @@ -129,15 +126,15 @@ impl SweatApi for Contract {
for i in 0..steps_batch.len() {
events.push(FtMint {
owner_id: &steps_batch[i].0,
amount: &sweats[i],
amount: sweats[i],
memo: None,
});
}

internal_deposit(&mut self.token, &env::predecessor_account_id(), oracle_fee.0);
let oracle_event = FtMint {
owner_id: &env::predecessor_account_id(),
amount: &oracle_fee,
amount: oracle_fee,
memo: None,
};
events.push(oracle_event);
Expand All @@ -159,9 +156,6 @@ impl Contract {
}
}

near_contract_standards::impl_fungible_token_core!(Contract, token);
near_contract_standards::impl_fungible_token_storage!(Contract, token);

/// Taken from contract standards but modified to default if account isn't initialized
/// rather than panicking:
/// <https://github.com/near/near-sdk-rs/blob/6596dc311036fe51d94358ac8f6497ef6e5a7cfc/near-contract-standards/src/fungible_token/core_impl.rs#L105>
Expand Down Expand Up @@ -196,11 +190,13 @@ impl FungibleTokenMetadataProvider for Contract {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use near_contract_standards::fungible_token::core::FungibleTokenCore;
use near_sdk::{
json_types::{U128, U64},
test_utils::VMContextBuilder,
testing_env, AccountId,
testing_env, AccountId, NearToken,
};
use sweat_model::SweatApi;

Expand All @@ -209,16 +205,19 @@ mod tests {
const EPS: f64 = 0.00001;

fn sweat_the_token() -> AccountId {
AccountId::new_unchecked("sweat_the_token".to_string())
AccountId::from_str("sweat_the_token").unwrap()
}

fn sweat_oracle() -> AccountId {
AccountId::new_unchecked("sweat_the_oracle".to_string())
AccountId::from_str("sweat_the_oracle").unwrap()
}

fn user1() -> AccountId {
AccountId::new_unchecked("sweat_user1".to_string())
AccountId::from_str("sweat_user1").unwrap()
}

fn user2() -> AccountId {
AccountId::new_unchecked("sweat_user2".to_string())
AccountId::from_str("sweat_user2").unwrap()
}

fn get_context(owner: AccountId, sender: AccountId) -> VMContextBuilder {
Expand All @@ -227,7 +226,7 @@ mod tests {
.current_account_id(owner.clone())
.signer_account_id(sender.clone())
.predecessor_account_id(sender)
.attached_deposit(1);
.attached_deposit(NearToken::from_yoctonear(1));
builder
}

Expand Down Expand Up @@ -380,7 +379,7 @@ mod tests {
token.add_oracle(&sweat_oracle());
token.tge_mint(&user1(), U128(9499999991723028480));
testing_env!(get_context(sweat_the_token(), user1()).build());
token.burn(&U128(9499999991723028480));
token.burn(U128(9499999991723028480));
assert!((0.0 - token.token.ft_balance_of(user1()).0 as f64 / 1e+18).abs() < EPS);
}

Expand Down
Loading

0 comments on commit 3256dc7

Please sign in to comment.