Skip to content

Commit

Permalink
[WIP] Testing chaining SC for issue
Browse files Browse the repository at this point in the history
Co-authored-by: Andrei Vasilescu <[email protected]>
  • Loading branch information
JustEatAnApple and andreivasilescu24 committed Aug 29, 2024
1 parent c4bf367 commit 88a84e3
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl ProxyTestInteract {
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;

let adder_owner_address = interactor.register_wallet(test_wallets::mike());
let wallet_address = interactor.register_wallet(test_wallets::mike());
let adder_owner_address = interactor.register_wallet(test_wallets::alice());
let wallet_address = interactor.register_wallet(test_wallets::alice());

Self {
interactor,
Expand Down Expand Up @@ -283,12 +283,12 @@ impl ProxyTestInteract {
.typed(proxy_test_proxy::ProxyTestProxy)
.issue_fungible_token(token_display_name, token_ticker, initial_supply)
.egld(egld_amount)
.returns(ReturnsNewTokenIdentifier)
.returns(ReturnsResultUnmanaged)
.prepare_async()
.run()
.await;

println!("Result: {response:?}");
println!("Papacioc: {response:?}"); // asta e gol
}

async fn send_egld(&mut self) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
path = "src/proxy_test_proxy.rs"

[[proxy]]
path = "interactor/src/proxy.rs"
path = "../returns-token-identifier-test/src/proxy_proxy_test.rs"

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ async fn main() {
Some(basic_interact_cli::InteractCliCommand::Upgrade(args)) => {
basic_interact.upgrade(args.value).await
},
Some(basic_interact_cli::InteractCliCommand::ContractCall) => {
basic_interact.call_contract().await;
},
None => {},
}
}
Expand All @@ -64,8 +67,8 @@ impl ReturnsTokenIdentifierTestInteract {
.await;

let adder_owner_address =
interactor.register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap());
let wallet_address = interactor.register_wallet(test_wallets::mike());
interactor.register_wallet(test_wallets::alice());
let wallet_address = interactor.register_wallet(test_wallets::alice());

Self {
interactor,
Expand Down Expand Up @@ -93,7 +96,7 @@ impl ReturnsTokenIdentifierTestInteract {
.interactor
.tx()
.from(&self.adder_owner_address)
.gas(3_000_000)
.gas(30_000_000)
.typed(returns_token_identifier_test_proxy::ReturnsTokenIdentifierTestProxy)
.init(0u32)
.code(ADDER_CODE_PATH)
Expand Down Expand Up @@ -123,7 +126,7 @@ impl ReturnsTokenIdentifierTestInteract {
.typed(returns_token_identifier_test_proxy::ReturnsTokenIdentifierTestProxy)
.init(0u32)
.code(ADDER_CODE_PATH)
.gas(3_000_000)
.gas(30_000_000)
.returns(ReturnsNewBech32Address)
});
}
Expand Down Expand Up @@ -151,7 +154,7 @@ impl ReturnsTokenIdentifierTestInteract {
.to(self.state.current_adder_address())
.typed(returns_token_identifier_test_proxy::ReturnsTokenIdentifierTestProxy)
.add(value)
.gas(3_000_000)
.gas(30_000_000)
});
}

Expand All @@ -176,7 +179,7 @@ impl ReturnsTokenIdentifierTestInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.current_adder_address())
.gas(3_000_000)
.gas(30_000_000)
.typed(returns_token_identifier_test_proxy::ReturnsTokenIdentifierTestProxy)
.add(value)
.prepare_async()
Expand Down Expand Up @@ -207,7 +210,7 @@ impl ReturnsTokenIdentifierTestInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.current_adder_address())
.gas(3_000_000)
.gas(30_000_000)
.typed(returns_token_identifier_test_proxy::ReturnsTokenIdentifierTestProxy)
.upgrade(BigUint::from(new_value))
.code_metadata(CodeMetadata::UPGRADEABLE)
Expand All @@ -231,6 +234,32 @@ impl ReturnsTokenIdentifierTestInteract {

println!("response: {response:?}");
}

async fn call_contract(&mut self) {
let egld_amount = BigUint::<StaticApi>::from(50000000000000000u128);

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs#L239

warning: unused variable: `egld_amount` --> contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs:239:13 | 239 | let egld_amount = BigUint::<StaticApi>::from(50000000000000000u128); | ^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_egld_amount` | = note: `#[warn(unused_variables)]` on by default
Raw output
contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs:239:13:w:warning: unused variable: `egld_amount`
   --> contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs:239:13
    |
239 |         let egld_amount = BigUint::<StaticApi>::from(50000000000000000u128);
    |             ^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_egld_amount`
    |
    = note: `#[warn(unused_variables)]` on by default


__END__

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs#L239

warning: unused variable: `egld_amount` --> contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs:239:13 | 239 | let egld_amount = BigUint::<StaticApi>::from(50000000000000000u128); | ^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_egld_amount` | = note: `#[warn(unused_variables)]` on by default
Raw output
contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs:239:13:w:warning: unused variable: `egld_amount`
   --> contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs:239:13
    |
239 |         let egld_amount = BigUint::<StaticApi>::from(50000000000000000u128);
    |             ^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_egld_amount`
    |
    = note: `#[warn(unused_variables)]` on by default


__END__

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Rust tests

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Rust tests

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Test Coverage

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Test Coverage

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Test Coverage

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Test Coverage

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Wasm tests

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Wasm tests

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

unused variable: `egld_amount`

Check warning on line 239 in contracts/examples/token-identifier-test/returns-token-identifier-test/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

unused variable: `egld_amount`

let contract_address = Bech32Address::from_bech32_string("erd1qqqqqqqqqqqqqpgqyu0yylry6dyvynsqsmswqzvyewes38j0a4sqevrx55".to_string());

let token_display_name = ManagedBuffer::from("PROXYTOKEN");
let token_ticker = ManagedBuffer::from("PRXY");
let initial_supply = BigUint::<StaticApi>::from(100000000000000000000u128);

let response = self
.interactor
.tx()
.from(&self.wallet_address)
.to(self.state.current_adder_address())
.gas(30_000_000u64)
.typed(returns_token_identifier_test_proxy::ReturnsTokenIdentifierTestProxy)
.call_contract(contract_address, token_display_name, token_ticker, initial_supply)
//.egld(egld_amount)
.returns(ReturnsResultUnmanaged)
.prepare_async()
.run()
.await;

println!("Result: {response:?}");
}
}

#[tokio::test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum InteractCliCommand {
Sum,
#[command(name = "upgrade", about = "Upgrade contract")]
Upgrade(UpgradeArgs),
#[command(name = "contract-call", about = "Call Proxy")]
ContractCall,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

[[proxy]]
path = "src/returns_token_identifier_test_proxy.rs"

[[proxy]]
path = "interactor/src/proxy.rs"

Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Code generated by the multiversx-sc proxy generator. DO NOT EDIT.

////////////////////////////////////////////////////
////////////////// AUTO-GENERATED //////////////////
////////////////////////////////////////////////////

#![allow(dead_code)]
#![allow(clippy::all)]

use multiversx_sc::proxy_imports::*;

pub struct ProxyTestProxy;

impl<Env, From, To, Gas> TxProxyTrait<Env, From, To, Gas> for ProxyTestProxy
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
type TxProxyMethods = ProxyTestProxyMethods<Env, From, To, Gas>;

fn proxy_methods(self, tx: Tx<Env, From, To, (), Gas, (), ()>) -> Self::TxProxyMethods {
ProxyTestProxyMethods { wrapped_tx: tx }
}
}

pub struct ProxyTestProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
wrapped_tx: Tx<Env, From, To, (), Gas, (), ()>,
}

#[rustfmt::skip]
impl<Env, From, Gas> ProxyTestProxyMethods<Env, From, (), Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
Gas: TxGas<Env>,
{
pub fn init<
Arg0: ProxyArg<BigUint<Env::Api>>,
>(
self,
initial_value: Arg0,
) -> TxTypedDeploy<Env, From, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_deploy()
.argument(&initial_value)
.original_result()
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> ProxyTestProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn upgrade<
Arg0: ProxyArg<BigUint<Env::Api>>,
>(
self,
initial_value: Arg0,
) -> TxTypedUpgrade<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_upgrade()
.argument(&initial_value)
.original_result()
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> ProxyTestProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn last_issued_token(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, TokenIdentifier<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("lastIssuedToken")
.original_result()
}

pub fn last_error_message(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedBuffer<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("lastErrorMessage")
.original_result()
}

pub fn sum(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getSum")
.original_result()
}

/// Add desired amount to the storage variable.
pub fn add<
Arg0: ProxyArg<BigUint<Env::Api>>,
>(
self,
value: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("add")
.argument(&value)
.original_result()
}

pub fn issue_fungible_token<
Arg0: ProxyArg<ManagedBuffer<Env::Api>>,
Arg1: ProxyArg<ManagedBuffer<Env::Api>>,
Arg2: ProxyArg<BigUint<Env::Api>>,
>(
self,
token_display_name: Arg0,
token_ticker: Arg1,
initial_supply: Arg2,
) -> TxTypedCall<Env, From, To, (), Gas, ()> {
self.wrapped_tx
.raw_call("issue_fungible_token")
.argument(&token_display_name)
.argument(&token_ticker)
.argument(&initial_supply)
.original_result()
}

pub fn send_egld<
Arg0: ProxyArg<ManagedAddress<Env::Api>>,
Arg1: ProxyArg<BigUint<Env::Api>>,
>(
self,
to: Arg0,
amount: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("send_egld")
.argument(&to)
.argument(&amount)
.original_result()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

use multiversx_sc::imports::*;

pub mod proxy_proxy_test;
pub mod returns_token_identifier_test_proxy;

/// One of the simplest smart contracts possible,
/// it holds a single variable in storage, which anyone can increment.
#[multiversx_sc::contract]
pub trait ReturnsTokenIdentifierTest {
#[view(lastIssuedToken)]
#[storage_mapper("lastIssuedToken")]
fn last_issued_token(&self) -> SingleValueMapper<TokenIdentifier>;

#[view(lastErrorMessage)]
#[storage_mapper("lastErrorMessage")]
fn last_error_message(&self) -> SingleValueMapper<ManagedBuffer>;

#[view(getSum)]
#[storage_mapper("sum")]
fn sum(&self) -> SingleValueMapper<BigUint>;
Expand All @@ -27,5 +36,48 @@ pub trait ReturnsTokenIdentifierTest {
fn add(&self, value: BigUint) {
self.sum().update(|sum| *sum += value);
}
// TODO!: Call the proxy-test issue function!!

#[payable("*")]
#[endpoint]
fn call_contract(
&self,
contract_address: &ManagedAddress,
token_display_name: ManagedBuffer,
token_ticker: ManagedBuffer,
initial_supply: BigUint,
) {
let caller = self.blockchain().get_caller();
self.tx()
.to(contract_address)
.typed(proxy_proxy_test::ProxyTestProxy)
.issue_fungible_token(token_display_name, token_ticker, initial_supply)
.with_callback(self.callbacks().esdt_issue_callback(&caller))
.async_call_and_exit();
}

#[callback]
fn esdt_issue_callback(
&self,
caller: &ManagedAddress,
#[call_result] result: ManagedAsyncCallResult<()>,
) {
let (token_identifier, returned_tokens) = self.call_value().egld_or_single_fungible_esdt();
// callback is called with ESDTTransfer of the newly issued token, with the amount requested,
// so we can get the token identifier and amount from the call data
match result {
ManagedAsyncCallResult::Ok(()) => {
self.last_issued_token().set(token_identifier.unwrap_esdt());
self.last_error_message().clear();
},
ManagedAsyncCallResult::Err(message) => {
// return issue cost to the caller
if token_identifier.is_egld() && returned_tokens > 0 {
self.tx().to(caller).egld(&returned_tokens).transfer();
}

self.last_error_message().set(&message.err_msg);
},
}
}
// erd1qqqqqqqqqqqqqpgqyu0yylry6dyvynsqsmswqzvyewes38j0a4sqevrx55
}
Loading

0 comments on commit 88a84e3

Please sign in to comment.