Using PaymentSplitter with Remix
This guide will walk through the process of deploying
PaymentSplitter contract from OpenZeppelin Contracts with Remix, sending payments and then calling
release to pull payments for a payee.
From the API documentation for PaymentSplitter:
This contract allows to split Ether payments among a group of accounts. The sender does not need to be aware that the Ether will be split in this way, since it is handled transparently by the contract.
The split can be in equal parts or in any other arbitrary proportion. The way this is specified is by assigning each account to a number of shares. Of all the Ether that this contract receives, each account will then be able to claim an amount proportional to the percentage of total shares they were assigned.
PaymentSplitterfollows a pull payment model. This means that payments are not automatically forwarded to the accounts but kept in this contract, and the actual transfer is triggered as a separate step by calling the
To deploy a PaymentSplitter with Remix we first need to import the contract via GitHub.
(see the Remix documentation for Importing from GitHub)
For OpenZeppelin Contracts you should only use code published in an official release, the example below imports from OpenZeppelin Contracts v3.0.1.
Start by creating a file on Remix to import the contract that we need from OpenZeppelin Contracts such as
OpenZeppelinContracts.sol with the following contents which is the Solidity version to use and the import statement for
// OpenZeppelinContracts.sol pragma solidity ^0.6.0; import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.1/contracts/payment/PaymentSplitter.sol";
We can then compile our file using the Solidity Compiler plugin and
PaymentSplitter will be imported and compiled.
constructor accepts an array of addresses, the payees and an array of integers, the shares corresponding to each payee.
From the API documentation for the constructor:
Creates an instance of
PaymentSplitterwhere each account in
payeesis assigned the number of shares at the matching position in the
All addresses in
payeesmust be non-zero. Both arrays must have the same non-zero length, and there must be no duplicates in
In the deploy we provide the
The following specifies two
payees with equal shares.
In the Deploy and Run Transactions plugin, set the contract to
PaymentSplitter and next to the
Deploy button in Remix specify the array of
shares such as the example above.
PaymentSplitter has been deployed, payments can be sent to the contract.
In Remix to send value to a contract, with the contract set to our
PaymentSplitter, set the Value to the amount to send, such as 1 ether and then press the
Transact button at Low level interactions section at the bottom of the page.
From the API documentation for the receive function:
The Ether received will be logged with
PaymentReceivedevents. Note that these events are not fully reliable: it’s possible for a contract to receive Ether without triggering this function. This only affects the reliability of the events, and not the actual splitting of Ether.
To learn more about this see the Solidity documentation for fallback functions.
To release the share of payments for a specific payee, we need to call the
release function with the
In Remix enter our
payee address, in this tutorial we use
0x67e45FE748d5B76ED59E6400842170DE78318C80 and press release.
From the API documentation for the release function:
Triggers a transfer to
accountof the amount of Ether they are owed, according to their percentage of the total shares and their previous withdrawals.