Skip to content

Commit

Permalink
system_mock register and set all roles and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ovstinga committed Jul 5, 2023
1 parent 3671cd3 commit 1b786f7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ fn interactor_nft_rs() {
}

#[test]
#[should_panic(expected = "not implemented")]
fn interactor_nft_all_roles_rs() {
world().run("scenarios/interactor_nft_all_roles.scen.json");
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/tx_execution/system_sc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn execute_system_sc(tx_context: TxContext) -> (TxContext, TxResult) {
"issueNonFungible" => issue_non_fungible(tx_context),
"registerMetaESDT" => register_meta_esdt(tx_context),
"changeSFTToMetaESDT" => change_sft_to_meta_esdt(tx_context),
"registerAndSetAllRoles" => register_and_set_all_roles(),
"registerAndSetAllRoles" => register_and_set_all_roles(tx_context),
"ESDTBurn" => esdt_burn(tx_context),
"mint" => mint(tx_context),
"freeze" => freeze(tx_context),
Expand Down
86 changes: 51 additions & 35 deletions vm/src/tx_execution/system_sc/system_sc_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
types::top_decode_u64,
};

/// Issues a new token.
/// Issues a new fungible token.
#[allow(unused_variables)]
pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) {
let tx_input = tx_context.input_ref();
let tx_cache = tx_context.blockchain_cache();
Expand All @@ -16,46 +17,33 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) {
tx_result = TxResult::from_vm_error("not enough arguments");
return (tx_context, tx_result);
}
let _name = tx_input.args[0].clone();
let name = tx_input.args[0].clone();
let ticker = tx_input.args[1].clone();
let _total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref());
let _decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32;
let total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref());
let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32;

let mut new_token_identifiers = tx_cache.get_new_token_identifiers();

let token_identifier = if let Some((i, ti)) =
first_token_identifier_with_ticker(&new_token_identifiers, &ticker)
{
new_token_identifiers.remove(i);
ti.into_bytes()
} else {
generate_token_identifier_from_ticker(tx_input, tx_cache, &ticker)
};

println!(
"\n\ngenerated new token_identifier: {}\n\n",
std::str::from_utf8(&token_identifier).unwrap()
);

tx_cache.with_account_mut(&tx_input.from, |account| {
account.esdt.issue_token(&token_identifier);
});
tx_cache.set_new_token_identifiers(new_token_identifiers);

tx_result = TxResult {
result_values: vec![token_identifier],
..Default::default()
};

(tx_context, tx_result)
register_and_set_roles(tx_context, ticker, "FT".to_string())
}

/// Issues a new semi-fungible token.
#[allow(unused_variables)]
pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) {
issue_non_fungible(tx_context)
let tx_input = tx_context.input_ref();
let tx_cache = tx_context.blockchain_cache();
let tx_result: TxResult;

if tx_input.args.len() < 2 {
tx_result = TxResult::from_vm_error("not enough arguments");
return (tx_context, tx_result);
}
let name = tx_input.args[0].clone();
let ticker = tx_input.args[1].clone();

register_and_set_roles(tx_context, ticker, "SFT".to_string())
}

/// Issues a new non-fungible token.
#[allow(unused_variables)]
pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) {
let tx_input = tx_context.input_ref();
let tx_cache = tx_context.blockchain_cache();
Expand All @@ -65,9 +53,35 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) {
tx_result = TxResult::from_vm_error("not enough arguments");
return (tx_context, tx_result);
}
let _name = tx_input.args[0].clone();
let name = tx_input.args[0].clone();
let ticker = tx_input.args[1].clone();

register_and_set_roles(tx_context, ticker, "NFT".to_string())
}

// Issues a new token and sets all roles for its type.
#[allow(unused_variables)]
pub fn register_and_set_all_roles(tx_context: TxContext) -> (TxContext, TxResult) {
let tx_input = tx_context.input_ref();
let tx_cache = tx_context.blockchain_cache();

if tx_input.args.len() < 4 {
let tx_result = TxResult::from_vm_error("not enough arguments");
return (tx_context, tx_result);
}

let name = tx_input.args[0].clone();
let ticker = tx_input.args[1].clone();
let token_type = String::from_utf8(tx_input.args[2].clone()).unwrap();
let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32;

register_and_set_roles(tx_context, ticker, token_type)
}

fn register_and_set_roles(tx_context: TxContext, ticker: Vec<u8>, token_type: String) -> (TxContext, TxResult) {
let tx_input = tx_context.input_ref();
let tx_cache = tx_context.blockchain_cache();

let mut new_token_identifiers = tx_cache.get_new_token_identifiers();

let token_identifier = if let Some((i, ti)) =
Expand All @@ -80,11 +94,13 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) {
};

tx_cache.with_account_mut(&tx_input.from, |account| {
account.esdt.issue_token(&token_identifier);
account
.esdt
.register_and_set_roles(&token_identifier, &token_type);
});
tx_cache.set_new_token_identifiers(new_token_identifiers);

tx_result = TxResult {
let tx_result = TxResult {
result_values: vec![token_identifier],
..Default::default()
};
Expand Down
9 changes: 2 additions & 7 deletions vm/src/tx_execution/system_sc/system_sc_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ pub fn register_meta_esdt(tx_context: TxContext) -> (TxContext, TxResult) {
}

#[allow(unused_variables)]
pub fn change_sft_to_meta_esdt(_tx_context: TxContext) -> (TxContext, TxResult) {
pub fn change_sft_to_meta_esdt(tx_context: TxContext) -> (TxContext, TxResult) {
unimplemented!()
}

#[allow(unused_variables)]
pub fn register_and_set_all_roles() -> (TxContext, TxResult) {
unimplemented!()
}

#[allow(unused_variables)]
pub fn esdt_burn(_tx_context: TxContext) -> (TxContext, TxResult) {
pub fn esdt_burn(tx_context: TxContext) -> (TxContext, TxResult) {
unimplemented!()
}

Expand Down
53 changes: 39 additions & 14 deletions vm/src/world_mock/esdt_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,56 @@ impl AccountEsdt {
self.0.iter()
}

pub fn issue_token(&mut self, token_identifier: &[u8]) {
let roles = vec![
"ESDTLocalMint".as_bytes().to_vec(),
"ESDTLocalBurn".as_bytes().to_vec(),
];
pub fn set_special_role(&mut self, token_identifier: &[u8], role: &[u8]) {
if let Some(esdt_data) = self.get_mut_by_identifier(token_identifier) {
let roles = esdt_data.roles.get();
if !roles.contains(role.to_vec().as_ref()) {
let mut new_roles = roles;
new_roles.push(role.to_vec());
esdt_data.roles = EsdtRoles::new(new_roles);
}
}
}

pub fn register_and_set_roles(&mut self, token_identifier: &[u8], token_type: &str) {
self.issue_token(token_identifier);
self.set_roles(
token_identifier.to_vec(),
Self::get_all_roles_for_token_type(token_type),
);
}

fn issue_token(&mut self, token_identifier: &[u8]) {
self.0.insert(
token_identifier.to_vec(),
EsdtData {
instances: EsdtInstances::new(),
last_nonce: 0,
roles: EsdtRoles::new(roles),
roles: EsdtRoles::default(),
frozen: false,
},
);
}

pub fn set_special_role(&mut self, token_identifier: &[u8], role: &[u8]) {
if let Some(esdt_data) = self.get_mut_by_identifier(token_identifier) {
let roles = esdt_data.roles.get();
if !roles.contains(role.to_vec().as_ref()) {
let mut new_roles = roles;
new_roles.push(role.to_vec());
esdt_data.roles = EsdtRoles::new(new_roles);
}
fn get_all_roles_for_token_type(token_type: &str) -> Vec<Vec<u8>> {
match token_type {
"NFT" => vec![
"ESDTRoleNFTCreate".as_bytes().to_vec(),
"ESDTRoleNFTBurn".as_bytes().to_vec(),
"ESDTRoleNFTUpdateAttributes".as_bytes().to_vec(),
"ESDTRoleNFTAddURI".as_bytes().to_vec(),
],
"SFT" | "META" => vec![
"ESDTRoleNFTCreate".as_bytes().to_vec(),
"ESDTRoleNFTBurn".as_bytes().to_vec(),
"ESDTRoleNFTAddQuantity".as_bytes().to_vec(),
],
"FNG" => vec![
"ESDTRoleLocalMint".as_bytes().to_vec(),
"ESDTRoleLocalBurn".as_bytes().to_vec(),
"ESDTRoleLocalTransfer".as_bytes().to_vec(),
],
_ => panic!("invalid token type"),
}
}
}
Expand Down

0 comments on commit 1b786f7

Please sign in to comment.