Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

solidity
communitycreator

What is the Solidity withdrawal pattern?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The withdrawal pattern

The withdrawal pattern lays the burden of proof on the fund receiver. The fund recipient must send a transaction to withdraw and retrieve their money.

This simplifies a smart contract that sends payments to recipients. This is because the contract doesn’t have to deal with the funds not being delivered. A smart contract has no way of knowing whether the money was sent due to an error or whether the receiver is a malevolent smart contract refusing to accept the purpose payments.

Using the withdrawal pattern

The article, Building Ethereum Payment Channels, raised the question of why shutting a channel adds funds to a balance rather than simply returning cash to channel members. This process is called the withdrawal pattern. This part of the shot explains why it is required. The problem develops when an Ethereum transaction attempts to transmit funds to an address, not under the sender’s control. The following is a much-simplified sample contract that does this:

pragma solidity ^0.5.0;
contract BadSplitter {
uint256 funds;
address sender;
address recipient;
// Deposit some funds in to the contract
function deposit(address other) payable {
funds = msg.value;
sender = msg.sender;
recipient = other;
}
// Split an amount of funds between the sender and the recipient
function split() {
// Can only be called by the recipient
require(msg.sender == recipient);
// Split the funds
sender.transfer(funds / 2);
recipient.transfer(funds / 2);
}
}

Explanation

Please notice that this contract is just meant to be used as an example and is completely unsuited for actual work.The following is how this contract works.

The sender makes a deposit() transaction, and the receiver submits a split() transaction with the recipient’s address. This sends half of the deposit to the recipient and half back to the sender. In-line comments are also provided for better understanding. We can also explain this graphically in the following way:

Graphcal Illustration
Graphcal Illustration

RELATED TAGS

solidity
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring