Skip to content

Commit

Permalink
refactor: migrate to a external near-account-id crate for reusable Ac…
Browse files Browse the repository at this point in the history
…countId type (near#1108)
  • Loading branch information
iho authored Nov 7, 2023
1 parent 24f9127 commit a780010
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 207 deletions.
2 changes: 1 addition & 1 deletion examples/fungible-token/test-contract-defi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl FungibleTokenReceiver for DeFi {
env::predecessor_account_id() == self.fungible_token_account_id,
"Only supports the one fungible token contract"
);
log!("in {} tokens from @{} ft_on_transfer, msg = {}", amount.0, sender_id.as_ref(), msg);
log!("in {} tokens from @{} ft_on_transfer, msg = {}", amount.0, sender_id, msg);
match msg.as_str() {
"take-my-money" => PromiseOrValue::Value(U128::from(0)),
_ => {
Expand Down
1 change: 1 addition & 0 deletions near-contract-standards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ near-sdk = { path = "../near-sdk", version = "~4.1.1", default-features = false,
serde = "1"
serde_json = "1"
schemars = "0.8"
near-account-id = {version="1.0.0-alpha.3", features = ["abi", "serde", "borsh", "schemars"]}

[features]
default = ["abi"]
Expand Down
2 changes: 1 addition & 1 deletion near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl FungibleToken {

fn measure_account_storage_usage(&mut self) {
let initial_storage_usage = env::storage_usage();
let tmp_account_id = AccountId::new_unchecked("a".repeat(64));
let tmp_account_id = "a".repeat(64).parse().unwrap();
self.accounts.insert(&tmp_account_id, &0u128);
self.account_storage_usage = env::storage_usage() - initial_storage_usage;
self.accounts.remove(&tmp_account_id);
Expand Down
4 changes: 2 additions & 2 deletions near-contract-standards/src/fungible_token/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ mod tests {
use near_sdk::{test_utils, AccountId};

fn bob() -> AccountId {
AccountId::new_unchecked("bob".to_string())
"bob".parse().unwrap()
}

fn alice() -> AccountId {
AccountId::new_unchecked("alice".to_string())
"alice".parse().unwrap()
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl NonFungibleToken {
let initial_storage_usage = env::storage_usage();
// 64 Length because this is the max account id length
let tmp_token_id = "a".repeat(64);
let tmp_owner_id = AccountId::new_unchecked("a".repeat(64));
let tmp_owner_id = "a".repeat(64).parse().unwrap();

// 1. set some dummy data
self.owner_by_id.insert(&tmp_token_id, &tmp_owner_id);
Expand Down
4 changes: 2 additions & 2 deletions near-contract-standards/src/non_fungible_token/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ mod tests {
use near_sdk::{test_utils, AccountId};

fn bob() -> AccountId {
AccountId::new_unchecked("bob".to_string())
"bob".parse().unwrap()
}

fn alice() -> AccountId {
AccountId::new_unchecked("alice".to_string())
"alice".parse().unwrap()
}

#[test]
Expand Down
8 changes: 2 additions & 6 deletions near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ once_cell = { version = "1.17", default-features = false }

near-abi = { version = "0.4.0", features = ["__chunked-entries"], optional = true }
near-gas = { version = "0.2.3", features = ["serde", "borsh", "schemars"] }
near-account-id = {version="1.0.0-alpha.3", features = ["abi", "serde", "borsh", "schemars"]}

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
near-vm-logic = { version = "0.17", optional = true }
Expand All @@ -61,12 +62,7 @@ expensive-debug = []
unstable = []
legacy = []
abi = ["borsh/unstable__schema", "near-abi", "schemars", "near-sdk-macros/abi"]
unit-testing = [
"near-vm-logic",
"near-primitives-core",
"near-primitives",
"near-crypto",
]
unit-testing = ["near-vm-logic", "near-primitives-core", "near-primitives", "near-crypto"]

__abi-embed = ["near-sdk-macros/__abi-embed"]
__abi-generate = ["abi", "near-sdk-macros/__abi-generate"]
Expand Down
2 changes: 1 addition & 1 deletion near-sdk/src/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ pub fn promise_and(promise_indices: &[PromiseIndex]) -> PromiseIndex {
}

pub fn promise_batch_create(account_id: &AccountId) -> PromiseIndex {
let account_id = account_id.as_ref();
let account_id: &str = account_id.as_ref();
unsafe {
PromiseIndex(sys::promise_batch_create(account_id.len() as _, account_id.as_ptr() as _))
}
Expand Down
16 changes: 0 additions & 16 deletions near-sdk/src/environment/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ mod receipt;
pub(crate) use self::external::SdkExternal;
pub use self::mocked_blockchain::MockedBlockchain;
pub use self::receipt::{Receipt, VmAction};
use crate::AccountId;
use core::cell::RefCell;
use near_primitives_core::account::id::ParseAccountError;

thread_local! {
/// Low-level blockchain interface wrapped by the environment. Prefer using `env::*` and
Expand All @@ -25,17 +23,3 @@ where
{
BLOCKCHAIN_INTERFACE.with(|b| f(&mut b.borrow_mut()))
}

impl From<near_vm_logic::types::AccountId> for AccountId {
fn from(id: near_vm_logic::types::AccountId) -> Self {
Self::new_unchecked(String::from(id))
}
}

impl std::convert::TryFrom<AccountId> for near_vm_logic::types::AccountId {
type Error = ParseAccountError;

fn try_from(value: AccountId) -> Result<Self, Self::Error> {
value.as_str().parse()
}
}
4 changes: 1 addition & 3 deletions near-sdk/src/test_utils/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use std::convert::TryInto;

/// Returns a pre-defined account_id from a list of 6.
pub fn accounts(id: usize) -> AccountId {
AccountId::new_unchecked(
["alice", "bob", "charlie", "danny", "eugene", "fargo"][id].to_string(),
)
["alice", "bob", "charlie", "danny", "eugene", "fargo"][id].parse().unwrap()
}

/// Simple VMContext builder that allows to quickly create custom context in tests.
Expand Down
6 changes: 3 additions & 3 deletions near-sdk/src/test_utils/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use crate::test_utils::VMContextBuilder;
use crate::{testing_env, AccountId, VMConfig};

pub fn alice() -> AccountId {
AccountId::new_unchecked("alice.near".to_string())
"alice.near".parse().unwrap()
}

pub fn bob() -> AccountId {
AccountId::new_unchecked("bob.near".to_string())
"bob.near".parse().unwrap()
}

pub fn carol() -> AccountId {
AccountId::new_unchecked("carol.near".to_string())
"carol.near".parse().unwrap()
}

/// Updates the blockchain interface with the config passed in.
Expand Down
169 changes: 0 additions & 169 deletions near-sdk/src/types/account_id.rs

This file was deleted.

3 changes: 1 addition & 2 deletions near-sdk/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ pub use self::public_key::{CurveType, PublicKey};
mod primitives;
pub use self::primitives::*;

mod account_id;
pub use self::account_id::{AccountId, ParseAccountIdError};
pub use near_account_id::AccountId;

pub use near_gas::NearGas as Gas;

Expand Down

0 comments on commit a780010

Please sign in to comment.