Skip to content

Commit 629b1b2

Browse files
committed
wip: factory
1 parent b70459c commit 629b1b2

File tree

6 files changed

+38
-90
lines changed

6 files changed

+38
-90
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ crate-type = ["cdylib", "rlib"]
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515
[dependencies]
16-
near-sdk = { version = "5.1.0", features = ["unstable"] }
16+
near-sdk = { version = "5.3.0", features = ["unstable"] }
17+
near-contract-standards = "5.3.0"
1718

1819
[dev-dependencies]
19-
near-sdk = { version = "5.1.0", features = ["unit-testing"] }
20+
near-sdk = { version = "5.3.0", features = ["unit-testing"] }
2021
near-workspaces = { version = "0.10.0", features = ["unstable"] }
2122
tokio = { version = "1.12.0", features = ["full"] }
2223
serde_json = "1"

src/deploy.rs

+32-56
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,63 @@
1-
use near_sdk::serde::Serialize;
2-
use near_sdk::{env, log, near, AccountId, NearToken, Promise, PromiseError, PublicKey};
1+
use near_sdk::{env, json_types::U128, log, near, require, AccountId, NearToken, Promise, PromiseError, PublicKey};
2+
use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata;
33

4-
use crate::{Contract, ContractExt, NEAR_PER_STORAGE, NO_DEPOSIT, TGAS};
4+
use crate::{Contract, ContractExt, FT_CONTRACT, NEAR_PER_STORAGE, NO_DEPOSIT, TGAS};
55

6-
#[derive(Serialize)]
7-
#[serde(crate = "near_sdk::serde")]
8-
struct DonationInitArgs {
9-
beneficiary: AccountId,
6+
7+
#[near(serializers = [json])]
8+
pub struct TokenArgs {
9+
owner_id: AccountId,
10+
total_supply: U128,
11+
metadata: FungibleTokenMetadata,
1012
}
1113

1214
#[near]
1315
impl Contract {
16+
17+
fn get_required(&self, args: &TokenArgs) -> u128 {
18+
((FT_WASM_CODE.len() + EXTRA_BYTES + args.try_to_vec().unwrap().len() * 2) as NearToken)
19+
* STORAGE_PRICE_PER_BYTE)
20+
.into()
21+
}
22+
1423
#[payable]
15-
pub fn create_factory_subaccount_and_deploy(
24+
pub fn create_token(
1625
&mut self,
17-
name: String,
18-
beneficiary: AccountId,
19-
public_key: Option<PublicKey>,
26+
args: TokenArgs,
2027
) -> Promise {
28+
args.metadata.assert_valid();
29+
let token_id = args.metadata.symbol.to_ascii_lowercase();
30+
31+
require!(is_valid_token_id(&token_id), "Invalid Symbol");
32+
2133
// Assert the sub-account is valid
22-
let current_account = env::current_account_id().to_string();
23-
let subaccount: AccountId = format!("{name}.{current_account}").parse().unwrap();
34+
let token_account_id = format!("{}.{}", token_id, env::current_account_id());
2435
assert!(
25-
env::is_valid_account_id(subaccount.as_bytes()),
26-
"Invalid subaccount"
36+
env::is_valid_account_id(token_account_id.as_bytes()),
37+
"Token Account ID is invalid"
2738
);
2839

2940
// Assert enough tokens are attached to create the account and deploy the contract
3041
let attached = env::attached_deposit();
42+
let required = self.get_required(&args);
3143

32-
let code = self.code.clone().unwrap();
33-
let contract_bytes = code.len() as u128;
34-
let minimum_needed = NEAR_PER_STORAGE.saturating_mul(contract_bytes);
3544
assert!(
36-
attached >= minimum_needed,
45+
attached >= required,
3746
"Attach at least {minimum_needed} yⓃ"
3847
);
3948

40-
let init_args = near_sdk::serde_json::to_vec(&DonationInitArgs { beneficiary }).unwrap();
49+
let init_args = near_sdk::serde_json::to_vec(args).unwrap();
4150

4251
let mut promise = Promise::new(subaccount.clone())
4352
.create_account()
4453
.transfer(attached)
45-
.deploy_contract(code)
54+
.deploy_contract(FT_CONTRACT)
4655
.function_call(
47-
"init".to_owned(),
56+
"new".to_owned(),
4857
init_args,
4958
NO_DEPOSIT,
50-
TGAS.saturating_mul(5),
59+
TGAS.saturating_mul(50),
5160
);
52-
53-
// Add full access key is the user passes one
54-
if let Some(pk) = public_key {
55-
promise = promise.add_full_access_key(pk);
56-
}
57-
58-
// Add callback
59-
promise.then(
60-
Self::ext(env::current_account_id()).create_factory_subaccount_and_deploy_callback(
61-
subaccount,
62-
env::predecessor_account_id(),
63-
attached,
64-
),
65-
)
6661
}
6762

68-
#[private]
69-
pub fn create_factory_subaccount_and_deploy_callback(
70-
&mut self,
71-
account: AccountId,
72-
user: AccountId,
73-
attached: NearToken,
74-
#[callback_result] create_deploy_result: Result<(), PromiseError>,
75-
) -> bool {
76-
if let Ok(_result) = create_deploy_result {
77-
log!(format!("Correctly created and deployed to {account}"));
78-
return true;
79-
};
80-
81-
log!(format!(
82-
"Error creating {account}, returning {attached}yⓃ to {user}"
83-
));
84-
Promise::new(user).transfer(attached);
85-
false
86-
}
8763
}

src/donation-contract/donation.wasm

-151 KB
Binary file not shown.

src/ft-contract/ft.wasm

220 KB
Binary file not shown.

src/lib.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,19 @@ use near_sdk::store::LazyOption;
33
use near_sdk::{near, Gas, NearToken};
44

55
mod deploy;
6-
mod manager;
76

87
const NEAR_PER_STORAGE: NearToken = NearToken::from_yoctonear(10u128.pow(18)); // 10e18yⓃ
9-
const DEFAULT_CONTRACT: &[u8] = include_bytes!("./donation-contract/donation.wasm");
8+
const FT_CONTRACT: &[u8] = include_bytes!("./ft-contract/ft.wasm");
109
const TGAS: Gas = Gas::from_tgas(1); // 10e12yⓃ
1110
const NO_DEPOSIT: NearToken = NearToken::from_near(0); // 0yⓃ
1211

1312
// Define the contract structure
1413
#[near(contract_state)]
15-
pub struct Contract {
16-
// Since a contract is something big to store, we use LazyOptions
17-
// this way it is not deserialized on each method call
18-
code: LazyOption<Vec<u8>>,
19-
// Please note that it is much more efficient to **not** store this
20-
// code in the state, and directly use `DEFAULT_CONTRACT`
21-
// However, this does not enable to update the stored code.
22-
}
14+
pub struct Contract { }
2315

2416
// Define the default, which automatically initializes the contract
2517
impl Default for Contract {
2618
fn default() -> Self {
27-
Self {
28-
code: LazyOption::new("code".as_bytes(), Some(DEFAULT_CONTRACT.to_vec())),
29-
}
19+
Self { }
3020
}
3121
}

src/manager.rs

-19
This file was deleted.

0 commit comments

Comments
 (0)