Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 38 additions & 0 deletions rust/examples/global_contract_accountid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use near_api::*;
use near_primitives::views::FinalExecutionOutcomeView;

/// Example deploying a contract to the global contract code storage as account-id
#[tokio::main]
async fn main() {
let global_account_id: AccountId = "nft-contract.testnet".parse().unwrap();
let global_code = std::fs::read("path/to/your/contract.wasm").unwrap();
let global_signer = Signer::new(Signer::from_ledger()).unwrap();

// Deploy the global contract code using the account ID `nft-contract.testnet`
// This will deploy the contract to the specified account ID
// and return the final execution outcome
let result: FinalExecutionOutcomeView = Contract::deploy_global_contract_code(global_code)
.as_account_id(global_account_id.clone())
.with_signer(global_signer)
.send_to_testnet()
.await.unwrap();

println!("{:?}", result);

// Prepare a transaction to deploy a contract to the provided account using a mutable account-id reference to the code from the global contract code storage.
// This is useful for deploying contracts that are meant to be used by multiple accounts or for creating a contract that can be updated later.
//
// Please note that you have to trust the account-id that you are providing. As the code is mutable, the owner of the referenced account can
// change the code at any time which might lead to unexpected behavior or malicious activity.
let my_account_id: AccountId = "my-contract.testnet".parse().unwrap();
let my_signer = Signer::new(Signer::from_ledger()).unwrap();

let result: FinalExecutionOutcomeView = Contract::deploy(my_account_id)
.use_global_account_id(global_account_id)
.without_init_call()
.with_signer(my_signer)
.send_to_testnet()
.await.unwrap();

println!("{:?}", result);
}
36 changes: 36 additions & 0 deletions rust/examples/global_contract_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use near_api::*;
use near_primitives::views::FinalExecutionOutcomeView;

/// Example deploying a contract to the global contract code storage as hash
#[tokio::main]
async fn main() {
let account_id: AccountId = "my-account.testnet".parse().unwrap();
let code = std::fs::read("path/to/your/contract.wasm").unwrap();
let signer = Signer::new(Signer::from_ledger()).unwrap();

// Deploy the global contract code using a hash
// This will deploy the contract to the global contract hash
// and return the final execution outcome
let result: FinalExecutionOutcomeView = Contract::deploy_global_contract_code(code)
.as_hash()
.with_signer(account_id, signer)
.send_to_testnet()
.await.unwrap();

println!("{:?}", result);

// Prepare a transaction to deploy a contract to the provided account using an immutable hash reference to the code from the global contract code storage.
let global_hash: types::CryptoHash = "DxfRbrjT3QPmoANMDYTR6iXPGJr7xRUyDnQhcAWjcoFF".parse().unwrap();
let my_account_id: AccountId = "my-contract.testnet".parse().unwrap();
let my_signer = Signer::new(Signer::from_ledger()).unwrap();

let result: FinalExecutionOutcomeView = Contract::deploy(my_account_id)
.use_global_hash(global_hash)
.without_init_call()
.with_signer(my_signer)
.send_to_testnet()
.await.unwrap();

println!("{:?}", result);

}