Skip to main content
Version: v2.3.0

Payment Splitter

This example shows how you can reuse the implementation of payment-splitter.

Step 1: Import default implementation

With default Cargo.toml, you need to import the payment_splitter module, enable the corresponding feature, and embed the module data structure as described in that section.

The main trait is PaymentSplitter.

Step 2: Define constructor

Define constructor where you init payees and shares.

impl Contract {
#[ink(constructor)]
pub fn new(payees_and_shares: Vec<(AccountId, Balance)>) -> Self {
ink_lang::codegen::initialize_contract(|instance: &mut Self| {
instance._init(payees_and_shares).expect("Should init");
})
}
}

You can check an example of the usage of PaymentSplitter.

Step 3 (Optional): Customize your contract

The PaymentSplitter trait defines and has default implementations for the core payment splitter functions. Additional functionality with some predefined functions is available through the payment_splitter::Internal trait. Likely the most common function to use from this internal trait will be _release_all. This allows you to payout all payees stored in the contract at once. To add this function to your contract, simply define a new publicly dispatchable function (i.e. #[ink(message)]) called release_all and have it call the internal _release_all function using self.

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[openbrush::contract]
pub mod my_payment_splitter {
use ink_prelude::vec::Vec;
use ink_storage::traits::SpreadAllocate;
use openbrush::contracts::payment_splitter::*;
use openbrush::traits::Storage;

#[ink(storage)]
#[derive(Default, SpreadAllocate, Storage)]
pub struct Contract {
#[storage_field]
splitter: payment_splitter::Data,
}

impl Contract {
#[ink(constructor)]
pub fn new(payees_and_shares: Vec<(AccountId, Balance)>) -> Self {
ink_lang::codegen::initialize_contract(|instance: &mut Self| {
instance._init(payees_and_shares).expect("Should init");
})
}

/// Payout all payees at once.
/// Delete this method if you don't want this functionality in your version of the payment splitter.
#[ink(message)]
pub fn release_all(&mut self) -> Result<(), PaymentSplitterError> {
// `_release_all()` is an internal method defined by the `payment_splitter::Internal` trait
self._release_all()
}
}

impl PaymentSplitter for Contract {}
}

The _add_payee function is also available in the payment_splitter::Internal trait and can be added to your contract in the same way as _release_all.