Skip to content

Commit

Permalink
Merge branch 'near:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
g4titanx authored Aug 5, 2024
2 parents 7384a84 + f0b522f commit b78b206
Show file tree
Hide file tree
Showing 56 changed files with 396 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- os: macos-latest
rs: 1.76.0
- os: macos-latest
rs: 1.78.0
rs: stable
features: ['', '--features unstable,legacy,__abi-generate']
steps:
- uses: actions/checkout@v3
Expand Down
23 changes: 20 additions & 3 deletions .github/workflows/test_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ jobs:
matrix:
platform: [ubuntu-latest, macos-latest]
toolchain: [stable]
example: [cross-contract-calls, fungible-token]
example: [
adder,
callback-results,
cross-contract-calls,
factory-contract,
fungible-token,
non-fungible-token,
versioned
]
steps:
- uses: actions/checkout@v3
- name: "${{ matrix.toolchain }} with rustfmt, and wasm32"
Expand All @@ -26,9 +34,18 @@ jobs:
- uses: Swatinem/rust-cache@v1
with:
working-directory: ./examples/${{ matrix.example }}
- name: Build status-message
if: matrix.example == 'factory-contract'
env:
RUSTFLAGS: '-C link-arg=-s'
run: |
cargo +${{ matrix.toolchain }} build --manifest-path="./examples/status-message/Cargo.toml" --target wasm32-unknown-unknown --release --all &&
cp ./examples/status-message/target/wasm32-unknown-unknown/release/*.wasm ./examples/status-message/res/
- name: Build
env:
RUSTFLAGS: '-C link-arg=-s'
run: cargo +${{ matrix.toolchain }} build --manifest-path=./examples/${{matrix.example}}/Cargo.toml --target wasm32-unknown-unknown --release --all && cp ./examples/${{matrix.example}}/target/wasm32-unknown-unknown/release/*.wasm ./examples/${{matrix.example}}/res/
run: |
cargo +${{ matrix.toolchain }} build --manifest-path="./examples/${{matrix.example}}/Cargo.toml" --target wasm32-unknown-unknown --release --all &&
cp ./examples/${{matrix.example}}/target/wasm32-unknown-unknown/release/*.wasm ./examples/${{matrix.example}}/res/
- name: Test
run: cargo +${{ matrix.toolchain }} test --manifest-path=./examples/${{ matrix.example }}/Cargo.toml --all
run: cargo +${{ matrix.toolchain }} test --manifest-path="./examples/${{ matrix.example }}/Cargo.toml" --all
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Ensure the following are satisfied before opening a PR:

- The `git-hooks.sh` script has been run to install the git hooks.
- Code is formatted with `rustfmt` by running `cargo fmt`
- Before running the tests, ensure that all example `.wasm` files are built by executing [./examples/build_all.sh](./examples/build_all.sh)
- Run all tests and linters with [./run-tests.sh](./run-tests.sh)
- Ensure any new functionality is adequately tested
- If any new public types or functions are added, ensure they have appropriate [rustdoc](https://doc.rust-lang.org/rustdoc/what-is-rustdoc.html) documentation
1 change: 1 addition & 0 deletions examples/adder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tokio = { version = "1.14", features = ["full"] }
anyhow = "1.0"
near-abi = "0.4.0"
zstd = "0.13"
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }

[profile.release]
codegen-units = 1
Expand Down
1 change: 1 addition & 0 deletions examples/callback-results/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ near-sdk = { path = "../../near-sdk" }
near-workspaces = "0.11.0"
tokio = { version = "1.14", features = ["full"] }
anyhow = "1.0"
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }

[profile.release]
codegen-units = 1
Expand Down
2 changes: 1 addition & 1 deletion examples/cross-contract-calls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"

[dev-dependencies]
anyhow = "1.0"
near-sdk = { path = "../../near-sdk" }
near-sdk = { path = "../../near-sdk", features = ["default", "unit-testing"] }
test-case = "2.0"
tokio = { version = "1.14", features = ["full"] }
near-workspaces = "0.11.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/cross-contract-calls/high-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] }
2 changes: 1 addition & 1 deletion examples/cross-contract-calls/low-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-sdk = { path = "../../../near-sdk" , features = ["default", "unit-testing"] }
1 change: 1 addition & 0 deletions examples/factory-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ anyhow = "1.0"
test-case = "2.0"
tokio = { version = "1.14", features = ["full"] }
near-workspaces = "0.11.0"
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }

[profile.release]
codegen-units = 1
Expand Down
3 changes: 3 additions & 0 deletions examples/factory-contract/high-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }
3 changes: 3 additions & 0 deletions examples/factory-contract/low-level/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }
4 changes: 1 addition & 3 deletions examples/factory-contract/tests/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ async fn test_deploy_status_message(contract_name: &str) -> anyhow::Result<()> {
let contract =
worker.dev_deploy(&std::fs::read(format!("res/{}.wasm", contract_name))?).await?;

// Needed because of 32 character minimum for TLA
// https://docs.near.org/docs/concepts/account#top-level-accounts
let status_id: AccountId = "status-top-level-account-long-name".parse()?;
let status_id: AccountId = format!("status.{}", contract.id()).parse()?;
let status_amt = NearToken::from_near(20);
let res = contract
.call("deploy_status_message")
Expand Down
3 changes: 3 additions & 0 deletions examples/fungible-token/ft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ crate-type = ["cdylib"]
[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }
3 changes: 3 additions & 0 deletions examples/fungible-token/test-contract-defi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ crate-type = ["cdylib"]
[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }
2 changes: 1 addition & 1 deletion examples/non-fungible-token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dev-dependencies]
anyhow = "1.0"
near-contract-standards = { path = "../../near-contract-standards" }
near-sdk = { path = "../../near-sdk" }
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }
tokio = { version = "1.14", features = ["full"] }
near-workspaces = "0.11.0"

Expand Down
3 changes: 3 additions & 0 deletions examples/non-fungible-token/nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ crate-type = ["cdylib"]
[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }
3 changes: 3 additions & 0 deletions examples/non-fungible-token/test-approval-receiver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ crate-type = ["cdylib"]
[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct ApprovalReceiver {
}

// Have to repeat the same trait for our own implementation.
trait ValueReturnTrait {
pub trait ValueReturnTrait {
fn ok_go(&self, msg: String) -> PromiseOrValue<String>;
}

Expand Down
3 changes: 3 additions & 0 deletions examples/non-fungible-token/test-token-receiver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ crate-type = ["cdylib"]
[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }

[dev-dependencies]
near-sdk = { path = "../../../near-sdk", features = ["unit-testing"] }
2 changes: 1 addition & 1 deletion examples/non-fungible-token/test-token-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct TokenReceiver {
}

// Have to repeat the same trait for our own implementation.
trait ValueReturnTrait {
pub trait ValueReturnTrait {
fn ok_go(&self, return_it: bool) -> PromiseOrValue<bool>;
}

Expand Down
3 changes: 3 additions & 0 deletions examples/test-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ opt-level = "z"
lto = true
debug = false
panic = "abort"

[dev-dependencies]
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }
5 changes: 4 additions & 1 deletion examples/versioned/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../near-sdk", features = ["unstable"] }
near-sdk = { path = "../../near-sdk", features = ["unstable", "unit-testing"] }

[profile.release]
codegen-units = 1
Expand All @@ -17,3 +17,6 @@ opt-level = "z"
lto = true
debug = false
panic = "abort"

[dev-dependencies]
near-sdk = { path = "../../near-sdk", features = ["unit-testing"] }
14 changes: 7 additions & 7 deletions examples/versioned/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use near_sdk::store::UnorderedMap;
use near_sdk::store::IterableMap;
use near_sdk::{env, log, near, AccountId, NearToken};

/// An example of a versioned contract. This is a simple contract that tracks how much
Expand Down Expand Up @@ -30,7 +30,7 @@ impl VersionedContract {
}
}

fn funders(&self) -> &UnorderedMap<AccountId, NearToken> {
fn funders(&self) -> &IterableMap<AccountId, NearToken> {
match self {
Self::V0(contract) => &contract.funders,
Self::V1(contract) => &contract.funders,
Expand All @@ -46,24 +46,24 @@ impl Default for VersionedContract {

#[near]
pub struct ContractV0 {
funders: UnorderedMap<AccountId, NearToken>,
funders: IterableMap<AccountId, NearToken>,
}

impl Default for ContractV0 {
fn default() -> Self {
Self { funders: UnorderedMap::new(b"f") }
Self { funders: IterableMap::new(b"f") }
}
}

#[near]
pub struct Contract {
funders: UnorderedMap<AccountId, NearToken>,
funders: IterableMap<AccountId, NearToken>,
nonce: u64,
}

impl Default for Contract {
fn default() -> Self {
Self { funders: UnorderedMap::new(b"f"), nonce: 0 }
Self { funders: IterableMap::new(b"f"), nonce: 0 }
}
}

Expand Down Expand Up @@ -128,7 +128,7 @@ mod tests {
#[test]
fn contract_v0_interactions() {
let mut contract = {
let mut funders = UnorderedMap::new(b"f");
let mut funders = IterableMap::new(b"f");
funders.insert(bob(), NearToken::from_yoctonear(8));
VersionedContract::V0(ContractV0 { funders })
};
Expand Down
2 changes: 1 addition & 1 deletion near-contract-standards/src/non_fungible_token/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn refund_deposit_to_account(storage_used: u64, account_id: AccountId) {

require!(
required_cost <= attached_deposit,
format!("Must attach {} yoctoNEAR to cover storage", required_cost)
format!("Must attach {} to cover storage", required_cost.exact_amount_display())
);

let refund = attached_deposit.saturating_sub(required_cost);
Expand Down
8 changes: 4 additions & 4 deletions near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Main macro of the library for writing NEAR smart contracts.
proc-macro = true

[dependencies]
proc-macro2 = { version = "1", default-features = false }
proc-macro2 = { version = "1", default-features = false }
syn = { version = "2", default-features = false }
strum = { version = "0.26", default-features = false }
strum = { version = "0.26", default-features = false }
strum_macros = "0.26"
quote = { version = "1.0", default-features = false }
quote = { version = "1.0", default-features = false }
Inflector = { version = "0.11.4", default-features = false, features = [] }
darling = { version = "0.20.3", default-features = false }
serde = { version = "1", default-features = false, features = ["serde_derive"] }
serde = { version = "1", default-features = false, features = ["serde_derive"] }
serde_json = "1"

[dev-dependencies]
Expand Down
10 changes: 8 additions & 2 deletions near-sdk-macros/src/core_impl/code_generator/attr_sig_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,16 @@ impl AttrSigInfo {
fn deserialize_data(ty: &SerializerType) -> TokenStream2 {
match ty {
SerializerType::JSON => quote! {
::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON")
match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
}
},
SerializerType::Borsh => quote! {
::near_sdk::borsh::BorshDeserialize::try_from_slice(&data).expect("Failed to deserialize callback using Borsh")
match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using Borsh"),
}
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,22 @@ impl ImplItemMethodInfo {
let decomposition = self.attr_signature_info.decomposition_pattern();
let serializer_invocation = match self.attr_signature_info.input_serializer {
SerializerType::JSON => quote! {
::near_sdk::serde_json::from_slice(
&::near_sdk::env::input().expect("Expected input since method has arguments.")
).expect("Failed to deserialize input from JSON.")
match ::near_sdk::env::input() {
Some(input) => match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize input from JSON.")
},
None => ::near_sdk::env::panic_str("Expected input since method has arguments.")
};
},
SerializerType::Borsh => quote! {
::near_sdk::borsh::BorshDeserialize::try_from_slice(
&::near_sdk::env::input().expect("Expected input since method has arguments.")
).expect("Failed to deserialize input from Borsh.")
match ::near_sdk::env::input() {
Some(input) => match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize input from Borsh.")
},
None => ::near_sdk::env::panic_str("Expected input since method has arguments.")
};
},
};
quote! {
Expand Down Expand Up @@ -354,10 +362,16 @@ impl ImplItemMethodInfo {

let value_ser = |result_serializer: &SerializerType| match result_serializer {
SerializerType::JSON => quote! {
let result = ::near_sdk::serde_json::to_vec(&result).expect("Failed to serialize the return value using JSON.");
let result = match near_sdk::serde_json::to_vec(&result) {
Ok(v) => v,
Err(_) => ::near_sdk::env::panic_str("Failed to serialize the return value using JSON."),
};
},
SerializerType::Borsh => quote! {
let result = ::near_sdk::borsh::to_vec(&result).expect("Failed to serialize the return value using Borsh.");
let result = match near_sdk::borsh::to_vec(&result) {
Ok(v) => v,
Err(_) => ::near_sdk::env::panic_str("Failed to serialize the return value using Borsh."),
};
},
};

Expand Down
10 changes: 8 additions & 2 deletions near-sdk-macros/src/core_impl/code_generator/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ pub fn generate_serializer(
let constructor = quote! { let __args = #constructor_call; };
let value_ser = match serializer {
SerializerType::JSON => quote! {
::near_sdk::serde_json::to_vec(&__args).expect("Failed to serialize the cross contract args using JSON.")
match near_sdk::serde_json::to_vec(&__args) {
Ok(serialized) => serialized,
Err(_) => ::near_sdk::env::panic_str("Failed to serialize the cross contract args using JSON."),
}
},
SerializerType::Borsh => quote! {
::near_sdk::borsh::to_vec(&__args).expect("Failed to serialize the cross contract args using Borsh.")
match near_sdk::borsh::to_vec(&__args) {
Ok(serialized) => serialized,
Err(_) => ::near_sdk::env::panic_str("Failed to serialize the cross contract args using Borsh."),
}
},
};

Expand Down
Loading

0 comments on commit b78b206

Please sign in to comment.