Skip to content

Commit

Permalink
Merge pull request #1745 from multiversx/test-esdt-transfer
Browse files Browse the repository at this point in the history
TestEsdtTransfer syntactic sugar
  • Loading branch information
andrei-marinica authored Aug 26, 2024
2 parents 2251e8a + 266b717 commit 4fa9ccc
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn test_nft_update_attributes_and_send() {
.tx()
.from(USER_ADDRESS)
.to(FORWARDER_ADDRESS)
.single_esdt(&TokenIdentifier::from(NFT_TOKEN_ID), 1, &1u32.into())
.payment(TestEsdtTransfer(NFT_TOKEN_ID, 1, 1))
.whitebox(forwarder_legacy::contract_obj, |sc| {
sc.nft_update_attributes(NFT_TOKEN_ID.to_token_identifier(), 1, new_attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn test_nft_update_attributes_and_send() {
.tx()
.from(USER_ADDRESS)
.to(FORWARDER_ADDRESS)
.single_esdt(&TokenIdentifier::from(NFT_TOKEN_ID), 1, &1u32.into())
.payment(TestEsdtTransfer(NFT_TOKEN_ID, 1, 1))
.whitebox(forwarder::contract_obj, |sc| {
sc.nft_update_attributes(NFT_TOKEN_ID.to_token_identifier(), 1, new_attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ fn test_transfer_role() {
.tx()
.from(USER_ADDRESS)
.to(TRANSFER_ROLE_FEATURES_ADDRESS)
.single_esdt(
&TokenIdentifier::from(TRANSFER_TOKEN_ID),
0,
&BigUint::from(100u64),
)
.payment(TestEsdtTransfer(TRANSFER_TOKEN_ID, 0, 100))
.whitebox(transfer_role_features::contract_obj, |sc| {
let payments = ManagedVec::from_single_item(EsdtTokenPayment::new(
managed_token_id!(TRANSFER_TOKEN_ID_EXPR),
Expand All @@ -101,11 +97,7 @@ fn test_transfer_role() {
.tx()
.from(USER_ADDRESS)
.to(TRANSFER_ROLE_FEATURES_ADDRESS)
.single_esdt(
&TokenIdentifier::from(TRANSFER_TOKEN_ID),
0,
&BigUint::from(100u64),
)
.payment(TestEsdtTransfer(TRANSFER_TOKEN_ID, 0, 100))
.returns(ExpectError(4u64, "Destination address not whitelisted"))
.whitebox(transfer_role_features::contract_obj, |sc| {
let payments = ManagedVec::from_single_item(EsdtTokenPayment::new(
Expand All @@ -126,11 +118,7 @@ fn test_transfer_role() {
.tx()
.from(USER_ADDRESS)
.to(TRANSFER_ROLE_FEATURES_ADDRESS)
.single_esdt(
&TokenIdentifier::from(TRANSFER_TOKEN_ID),
0,
&BigUint::from(100u64),
)
.payment(TestEsdtTransfer(TRANSFER_TOKEN_ID, 0, 100))
.whitebox(transfer_role_features::contract_obj, |sc| {
let payments = ManagedVec::from_single_item(EsdtTokenPayment::new(
managed_token_id!(TRANSFER_TOKEN_ID_EXPR),
Expand Down Expand Up @@ -159,11 +147,7 @@ fn test_transfer_role() {
.tx()
.from(USER_ADDRESS)
.to(TRANSFER_ROLE_FEATURES_ADDRESS)
.single_esdt(
&TokenIdentifier::from(TRANSFER_TOKEN_ID),
0,
&BigUint::from(100u64),
)
.payment(TestEsdtTransfer(TRANSFER_TOKEN_ID, 0, 100))
.whitebox(transfer_role_features::contract_obj, |sc| {
let payments = ManagedVec::from_single_item(EsdtTokenPayment::new(
managed_token_id!(TRANSFER_TOKEN_ID_EXPR),
Expand Down
117 changes: 30 additions & 87 deletions contracts/feature-tests/use-module/tests/gov_module_whitebox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use multiversx_sc_modules::governance::{
};
use multiversx_sc_scenario::imports::*;

const GOV_TOKEN_ID_EXPR: TestTokenIdentifier = TestTokenIdentifier::new("GOV-123456");
const GOV_TOKEN_ID: &[u8] = b"GOV-123456";
const GOV_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("GOV-123456");
const QUORUM: u64 = 1_500;
const MIN_BALANCE_PROPOSAL: u64 = 500;
const VOTING_DELAY_BLOCKS: u64 = 10;
Expand Down Expand Up @@ -42,19 +41,19 @@ fn setup() -> ScenarioWorld {
world
.account(OWNER_ADDRESS)
.nonce(1)
.esdt_balance(GOV_TOKEN_ID_EXPR, INITIAL_GOV_TOKEN_BALANCE);
.esdt_balance(GOV_TOKEN_ID, INITIAL_GOV_TOKEN_BALANCE);
world
.account(FIRST_USER_ADDRESS)
.nonce(1)
.esdt_balance(GOV_TOKEN_ID_EXPR, INITIAL_GOV_TOKEN_BALANCE);
.esdt_balance(GOV_TOKEN_ID, INITIAL_GOV_TOKEN_BALANCE);
world
.account(SECOND_USER_ADDRESS)
.nonce(1)
.esdt_balance(GOV_TOKEN_ID_EXPR, INITIAL_GOV_TOKEN_BALANCE);
.esdt_balance(GOV_TOKEN_ID, INITIAL_GOV_TOKEN_BALANCE);
world
.account(THIRD_USER_ADDRESS)
.nonce(1)
.esdt_balance(GOV_TOKEN_ID_EXPR, INITIAL_GOV_TOKEN_BALANCE);
.esdt_balance(GOV_TOKEN_ID, INITIAL_GOV_TOKEN_BALANCE);

// init
let new_address = world
Expand All @@ -66,7 +65,7 @@ fn setup() -> ScenarioWorld {
.returns(ReturnsNewBech32Address)
.whitebox(use_module::contract_obj, |sc| {
sc.init_governance_module(
TokenIdentifier::from(GOV_TOKEN_ID_EXPR),
TokenIdentifier::from(GOV_TOKEN_ID),
BigUint::from(QUORUM),
BigUint::from(MIN_BALANCE_PROPOSAL),
VOTING_DELAY_BLOCKS,
Expand Down Expand Up @@ -96,11 +95,7 @@ pub fn propose(
.tx()
.from(proposer)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID_EXPR),
0,
&BigUint::from(gov_token_amount),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, gov_token_amount))
.whitebox(use_module::contract_obj, |sc| {
let mut args_managed = ManagedVec::new();
for arg in args {
Expand Down Expand Up @@ -151,11 +146,7 @@ fn test_change_gov_config() {
.tx()
.from(SECOND_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(999u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 999))
.returns(ExpectError(4u64, "Proposal is not active"))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
Expand All @@ -168,11 +159,7 @@ fn test_change_gov_config() {
.tx()
.from(SECOND_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(999u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 999))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
});
Expand Down Expand Up @@ -218,11 +205,7 @@ fn test_change_gov_config() {
.tx()
.from(FIRST_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(200u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 200))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
});
Expand All @@ -232,11 +215,7 @@ fn test_change_gov_config() {
.tx()
.from(OWNER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(200u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 200))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::DownVote);
});
Expand All @@ -262,11 +241,7 @@ fn test_change_gov_config() {
.tx()
.from(FIRST_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(200u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 200))
.returns(ExpectError(4u64, "Already voted for this proposal"))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
Expand All @@ -277,11 +252,7 @@ fn test_change_gov_config() {
.tx()
.from(THIRD_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(200u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 200))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
});
Expand Down Expand Up @@ -330,16 +301,16 @@ fn test_change_gov_config() {

world
.check_account(FIRST_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(300u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(300u64));
world
.check_account(SECOND_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(1u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(1u64));
world
.check_account(THIRD_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(800u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(800u64));
world
.check_account(OWNER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(800u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(800u64));
}

#[test]
Expand All @@ -366,11 +337,7 @@ fn test_down_veto_gov_config() {
.tx()
.from(FIRST_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(300u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 300))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
});
Expand All @@ -382,11 +349,7 @@ fn test_down_veto_gov_config() {
.tx()
.from(SECOND_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(200u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 200))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
});
Expand All @@ -395,11 +358,7 @@ fn test_down_veto_gov_config() {
.tx()
.from(THIRD_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(200u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 200))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::DownVetoVote)
});
Expand All @@ -419,15 +378,15 @@ fn test_down_veto_gov_config() {

world
.check_account(FIRST_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(200u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(200u64));

world
.check_account(SECOND_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(800u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(800u64));

world
.check_account(THIRD_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(800u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(800u64));
}

#[test]
Expand All @@ -454,11 +413,7 @@ fn test_abstain_vote_gov_config() {
.tx()
.from(FIRST_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(500u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 500))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::UpVote);
});
Expand All @@ -470,11 +425,7 @@ fn test_abstain_vote_gov_config() {
.tx()
.from(SECOND_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(400u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 400))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::DownVote);
});
Expand All @@ -483,11 +434,7 @@ fn test_abstain_vote_gov_config() {
.tx()
.from(THIRD_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(600u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 600))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::AbstainVote);
});
Expand Down Expand Up @@ -527,13 +474,13 @@ fn test_abstain_vote_gov_config() {

world
.check_account(FIRST_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::zero());
.esdt_balance(GOV_TOKEN_ID, BigUint::zero());
world
.check_account(SECOND_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(600u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(600u64));
world
.check_account(THIRD_USER_ADDRESS)
.esdt_balance(GOV_TOKEN_ID_EXPR, BigUint::from(400u64));
.esdt_balance(GOV_TOKEN_ID, BigUint::from(400u64));
}

#[test]
Expand All @@ -560,11 +507,7 @@ fn test_gov_cancel_defeated_proposal() {
.tx()
.from(SECOND_USER_ADDRESS)
.to(USE_MODULE_ADDRESS)
.single_esdt(
&TokenIdentifier::from(GOV_TOKEN_ID),
0,
&BigUint::from(999u64),
)
.payment(TestEsdtTransfer(GOV_TOKEN_ID, 0, 999))
.whitebox(use_module::contract_obj, |sc| {
sc.vote(proposal_id, VoteType::DownVote)
});
Expand Down
Loading

0 comments on commit 4fa9ccc

Please sign in to comment.