Skip to content

Commit

Permalink
make forward payable and allow it to cover fees
Browse files Browse the repository at this point in the history
  • Loading branch information
alyn509 committed Sep 19, 2023
1 parent 708298f commit 8df5ef6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
17 changes: 14 additions & 3 deletions contracts/examples/digital-cash/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,23 @@ pub trait HelpersModule: storage::StorageModule {
});
}

fn update_fees(&self, address: &ManagedAddress, payment: EgldOrEsdtTokenPayment) {
fn update_fees(
&self,
caller_address: ManagedAddress,
address: &ManagedAddress,
payment: EgldOrEsdtTokenPayment,
) {
self.check_token_is_accepted_as_fee(&payment.token_identifier);
let caller_address = self.blockchain().get_caller();
let deposit_mapper = self.deposit(address);
if !deposit_mapper.is_empty() {
deposit_mapper.update(|deposit| deposit.fees.value += payment.amount);
deposit_mapper.update(|deposit| {
require!(

Check failure on line 84 in contracts/examples/digital-cash/src/helpers.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

proc macro panicked

Check failure on line 84 in contracts/examples/digital-cash/src/helpers.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

cannot apply unary operator `!` to type `multiversx_sc::types::ManagedAddress<<Self as ContractBase>::Api>`

Check failure on line 84 in contracts/examples/digital-cash/src/helpers.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

proc macro panicked

Check failure on line 84 in contracts/examples/digital-cash/src/helpers.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

cannot apply unary operator `!` to type `multiversx_sc::types::ManagedAddress<<Self as ContractBase>::Api>`
deposit.depositor_address,
caller_address,
"invalid depositor address"
);
deposit.fees.value += payment.amount;
});
return;
}

Expand Down
6 changes: 4 additions & 2 deletions contracts/examples/digital-cash/src/pay_fee_and_fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub trait PayFeeAndFund: storage::StorageModule + helpers::HelpersModule {
fn pay_fee_and_fund(&self, address: ManagedAddress, valability: u64) {
let mut payments = self.call_value().all_esdt_transfers().clone_value();
let fee = EgldOrEsdtTokenPayment::from(payments.get(0));
self.update_fees(&address, fee);
let caller_address = self.blockchain().get_caller();
self.update_fees(caller_address, &address, fee);

payments.remove(0);

Expand All @@ -36,6 +37,7 @@ pub trait PayFeeAndFund: storage::StorageModule + helpers::HelpersModule {
#[payable("EGLD")]
fn deposit_fees(&self, address: &ManagedAddress) {
let payment = self.call_value().egld_or_single_esdt();
self.update_fees(address, payment);
let caller_address = self.blockchain().get_caller();
self.update_fees(caller_address, address, payment);
}
}
40 changes: 22 additions & 18 deletions contracts/examples/digital-cash/src/signature_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,56 @@ pub trait SignatureOperationsModule: storage::StorageModule + helpers::HelpersMo
}

#[endpoint]
#[payable("*")]
fn forward(
&self,
address: ManagedAddress,
forward_address: ManagedAddress,
signature: ManagedByteArray<Self::Api, ED25519_SIGNATURE_BYTE_LEN>,
) {
let deposit_mapper = self.deposit(&forward_address);
require!(!deposit_mapper.is_empty(), CANNOT_DEPOSIT_FUNDS_ERR_MSG);

let fee = self.call_value().egld_or_single_esdt();
let caller_address = self.blockchain().get_caller();
let fee = self.fee().get();
self.require_signature(&address, &caller_address, signature);
self.update_fees(caller_address, &address, fee);

let mut forwarded_deposit = self.deposit(&address).take();
let num_tokens = forwarded_deposit.get_num_tokens();
deposit_mapper.update(|deposit| {
let new_deposit = self.deposit(&forward_address);
let fee = self.fee().get();

let mut current_deposit = self.deposit(&address).take();
let num_tokens = current_deposit.get_num_tokens();
new_deposit.update(|fwd_deposit| {
require!(
deposit.egld_funds == BigUint::zero() && deposit.esdt_funds.is_empty(),
fwd_deposit.egld_funds == BigUint::zero() && fwd_deposit.esdt_funds.is_empty(),
"key already used"
);
require!(
&fee * num_tokens as u64 <= deposit.fees.value,
&fee * num_tokens as u64 <= fwd_deposit.fees.value,
"cannot forward funds without the owner covering the fee cost first"
);

deposit.fees.num_token_to_transfer += num_tokens;
deposit.valability = forwarded_deposit.valability;
deposit.expiration_round = self.get_expiration_round(forwarded_deposit.valability);
deposit.esdt_funds = forwarded_deposit.esdt_funds;
deposit.egld_funds = forwarded_deposit.egld_funds;
fwd_deposit.fees.num_token_to_transfer += num_tokens;
fwd_deposit.valability = current_deposit.valability;
fwd_deposit.expiration_round = self.get_expiration_round(current_deposit.valability);
fwd_deposit.esdt_funds = current_deposit.esdt_funds;
fwd_deposit.egld_funds = current_deposit.egld_funds;
});

let forward_fee = &fee * num_tokens as u64;
forwarded_deposit.fees.value -= &forward_fee;
current_deposit.fees.value -= &forward_fee;

self.collected_fees()
.update(|collected_fees| *collected_fees += forward_fee);

if forwarded_deposit.fees.value > 0 {
if current_deposit.fees.value > 0 {
self.send_fee_to_address(
&forwarded_deposit.fees.value,
&forwarded_deposit.depositor_address,
&current_deposit.fees.value,
&current_deposit.depositor_address,
);
}
}

fn make_forward(&self) {}

fn require_signature(
&self,
address: &ManagedAddress,
Expand Down

0 comments on commit 8df5ef6

Please sign in to comment.