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

chore: Added cross contract tests #123

Merged
merged 2 commits into from
Apr 20, 2022
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
Binary file added examples/res/factory_contract_high_level.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ libc = "0.2"
[dev-dependencies]
borsh = "0.9"
near-units = "0.1.0"
near-sdk = "4.0.0-pre.7"
test-log = { version = "0.2.8", default-features = false, features = ["trace"] }
tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }

Expand Down
84 changes: 84 additions & 0 deletions workspaces/tests/cross_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use near_sdk::json_types::U128;
use near_units::parse_near;
use workspaces::prelude::*;
use workspaces::result::CallExecutionDetails;
use workspaces::{AccountId, Contract, Network, Worker};

/// The factory contract used in these tests can be found in
/// [near-sdk/examples/factory-contract](https://github.com/near/near-sdk-rs/tree/master/examples/factory-contract/high-level).
const FACTORY_CONTRACT: &[u8] =
include_bytes!("../../examples/res/factory_contract_high_level.wasm");

/// Create a new contract account through a cross contract call with "deploy_status_message".
async fn cross_contract_create_contract(
status_id: &AccountId,
status_amt: &U128,
worker: &Worker<impl Network>,
contract: &Contract,
) -> anyhow::Result<CallExecutionDetails> {
contract
.call(worker, "deploy_status_message")
.args_json((status_id.clone(), status_amt))?
.deposit(parse_near!("50 N"))
.max_gas()
.transact()
.await
}

#[tokio::test]
async fn test_cross_contract_create_contract() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(FACTORY_CONTRACT).await?;
let status_amt = U128::from(parse_near!("35 N"));

// Expect to fail for trying to create a new contract account with too short of a
// top level account name, such as purely just "status"
let status_id: AccountId = "status".parse().unwrap();
let outcome =
cross_contract_create_contract(&status_id, &status_amt, &worker, &contract).await?;
let failures = outcome.failures();
assert!(
failures.len() == 1,
"Expected one receipt failure for creating too short of a TLA, but got {} failures",
failures.len()
);

// Expect to succeed after calling into the contract with expected length for a
// top level account.
let status_id: AccountId = "status-top-level-account-long-name".parse().unwrap();
let outcome =
cross_contract_create_contract(&status_id, &status_amt, &worker, &contract).await?;
let failures = outcome.failures();
assert!(
failures.is_empty(),
"Expected no failures for creating a TLA, but got {} failures",
failures.len(),
);

Ok(())
}

#[tokio::test]
async fn test_cross_contract_calls() -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(FACTORY_CONTRACT).await?;
let status_amt = U128::from(parse_near!("35 N"));

let status_id: AccountId = "status-top-level-account-long-name".parse().unwrap();
cross_contract_create_contract(&status_id, &status_amt, &worker, &contract).await?;

let message = "hello world";
let result = contract
.call(&worker, "complex_call")
.args_json((status_id, message))?
.max_gas()
.transact()
.await?
.json::<String>()?;
assert_eq!(
message, result,
"Results from cross contract call do not match."
);

Ok(())
}