Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding low-level way to make XCC #16

Merged
merged 10 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests-advanced-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "18"
node-version: "20"
- name: Install and test modules
run: |
cd ./contract-advanced-ts
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests-simple-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "18"
node-version: "20"
- name: Install and test modules
run: |
cd ./contract-simple-ts
Expand Down
2 changes: 0 additions & 2 deletions contract-advanced-rs/.cargo/config

This file was deleted.

20 changes: 4 additions & 16 deletions contract-advanced-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@ crate-type = ["cdylib", "rlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = "5.4.0"
near-sdk = "5.6.0"
near-contract-standards = "5.6.0"
schemars = "0.8.16"

[dev-dependencies]
near-sdk = { version = "5.4.0", features = ["unit-testing"] }
near-workspaces = { version = "0.11.0", features = ["unstable"] }
near-sdk = { version = "5.6.0", features = ["unit-testing"] }
near-workspaces = { version = "0.15.0", features = ["unstable"] }
tokio = { version = "1.12.0", features = ["full"] }
serde_json = "1"

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = true
debug = false
panic = "abort"
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801
overflow-checks = true

[workspace]
members = []
18 changes: 18 additions & 0 deletions contract-advanced-rs/tests/contracts/hello-near/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "hello-near"
description = "Hello Near Example"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = "5.6.0"

[dev-dependencies]
near-sdk = { version = "5.6.0", features = ["unit-testing"] }
near-workspaces = { version = "0.15.0", features = ["unstable"] }
tokio = { version = "1.12.0", features = ["full"] }
serde_json = "1"
55 changes: 55 additions & 0 deletions contract-advanced-rs/tests/contracts/hello-near/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Find all our documentation at https://docs.near.org
use near_sdk::{log, near};

// Define the contract structure
#[near(contract_state)]
pub struct Contract {
greeting: String,
}

// Define the default, which automatically initializes the contract
impl Default for Contract {
fn default() -> Self {
Self {
greeting: "Hello".to_string(),
}
}
}

// Implement the contract structure
#[near]
impl Contract {
// Public method - returns the greeting saved, defaulting to DEFAULT_GREETING
pub fn get_greeting(&self) -> String {
self.greeting.clone()
}

// Public method - accepts a greeting, such as "howdy", and records it
pub fn set_greeting(&mut self, greeting: String) {
log!("Saving greeting: {}", greeting);
self.greeting = greeting;
}
}

/*
* The rest of this file holds the inline tests for the code above
* Learn more about Rust tests: https://doc.rust-lang.org/book/ch11-01-writing-tests.html
*/
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn get_default_greeting() {
let contract = Contract::default();
// this test did not call set_greeting so should return the default "Hello" greeting
assert_eq!(contract.get_greeting(), "Hello");
}

#[test]
fn set_then_get_greeting() {
let mut contract = Contract::default();
contract.set_greeting("howdy".to_string());
assert_eq!(contract.get_greeting(), "howdy");
}
}
Binary file not shown.
96 changes: 52 additions & 44 deletions contract-advanced-rs/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use std::string::String;
use serde_json::json;
use near_workspaces::{types::NearToken, Account, Contract};
use near_sdk::near;

use near_workspaces::{types::NearToken, Account, Contract};
use serde_json::json;
use std::string::String;

#[tokio::test]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let worker = near_workspaces::sandbox().await?;
// Deploy hello contract
let hello_contract_wasm = std::fs::read("./tests/external-contracts/hello-near.wasm")?;
let hello_contract_wasm =
near_workspaces::compile_project("./tests/contracts/hello-near").await?;
let hello_contract = worker.dev_deploy(&hello_contract_wasm).await?;
// Deploy guest-book contract
let guest_book_contract_wasm = std::fs::read("./tests/external-contracts/guest-book.wasm")?;
let guest_book_contract_wasm = std::fs::read("./tests/contracts/guest-book.wasm")?;
let guest_book_contract = worker.dev_deploy(&guest_book_contract_wasm).await?;
// Deploy counter contract
let counter_contract_wasm = std::fs::read("./tests/external-contracts/counter.wasm")?;
let counter_contract_wasm = std::fs::read("./tests/contracts/counter.wasm")?;
let counter_contract = worker.dev_deploy(&counter_contract_wasm).await?;
// Deploy contract for testing
let contract_wasm = near_workspaces::compile_project("./").await?;
let contract = worker.dev_deploy(&contract_wasm).await?;

// Create accounts
let account = worker.dev_create_account().await?;
let alice = account
Expand All @@ -39,9 +40,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.transact()
.await?
.into_result()?;

// Begin tests
test_multiple_contracts(&alice, &contract, &hello_contract, &guest_book_contract, &counter_contract).await?;
test_multiple_contracts(
&alice,
&contract,
&hello_contract,
&guest_book_contract,
&counter_contract,
)
.await?;
test_similar_contracts(&alice, &contract).await?;
test_batch_actions(&alice, &contract).await?;
Ok(())
Expand All @@ -52,7 +60,7 @@ async fn test_multiple_contracts(
contract: &Contract,
hello_contract: &Contract,
guest_book_contract: &Contract,
counter_contract: &Contract
counter_contract: &Contract,
) -> Result<(), Box<dyn std::error::Error>> {
#[derive(Debug, PartialEq)]
#[near(serializers = [json])]
Expand Down Expand Up @@ -96,48 +104,48 @@ async fn test_multiple_contracts(
.transact()
.await?
.json()?;

assert_eq!(result.0, "Howdy".to_string());
assert_eq!(result.1, -1);
assert_eq!(result.2, expected_messages);
Ok(())
}

async fn test_similar_contracts(
user: &Account,
contract: &Contract
user: &Account,
contract: &Contract,
) -> Result<(), Box<dyn std::error::Error>> {
let expected: Vec<String> = vec![
"hi".parse().unwrap(),
"howdy".parse().unwrap(),
"bye".parse().unwrap()
];

let result: Vec<String> = user
.call(contract.id(), "similar_contracts")
.args_json(json!({}))
.max_gas()
.transact()
.await?
.json()?;

assert_eq!(result, expected);
Ok(())
let expected: Vec<String> = vec![
"hi".parse().unwrap(),
"howdy".parse().unwrap(),
"bye".parse().unwrap(),
];

let result: Vec<String> = user
.call(contract.id(), "similar_contracts")
.args_json(json!({}))
.max_gas()
.transact()
.await?
.json()?;

assert_eq!(result, expected);
Ok(())
}

async fn test_batch_actions(
user: &Account,
contract: &Contract
user: &Account,
contract: &Contract,
) -> Result<(), Box<dyn std::error::Error>> {
let expected: String = "bye".parse().unwrap();
let result: String = user
.call(contract.id(), "batch_actions")
.args_json(json!({}))
.max_gas()
.transact()
.await?
.json()?;

assert_eq!(result, expected);
Ok(())
}
let expected: String = "bye".parse().unwrap();
let result: String = user
.call(contract.id(), "batch_actions")
.args_json(json!({}))
.max_gas()
.transact()
.await?
.json()?;

assert_eq!(result, expected);
Ok(())
}
6 changes: 3 additions & 3 deletions contract-advanced-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"test": "$npm_execpath build && ava -- ./build/cross_contract.wasm"
},
"dependencies": {
"near-cli": "^4.0.10",
"near-sdk-js": "1.0.0"
"near-cli": "^4.0.13",
"near-sdk-js": "2.0.0"
},
"devDependencies": {
"@ava/typescript": "^4.1.0",
"ava": "^6.1.2",
"near-workspaces": "^3.5.0",
"near-workspaces": "^4.0.0",
"ts-morph": "^22.0.0",
"ts-node": "^10.9.2",
"tsimp": "^2.0.11",
Expand Down
2 changes: 0 additions & 2 deletions contract-simple-rs/.cargo/config

This file was deleted.

24 changes: 6 additions & 18 deletions contract-simple-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,15 @@ authors = ["Near Inc <[email protected]>"]
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = "5.4.0"
near-sdk = "5.6.0"
near-contract-standards = "5.6.0"

[dev-dependencies]
near-sdk = { version = "5.4.0", features = ["unit-testing"] }
near-workspaces = { version = "0.11.0", features = ["unstable"] }
near-sdk = { version = "5.6.0", features = ["unit-testing"] }
near-workspaces = { version = "0.15.0", features = ["unstable"] }
tokio = { version = "1.12.0", features = ["full"] }
serde_json = "1"

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = true
debug = false
panic = "abort"
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801
overflow-checks = true

[workspace]
members = []
serde_json = "1"
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Find all our documentation at https://docs.near.org
use near_sdk::ext_contract;

pub const NO_DEPOSIT: u128 = 0;
pub const XCC_SUCCESS: u64 = 1;

// Validator interface, for cross-contract calls
#[ext_contract(hello_near)]
trait HelloNear {
Expand Down
Loading
Loading