#how to call Approve function from frontend

14 messages · Page 1 of 1 (latest)

lone sail
#

i am a bit confuse on how how to use approve function from front end.
this is my contract function

pub fn pay_merchant(env: &Env, merchant: Address, amount: i128, user: Address) -> Result<(), Error> {
        if paused(&env) {
            return Err(Error::Paused);
        }
        if amount <= 0 {
            return Err( Error::InvalidAmount);
        }
        // if merchant == Address::from_str(&env, "0") {
        //     panic_with_error!(&env, Error::InvalidAddress);
        // }

       

        let token_address = env.storage().instance().get(&DataKey::Sep41)
            .ok_or(Error::NotFound)?;

        let token = token::Client::new(&env, &token_address);
        let contract_address = env.current_contract_address();

        // Calculate fees
        let (fee_num, fee_den) = Self::get_fees_parameter(&env);
        let fee = (amount * fee_num as i128) / (100 * fee_den as i128);
        let merchant_amount = amount - fee;

        token.transfer_from(&contract_address,&user, &contract_address, &amount);
        token.transfer(&contract_address, &merchant, &merchant_amount);

        // Update admin fees
        let mut admin_deposit: i128 = env.storage().instance()
            .get(&DataKey::AdminTotalDeposit)
            .unwrap_or(0);
        admin_deposit += fee;
        env.storage().instance().set(&DataKey::AdminTotalDeposit, &admin_deposit);

        // Emit event
        env.events().publish(
            (symbol_short!("payment"), user),
            (merchant, amount, merchant_amount)
        );

        Ok(())
    }
grave cargo
#

What do you mean by approve function?, do you mean authentication?

lone sail
#

approve and transferFrom function in a smart contract token

#

transferFrom can be used in smart contract but approval need to be called from front end

pale marsh
#

Looking at your pay_merchant function, seems like what you're trying to achieve is:

  • the user send a payment, we'll say $100
  • the contract address keeps some fee, let's say 1% so $1
  • the merchant is paid the remaining $99

does that sound right?

#

If so, you don't really need to use the transfer_from function at all. you could do something like:

let merchant_amount = amount - fee;
token.transfer(&user, &contract_address, &fee);
token.transfer(&user, &merchant, &merchant_amount);
#

This would require you to have a user.require_auth(); call in your function, which you probably need anyway

#

if you really do need to use transfer_from, though, you actually could just do a token.approve(&user, &contract_address, &amount, &whatever_your_expiration_ledger_is); right ahead of the transfer from function. again, though, i don't really think that's necessary

lone sail
#

If I don't use transfer_from, then how can I accept the payment from user in smart contract?

lone sail
#

Is it different in stellar from the way it works in ethereum chain?

lone sail
#

@pale marsh ?

grave cargo
#

That's not how it works in soroban

#

You just need to create a token client. let token_client = TokenClient::new(&env, &token);