Skip to content

Commit

Permalink
Merge pull request #1 from TeamX-x/auto_deploy_contract
Browse files Browse the repository at this point in the history
Auto deploy contract
  • Loading branch information
thangtranth authored Jun 18, 2022
2 parents 6e06846 + c0dcf18 commit 727caf3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
66 changes: 66 additions & 0 deletions src/deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::*;

const INITIAL_BALANCE: Balance = 3_000_000_000_000_000_000_000_000; // 3e24yN, 3N

#[near_bindgen]
impl Contract {
#[private]
pub fn create_child_contract(prefix: AccountId, code: Vec<u8>) -> Promise {
let subaccount_id =
AccountId::try_from(format!("{}.{}", prefix, env::current_account_id()));
Promise::new(subaccount_id.unwrap())
.create_account()
.add_full_access_key(env::signer_account_pk())
.transfer(INITIAL_BALANCE)
.deploy_contract(code.to_vec())
}

#[private]
pub fn create_smart_contract(
&mut self,
creator_id: AccountId,
contract_deploy_address: AccountId,
frontend_address: String,
contract_name: String,
) {
let deployed_smart_contract = DeployedSmartContract {
contract_deploy_address,
frontend_address,
contract_name,
};

let mut creates = self.creates.get(&creator_id).unwrap_or_else(|| {
UnorderedSet::new(
StorageKey::InnerByCreatorIdKey {
account_id_hash: hash_account_id(&creator_id),
}
.try_to_vec()
.unwrap(),
)
});

creates.insert(&deployed_smart_contract);
self.creates.insert(&creator_id, &creates);
}

pub fn get_created_contract_by_creator(
&self,
creator_id: AccountId,
from_index: Option<u128>,
limit: Option<u64>,
) -> Vec<DeployedSmartContract> {
let by_creator_id = self.creates.get(&creator_id);
let deploy_smart_contracts = if let Some(by_creator_id) = by_creator_id {
by_creator_id
} else {
return vec![];
};
let start = u128::from(from_index.unwrap_or(0));
deploy_smart_contracts
.as_vector()
.iter()
.skip(start as usize)
.take(limit.unwrap_or(0) as usize)
.collect()
}
}
20 changes: 17 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use near_sdk::{
AccountId, Balance, CryptoHash, Gas, PanicOnDefault, Promise,
};

use crate::deploy::*;
use crate::internal::*;
use crate::nft_callback::*;
use crate::sale::*;
Expand All @@ -15,6 +16,7 @@ use crate::uses::*;
use crate::uses_view::*;
use crate::utils::*;

mod deploy;
mod internal;
mod nft_callback;
mod sale;
Expand Down Expand Up @@ -51,14 +53,23 @@ pub struct Uses {
pub use_conditions: UsePriceInYoctoNear,
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct DeployedSmartContract {
pub contract_deploy_address: AccountId,
pub frontend_address: String,
pub contract_name: String,
}

#[near_bindgen]
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
pub struct Contract {
pub owner_id: AccountId,
// Sales token
pub sales: UnorderedMap<ContractAndTokenId, Sale>,
// Uses:
pub creates: LookupMap<AccountId, UnorderedSet<DeployedSmartContract>>,
pub uses: UnorderedMap<ContractAndTokenId, Uses>,
// Sales list by ID
// Sales
pub sales: UnorderedMap<ContractAndTokenId, Sale>,
pub by_owner_id: LookupMap<AccountId, UnorderedSet<ContractAndTokenId>>,
pub by_contract_id: LookupMap<NFTContractId, UnorderedSet<TokenId>>,
pub storage_deposit: LookupMap<AccountId, Balance>,
Expand All @@ -68,6 +79,8 @@ pub struct Contract {
pub enum StorageKey {
SaleKey,
UsesKey,
CreateKey,
InnerByCreatorIdKey { account_id_hash: CryptoHash },
ByOwnerIdKey,
InnerByOwnerIdKey { account_id_hash: CryptoHash },
ByContractIdKey,
Expand All @@ -83,6 +96,7 @@ impl Contract {
owner_id,
sales: UnorderedMap::new(StorageKey::SaleKey.try_to_vec().unwrap()),
uses: UnorderedMap::new(StorageKey::UsesKey.try_to_vec().unwrap()),
creates: LookupMap::new(StorageKey::CreateKey.try_to_vec().unwrap()),
by_owner_id: LookupMap::new(StorageKey::ByOwnerIdKey.try_to_vec().unwrap()),
by_contract_id: LookupMap::new(StorageKey::ByContractIdKey.try_to_vec().unwrap()),
storage_deposit: LookupMap::new(StorageKey::StorageDepositKey.try_to_vec().unwrap()),
Expand Down

0 comments on commit 727caf3

Please sign in to comment.