According to Wikipedia, Escrow is a contractual agreement between two transacting parties whereby a third party holds the funds and makes the disbursement based on certain agreed conditions. An example is a trust account holding funds in a person's name to pay taxes or insurance fees.
It is the bread and butter of Ethereum smart contracts. When a depositor initiates an escrow transaction, the value (Ether) is held in the escrow contract until the beneficiary has fulfilled an agreed task.
This code demonstrates an escrow written in Solidity, the prominent language of the Ethereum Virtual Machine (EVM). The contract is called Escrow
in the Escrow.sol
file:
contract Escrow{...}
Variables hold values that are persistently stored in the blockchain (the Ethereum ledger here). The smart contract in this code defines a depositor
of value, a beneficiary
of transferred value and an arbiter
who alone can approve a transfer transaction. Therefore, they are defined early enough in the contract.
Contract Escrow {
address public depositor;
address public beneficiary;
address public arbiter;
...
}
- the
address
data type in Solidity is a 20-byte value representing an Ethereum address. We used it for our variables here since they will be EOAs or smart contracts with an Ethereum address.
Here, the state variables are initialized in the constructor of the contract. This is similar to how classes are defined in some object-oriented languages like C#.
contract Escrow {
...
constructor(address _arbiter, address _beneficiary) payable {
arbiter = _arbiter;
beneficiary = _beneficiary;
depositor = msg.sender;
}
}
In the above code, the depositor deploys the contract and, the msg.sender
address assumes the position of the depositor in our escrow contract. The msg.sender
is a globally available identifier for the externally owned account (EOA) or another smart contract calling our escrow smart contract. Read more about the use of msg.sender
in this metaschool.so answer by Munim Iftikhar.
The depositor being the deployer of the contract obtains the addresses of the arbiter and beneficiary and stores the addresses as arguments to our contract for storage as state variables.
It's noteworthy to point out that the payable
keyword in the constructor code above identifies the contract as being able to pay the beneficiary after the arbiter approves the value transfer. The payable
keyword is used in Solidity to signify the ability to accept ether. Ether is the denomination of payment for EVM computation.
In the following code, we will write the logic to transfer the value in the escrow balance to the beneficiary whose address we saved in the state storage. This is how the arbiter will be able to approve the transfer of the deposit to the beneficiary. Let's create a function approve()
for the process:
...
function approve() external {
uint balance = address(this).balance;
(bool success, ) = beneficiary.call{ value: balance }("");
require(success);
}
-
the
external
keyword in the function definition indicates that theapprove()
function can only be used outside of the contract by other contracts. -
address(this).balance
allows us to access the constructor's balance with its address using thethis
keyboard. This is the value in the escrow. In some languages like JavaScript, this keyword helps to access variables defined within a specified scope of usage. -
we made a message call to beneficiary transfering the escrow balance to the beneficiary
-
we used the
require(success)
line to handle when theapprove()
function executes successfully
We need to allow only the arbiter to approve the deposit transfer. Therefore, we will create an Error to be returned once the arbiter is not the approver.
...
error NotAuthorized();
function approve() external {
if(msg.sender != arbiter) revert NotAuthorized();
...
require(success);
}
...
In the code above:
-
we specify our custom error titled "NotAuthorized"
-
In the approve() function, we used the
revert
keyword to throw NotAuthorized when themsg.sender
of the deposit transfer is not the arbiter. Themsg.sender
signifies the address that is approving the transfer from the escrow to the beneficiary.
Events are a way to emit data for front-end systems and servers consuming or working with our contract. They can listen to events and get up-to-date information for their users and processes alike. Let us add an event, Approved(uint)
which we will use to emit the escrow balance
that the arbiter transfers to the beneficiary.
...
event Approved(uint);
...
function approve() external {
...
emit Approved(balance);
}