Cookie Consent by Free Privacy Policy Generator 📌 Building an Escrow Smart Contract


✅ Building an Escrow Smart Contract


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

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.

Escrow Contract

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{...}

State Variables

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.

Constructor and State Variables Initialization

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.

Arbiter and Transfer Approval

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 the approve() 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 the this 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 the approve() function executes successfully

Arbiter Authorization

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 the msg.sender of the deposit transfer is not the arbiter. The msg.sender signifies the address that is approving the transfer from the escrow to the beneficiary.

Emitting Events

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);
}

Conclusion

I hope you enjoyed reading this short tutorial. We have just touched on one of the first concepts in blockchain development. The smart contract source code is on GitHub.

You can go ahead and build other use cases for smart contracts and improve your knowledge of Solidity. The Solidity documentation is a great place to start. You can also learn more about Ethereum development from the Ethereum documentation.

Are there any other concepts you would like me to write about? Share them with me! You can find me on Twitter.

Cover Photo by Mikhail Nilov: https://www.pexels.com/photo/people-having-a-meeting-7731327/

...

✅ Building an Escrow Smart Contract


📈 49.29 Punkte

✅ Solidity Alchemy - Course (ESCROW SMART CONTRACT)


📈 42.2 Punkte

✅ Secure your blockchain smart contract via smart contract auditing


📈 29.51 Punkte

✅ Cancel an Escrow


📈 27.45 Punkte

✅ Escrow-Betrug: Altenen-Beteiligte betrügen eigene Kunden


📈 27.45 Punkte

✅ Bitcoins of DarkSide ransomware gang still locked in hacker forum’s escrow


📈 27.45 Punkte

✅ Security Awareness On GoDaddy Employee Phishing Scheme Jeopardized Escrow.com, Among Others


📈 27.45 Punkte

✅ AlstraSoft EPay Enterprise escrow.htm cross site scripting


📈 27.45 Punkte

✅ Windows 10 Whole-Disk Encryption without Key Escrow


📈 27.45 Punkte

✅ Windows 10 Whole-Disk Encryption without Key Escrow


📈 27.45 Punkte

✅ XXL-SPECIAL: Neue Canton Smart Produkte Smart GLE 3, Smart GLE 5 und Smart Sounddeck 100


📈 19.23 Punkte

✅ How Smart Building Technologies are Improving Building Operations and Maintenance


📈 18.99 Punkte

✅ Building a Winning Government Contract Bid Team


📈 17.03 Punkte

✅ Key learnings for building smart communities at Smart City Expo World Congress 2019


📈 16.71 Punkte

✅ RCKT_Coin mintToken Smart Contract erweiterte Rechte


📈 14.75 Punkte

✅ tickets mintToken Smart Contract erweiterte Rechte


📈 14.75 Punkte

✅ GreenEnergyToken mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ GFC mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ Extreme Coin sell Smart Contract privilege escalation


📈 14.75 Punkte

✅ VITToken mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ SwapToken sellBuyerTokens Smart Contract privilege escalation


📈 14.75 Punkte

✅ Crowdsale mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ Ethereum Smart Contract Backdoored Using Malicious Constructor


📈 14.75 Punkte

✅ Bitstarti mintToken Smart Contract erweiterte Rechte


📈 14.75 Punkte

✅ Introduction to BitPower Smart Contract


📈 14.75 Punkte

✅ The Evolution of Smart Contract Threats: Staying Ahead with Proactive Auditing


📈 14.75 Punkte

✅ Spadeico mint Smart Contract privilege escalation


📈 14.75 Punkte

✅ Substratum Ethereum ERC20 Token mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ HitToken mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ Trabet_Coin_PreICO mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ wellieat mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ Nectar mintToken Smart Contract privilege escalation


📈 14.75 Punkte

✅ ATLANT mint Smart Contract erweiterte Rechte


📈 14.75 Punkte

✅ Ether Cartel DrugDealer Smart Contract erweiterte Rechte


📈 14.75 Punkte

✅ RRToken mintToken Smart Contract privilege escalation


📈 14.75 Punkte











matomo

Datei nicht gefunden!