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

feat: add new param contract_bytes_base64 with type Option<Base64VecU8> #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 modified res/linkdrop.wasm
Binary file not shown.
66 changes: 64 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,12 @@ impl LinkDrop {
new_account_id: AccountId,
options: CreateAccountOptions,
) -> Promise {
let is_some_option = options.contract_bytes.is_some() || options.full_access_keys.is_some() || options.limited_access_keys.is_some();
let is_some_option = options.contract_bytes_base64.is_some() || options.contract_bytes.is_some() || options.full_access_keys.is_some() || options.limited_access_keys.is_some();
assert!(is_some_option, "Cannot create account with no options. Please specify either contract bytes, full access keys, or limited access keys.");

let is_conflict_contract_bytes = options.contract_bytes_base64.is_some() && options.contract_bytes.is_some();
assert!(!is_conflict_contract_bytes, "Cannot give contract bytes and base64 contract byte string at the same time.");

let amount = env::attached_deposit();

// Initiate a new promise on the new account we're creating and transfer it any attached deposit
Expand All @@ -185,6 +188,11 @@ impl LinkDrop {
promise = promise.deploy_contract(bytes);
};

// If there are any base 64 contract byte string, we should deploy the contract to the account
if let Some(bytes) = options.contract_bytes_base64 {
promise = promise.deploy_contract(bytes.0);
};

// Callback if anything went wrong, refund the predecessor for their attached deposit
promise.then(
Self::ext(env::current_account_id())
Expand Down Expand Up @@ -500,6 +508,35 @@ mod tests {
method_names: "send".to_string(),
}]),
contract_bytes: Some(include_bytes!("../target/wasm32-unknown-unknown/release/linkdrop.wasm").to_vec()),
contract_bytes_base64: None
};

// Initialize the mocked blockchain
testing_env!(
VMContextBuilder::new()
.current_account_id(linkdrop())
.attached_deposit(deposit)
.context.clone()
);

// Create bob's account with the advanced options
contract.create_account_advanced(bob(), options);
}

#[test]
fn test_create_advanced_account_with_base64_contract_byte_string() {
// Create a new instance of the linkdrop contract
let mut contract = LinkDrop::new();

// Default the deposit to an extremely small amount
let deposit = 1_000_000;

// Create options for the advanced account creation
let options: CreateAccountOptions = CreateAccountOptions {
full_access_keys: None,
limited_access_keys: None,
contract_bytes: None,
contract_bytes_base64: Some(include_bytes!("../target/wasm32-unknown-unknown/release/linkdrop.wasm").to_vec().into()),
};

// Initialize the mocked blockchain
Expand Down Expand Up @@ -531,6 +568,31 @@ mod tests {
);

// Create bob's account with the advanced options
contract.create_account_advanced(bob(), CreateAccountOptions { full_access_keys: None, limited_access_keys: None, contract_bytes: None });
contract.create_account_advanced(bob(), CreateAccountOptions { full_access_keys: None, limited_access_keys: None, contract_bytes: None, contract_bytes_base64: None });
}

#[test]
#[should_panic]
fn test_create_advanced_account_conflict_contract_bytes() {
// Create a new instance of the linkdrop contract
let mut contract = LinkDrop::new();
// Default the deposit to an extremely small amount
let deposit = 1_000_000;

// Initialize the mocked blockchain
testing_env!(
VMContextBuilder::new()
.current_account_id(linkdrop())
.attached_deposit(deposit)
.context.clone()
);

// Create bob's account with the advanced options
contract.create_account_advanced(bob(), CreateAccountOptions {
full_access_keys: None,
limited_access_keys: None,
contract_bytes: Some(include_bytes!("../target/wasm32-unknown-unknown/release/linkdrop.wasm").to_vec()),
contract_bytes_base64: Some(include_bytes!("../target/wasm32-unknown-unknown/release/linkdrop.wasm").to_vec().into())
});
}
}
3 changes: 2 additions & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use near_sdk::json_types::Base64VecU8;
use near_sdk::serde::{Serialize, Deserialize};

use crate::*;
Expand Down Expand Up @@ -34,5 +35,5 @@ pub struct CreateAccountOptions {
pub full_access_keys: Option<Vec<PublicKey>>,
pub limited_access_keys: Option<Vec<LimitedAccessKey>>,
pub contract_bytes: Option<Vec<u8>>,
pub contract_bytes_base64: Option<Base64VecU8>
}