Using PaymentSplitter with Remix

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.

We will deploy to the local JavaScript VM, but any network can be used with injected Web3 (such as MetaMask).

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.

PaymentSplitter follows 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 release function.

Import

To deploy a PaymentSplitter with Remix we first need to import the contract via GitHub.

(see the Remix documentation for Importing from GitHub)

:warning: 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 PaymentSplitter:

// OpenZeppelinContracts.sol
pragma solidity ^0.6.2;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.1/contracts/payment/PaymentSplitter.sol";

Compile

We can then compile our file using the Solidity Compiler plugin and PaymentSplitter will be imported and compiled.

Deploy

The PaymentSplitter 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 PaymentSplitter where each account in payees is assigned the number of shares at the matching position in the shares array.

All addresses in payees must be non-zero. Both arrays must have the same non-zero length, and there must be no duplicates in payees .

In the deploy we provide the payees and shares.
The following specifies two payees with equal shares.
["0x3D97c3C7986Bd9eB24C37066E650cC7563184199","0x67e45FE748d5B76ED59E6400842170DE78318C80"],[1,1]

In the Deploy and Run Transactions plugin, set the contract to PaymentSplitter and next to the Deploy button in Remix specify the array of payees and shares such as the example above.

Send payment

Once the 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.

paymentsplitter_send

From the API documentation for the receive function:

The Ether received will be logged with PaymentReceived events. 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.

Release

To release the share of payments for a specific payee, we need to call the release function with the payee address.

In Remix enter our payee address, in this tutorial we use 0x67e45FE748d5B76ED59E6400842170DE78318C80 and press release.
paymentsplitter_release

From the API documentation for the release function:

Triggers a transfer to account of the amount of Ether they are owed, according to their percentage of the total shares and their previous withdrawals.