Payment Splitter
This example shows how you can reuse the implementation of payment-splitter.
#
Step 1: Include dependenciesInclude brush
as dependency in the cargo file or you can use default Cargo.toml
template.
After you need to enable default implementation of Payment Splitter via brush
features.
brush = { tag = "v1.6.1", git = "https://github.com/Supercolony-net/openbrush-contracts", default-features = false, features = ["payment_splitter"] }
# payment-splitter uses dividing inside, so your version of rust can require you to disable check overflow.[profile.dev]
#
Step 2: Add imports and enable unstable featureUse brush::contract
macro instead of ink::contract
. Import everything from brush::contracts::payment_splitter
.
#![cfg_attr(not(feature = "std"), no_std)]#![feature(min_specialization)]
#[brush::contract]pub mod my_payment_splitter { use brush::contracts::payment_splitter::*; use ink_prelude::vec::Vec; use ink_storage::traits::SpreadAllocate;
#
Step 3: Define storageDeclare storage struct and declare the field related to PaymentSplitterStorage
Then you need to derive PaymentSplitterStorage
trait and mark corresponding field
with #[PaymentSplitterStorageField]
attribute. Deriving this trait allows you to reuse
the default implementation of PaymentSplitter
.
#[ink(storage)]#[derive(Default, SpreadAllocate, PaymentSplitterStorage)]pub struct SplitterStruct { #[PaymentSplitterStorageField] splitter: PaymentSplitterData,}
#
Step 4: Inherit logicInherit the implementation of PaymentSplitter
. You can customize (override) methods in this impl
block.
impl PaymentSplitter for SplitterStruct {}
#
Step 5: Define constructorDefine constructor. Your basic version of PaymentSplitter
contract is ready!
impl SplitterStruct { #[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.