Skip to content

Commit

Permalink
Merge pull request #48
Browse files Browse the repository at this point in the history
Fixed ESDT fetching identifiers and attributes
  • Loading branch information
gfusee authored Nov 17, 2023
2 parents 8e1f205 + 5cf723f commit 919765f
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 13 deletions.
25 changes: 24 additions & 1 deletion .novax/abis/tester-contract.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"contractCrate": {
"name": "tester-contract",
"version": "0.0.0",
"gitVersion": "0.0.16-1-gecdcf08"
"gitVersion": "0.0.17-4-g302e541"
},
"framework": {
"name": "multiversx-sc",
Expand Down Expand Up @@ -49,6 +49,16 @@
],
"outputs": []
},
{
"name": "returnNftProperties",
"mutability": "mutable",
"inputs": [],
"outputs": [
{
"type": "TestTokenProperties"
}
]
},
{
"name": "returnFungibleBalance",
"mutability": "mutable",
Expand Down Expand Up @@ -703,6 +713,19 @@
"type": "CustomStruct"
}
]
},
"TestTokenProperties": {
"type": "struct",
"fields": [
{
"name": "buffer",
"type": "bytes"
},
{
"name": "integer",
"type": "BigUint"
}
]
}
}
}
Binary file modified .novax/tester-contract.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mocking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ serde = "1.0.180"
serde_json = "1.0.104"
futures = "0.3.28"
hex = "0.4.3"
base64 = "0.21.5"
70 changes: 66 additions & 4 deletions mocking/src/world/infos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs::OpenOptions;
use std::future::join;
use std::hash::Hash;
use std::path::Path;
use base64::Engine;
use num_bigint::BigUint;
use futures::future::join_all;
use multiversx_sc_snippets::hex;
Expand All @@ -26,7 +27,7 @@ pub struct ScenarioWorldInfosEsdtTokenAmount {
pub token_identifier: String,
pub nonce: u64,
pub amount: BigUint,
pub opt_attributes_expr: Option<Vec<u8>>
pub opt_attributes: Option<Vec<u8>>
}

#[derive(PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -130,7 +131,13 @@ impl ScenarioWorldInfos {
if balance.nonce == 0 {
account = account.esdt_balance(token_identifier, &balance.amount)
} else {
account = account.esdt_nft_balance(token_identifier, balance.nonce, &balance.amount, balance.opt_attributes_expr.clone());
let opt_attributes_expr = balance.opt_attributes
.as_deref()
.map(|e| {
base64::engine::general_purpose::STANDARD.decode(e).unwrap()
});

account = account.esdt_nft_balance(token_identifier, balance.nonce, &balance.amount, opt_attributes_expr);
}
}
}
Expand Down Expand Up @@ -253,10 +260,10 @@ async fn get_addresses_balances(gateway_url: &str, addresses: &[Address]) -> Res
let mut amounts: Vec<ScenarioWorldInfosEsdtTokenAmount> = vec![];
for infos in e.1 {
amounts.push(ScenarioWorldInfosEsdtTokenAmount {
token_identifier: infos.token_identifier,
token_identifier: parse_token_identifier(&infos.token_identifier),
nonce: infos.nonce,
amount: infos.balance,
opt_attributes_expr: None, // TODO
opt_attributes: infos.attributes.map(|e| e.as_bytes().to_vec())
})
}

Expand All @@ -283,4 +290,59 @@ where
for (new_key, new_value) in new {
original.insert(new_key, new_value);
}
}

fn parse_token_identifier(token_identifier: &str) -> String {
let mut hyphen_count = 0;
let mut end_index = None;

for (index, char) in token_identifier.char_indices() {
if char == '-' {
hyphen_count += 1;
if hyphen_count == 2 {
end_index = Some(index);
break;
}
}
}

match end_index {
Some(index) => String::from(&token_identifier[..index]),
None => String::from(token_identifier),
}
}

#[cfg(test)]
mod tests {
use crate::world::infos::parse_token_identifier;

#[test]
fn test_parse_token_identifier_empty_string() {
assert_eq!(parse_token_identifier(""), "");
}

#[test]
fn test_parse_token_identifier_no_hyphen() {
assert_eq!(parse_token_identifier("TEST"), "TEST");
}

#[test]
fn test_parse_token_identifier_one_hyphen() {
assert_eq!(parse_token_identifier("TEST-abcdef"), "TEST-abcdef");
}

#[test]
fn test_parse_token_identifier_trailing_hyphen() {
assert_eq!(parse_token_identifier("TEST-abcdef-"), "TEST-abcdef");
}

#[test]
fn test_parse_token_identifier_two_hyphens() {
assert_eq!(parse_token_identifier("TEST-abcdef-123456"), "TEST-abcdef");
}

#[test]
fn test_parse_token_identifier_more_than_two_hyphens() {
assert_eq!(parse_token_identifier("TEST-abcdef-123456-abcdef"), "TEST-abcdef");
}
}
8 changes: 7 additions & 1 deletion mocking/src/world/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ mod test {
token_identifier: "WEGLD-abcdef".to_string(),
nonce: 0,
amount: BigUint::from(10u8),
opt_attributes_expr: Some(vec![0, 1]),
opt_attributes: Some(vec![0, 1]),
},
ScenarioWorldInfosEsdtTokenAmount {
token_identifier: "NFT-abcdef-9a".to_string(),
nonce: 0,
amount: BigUint::from(10u8),
opt_attributes: Some(vec![0, 1]),
}
]
);
Expand Down
14 changes: 14 additions & 0 deletions tester/contract/src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi)]
pub struct TestTokenProperties<M: ManagedTypeApi> {
pub buffer: ManagedBuffer<M>,
pub integer: BigUint<M>
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi)]
pub struct CustomStruct<M: ManagedTypeApi> {
pub first: ManagedBuffer<M>,
Expand Down Expand Up @@ -36,6 +42,14 @@ pub enum CustomEnumWithFields<M: ManagedTypeApi> {

#[multiversx_sc::module]
pub trait PrinterModule: ContractBase {
#[endpoint(returnNftProperties)]
fn return_nft_properties(&self) -> TestTokenProperties<Self::Api> {
self.blockchain().get_token_attributes(
&TokenIdentifier::from("NFT-abcdef"),
6
)
}

#[endpoint(returnFungibleBalance)]
fn return_fungible_balance(&self) -> BigUint<Self::Api> {
self.blockchain().get_sc_balance(
Expand Down
5 changes: 3 additions & 2 deletions tester/contract/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 44
// Endpoints: 45
// Async Callback: 1
// Total number of exported functions: 46
// Total number of exported functions: 47

#![no_std]

Expand All @@ -24,6 +24,7 @@ multiversx_sc_wasm_adapter::endpoints! {
init => init
getSum => sum
add => add
returnNftProperties => return_nft_properties
returnFungibleBalance => return_fungible_balance
returnNonFungibleBalance => return_non_fungible_balance
noArgNoReturnEndpoint => no_arg_no_return_endpoint
Expand Down
4 changes: 2 additions & 2 deletions tester/core/tests/dummy_deploy.rs

Large diffs are not rendered by default.

27 changes: 24 additions & 3 deletions tester/core/tests/mock_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use novax::errors::NovaXError;
use novax_mocking::world::infos::{ScenarioWorldInfosEsdtTokenAmount, ScenarioWorldInfos};
use num_bigint::BigUint;
use novax::Address;
use novax::tester::tester::{CustomEnum, CustomEnumWithFields, CustomEnumWithValues, CustomStruct, CustomStructWithStructAndVec, TesterContract};
use novax::tester::tester::{CustomEnum, CustomEnumWithFields, CustomEnumWithValues, CustomStruct, CustomStructWithStructAndVec, TesterContract, TestTokenProperties};
use novax::executor::StandardMockExecutor;

const TESTER_CONTRACT_ADDRESS: &str = "erd1qqqqqqqqqqqqqpgq9wmk04e90fkhcuzns0pgwm33sdtxze346vpsq0ka9p";
Expand All @@ -24,7 +24,7 @@ fn get_executor() -> Arc<StandardMockExecutor> {
token_identifier: "TEST-abcdef".to_string(),
nonce: 0,
amount: BigUint::from(25u8),
opt_attributes_expr: None,
opt_attributes: None,
}
);

Expand All @@ -33,7 +33,7 @@ fn get_executor() -> Arc<StandardMockExecutor> {
token_identifier: "NFT-abcdef".to_string(),
nonce: 6,
amount: BigUint::from(1u8),
opt_attributes_expr: None,
opt_attributes: Some(b"AAAAC3Rlc3QgYnVmZmVyAAAAAQo=".to_vec()),
}
);

Expand Down Expand Up @@ -144,6 +144,27 @@ async fn test_query_sc_non_fungible_balance() -> Result<(), NovaXError> {
Ok(())
}

#[tokio::test]
async fn test_query_sc_non_fungible_attributes() -> Result<(), NovaXError> {
let executor = get_executor();

let result = TesterContract::new(
TESTER_CONTRACT_ADDRESS
)
.query(executor)
.return_nft_properties()
.await?;

let expected = TestTokenProperties {
buffer: "test buffer".to_string(),
integer: BigUint::from(10u8),
};

assert_eq!(result, expected);

Ok(())
}

#[tokio::test]
async fn test_query_buffer_result() -> Result<(), NovaXError> {
let executor = get_executor();
Expand Down

0 comments on commit 919765f

Please sign in to comment.