How to divide amount unequally in Plutus Smart Contracts

94 Views Asked by At

I need to make a smart contract using Plutus to divide an amount to 2 wallet unequally so I can define custom fees for each transaction. I am using this documentation. What I need is that I would like to split the amount to two unequal amounts and send each to a separate wallet. Assume a user wants to send 7$ to wallet B from his wallet A. The additional fee which I have defined before for each transaction is 2$. using code block below, How can I divide 9$ to 7$ and 2$, then send them to wallets B and C (C is my wallet).

validateSplit :: SplitData -> () -> ScriptContext -> Bool
validateSplit SplitData{recipient1, recipient2, amount} _ ScriptContext{scriptContextTxInfo} =
    let half = Ada.divide amount 2 in
    Ada.fromValue (valuePaidTo scriptContextTxInfo recipient1) >= half &&
    Ada.fromValue (valuePaidTo scriptContextTxInfo recipient2) >= (amount - half)
1

There are 1 best solutions below

0
On

Well if you know your fee beforehand, why not include it in the parameter list recipient1, recipient2, amount, fee. So then you can subtract it accordingly. Or if it is always 2 Ada and will always be 2 Ada, then simply hardcode it in the contract. How else should the contract know how much is the fee which goes to your wallet (C) and how much should go to the other wallet (B).