Payment Splitter
Split payouts equally among a list of specified payees
A payment splitter can be used for example to create a shared project donation address, ensuring that all payees receive the same amount
Sending lovelace to the contract works similarly to sending lovelace to any other address. The payout transaction can only be submitted by one of the payees, and the output addresses are restricted to the payees. The output sum must be equally divided to ensure the transaction is successful.
There are 2 actions (or endpoints) available to interact with this smart contract:
- Send Lovelace to Payment Splitter
- Trigger Payout
To initialize the payment splitter, we need to initialize a provider, a MeshTxBuilder
, and a MeshPaymentSplitterContract
. Additionally, a list of payees is required to define the allowed payout addresses for the contract.
import { BlockfrostProvider, MeshTxBuilder } from '@meshsdk/core'; import { MeshPaymentSplitterContract } from '@meshsdk/contracts'; import { useWallet } from '@meshsdk/react'; const { connected, wallet } = useWallet(); const blockchainProvider = new BlockfrostProvider(APIKEY); const meshTxBuilder = new MeshTxBuilder({ fetcher: blockchainProvider, submitter: blockchainProvider, }); const contract = new MeshPaymentSplitterContract( { mesh: meshTxBuilder, fetcher: blockchainProvider, wallet: wallet, networkId: 0, }, [ 'addr_test1vpg334d6skwu6xxq0r4lqrnsjd5293n8s3d80em60kf6guc7afx8k', 'addr_test1vp4l2kk0encl7t7972ngepgm0044fu8695prkgh5vjj5l6sxu0l3p', 'addr_test1vqqnfs2vt42nq4htq460wd6gjxaj05jg9vzg76ur6ws4sngs55pwr', 'addr_test1vqv2qhqddxmf87pzky2nkd9wm4y5599mhp62mu4atuss5dgdja5pw', ] );
Both on-chain and off-chain codes are open-source and available on Mesh Github Repository.
Send Lovelace to Payment Splitter
sendLovelaceToSplitter()
will lock Lovelace in the contract. The function accepts the following parameters:
- lovelaceAmount (number) - the amount of Lovelace you want to send to the contract
The function returns a transaction hash.
This demo, shows how to send 10000000 Ada to the payment splitter.
const lovelaceAmount: number = 10000000; const txHash = await contract.sendLovelaceToSplitter(lovelaceAmount);
Trigger Payout
triggerPayout()
will split the locked amount equally among the list of payees. The function doesn't need any parameters.
The function returns a transaction hash if the payout has been done successfully.
This demo, we will trigger the payout of the locked amount equally among the list of payees. The amount has been locked in the previous step.
const txHash = await contract.triggerPayout();