Ethereum Smart Contract Vulnerabilities and Security Detection

When a vulnerability in smart contract security is exposed, it also plummets the coin market. Hackers can easily exploit the holes to make unlimited issues and free transfer of virtual currencies…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Developing a Smart Contract on Elrond Blockchain Network

Develop, build and deploy a Smart Contract on the Elrond Network

Prerequisites for your System (you can follow the links if you didn't install those tools yet):

Let's start by creating a new contract on the command line in a folder of your choice:

We go through every part of that first command.

Here is the official explanation of the erdpy CLI purpose:

The command group contract is for building, deploying, upgrading, and interacting with Smart Contracts.

With new, we create a new Smart Contract based on a template.

After this, we specify the name of our new Smart Contract crowdfunding.

The last is an optional argument template with which we specify the template to use. We use empty which creates a Smart Contract with just the necessary base structure.

Please open up the newly created project in VS Code. You should see one folder in your Explorer crowdfunding that holds everything we need for creating our contract.

We will do some housekeeping first, and change the path value in the Cargo.toml file from “src/empty.rs” to “src/crowdfunding.rs” to declare that this file will contain the code for our Smart Contract. Now we need to rename the file in the src folder from empty.rs to crowdfunding.rs and replace the contents of the file with the following:

Let's go through does few lines.

Rust is not specially written for just Smart Contract development like Solidity. In the first line, we say that we don't wanna load the standard libraries to make our Smart Contract very lightweight.

The next line imports the Elrond Framework to simplify your Smart Contract.

With the next line, we say that the following is a Smart Contract. After that line, our Smart Contract starts with pub trait Crowdfunding.

For what does the keyword trait stand for in Rust?

The next two lines define the constructor method, which will only run once when you deploy the Smart Contract to the network. It must be annotated with #[init]

What are Smart Contract annotations?

Let's build our new contract.

This might take a while. If the command was successful you now see a new subfolder in the crowdfunding folder > output.

We define values that should be stored on-chain. We can achieve that by annotations. All methods defined for storage handling must not have an implementation.

We need three on-chain storage keys first a configured desired target amount, and a deadline expressed as a block timestamp after which the contract cannot be funded, the deposit is the received funds it has an extra argument for the donor address.

The values for target and deposit are a sum of EGLD and will be stored as 1 EGLD = 10¹⁸ EGLD-wei, all payments are expressed in wei.

Next, we will add two more arguments to the constructor of two of our storage fields and two checks that the target amount needs to be higher than 0 and the deadline must be in the future.

Next, we define a publicly accessible endpoint to keep track of who donated how much, it's a payable function that will receive EGLD.

We add two more lines to our fund function, the new lines are marked bold. To prevent funding after the deadline has passed we reject all payments that come in after the defined deadline.

We need to define an endpoint to read the actual status of the funding. These lines must be added outside of the contract.

We need to add another import to our file underneath the first import:

We add two more methods into our contract trait to receive the actual status and the current funding with getCurrentFunds.

Finally, we need a function to claim the funding. In this function, we prevent claiming before the deadline is reached. If the funding was successful, the owner receives the current balance. If the funding failed only donors can claim their invested amounts.

Save the security phrase (24 words) in a safe place.

Login into your Devnet Wallet and click on Faucet to get some funds. For sending transactions we need some xEGLD for the gas fees.

We generate a private key PEM file so we don't need to type in our password every time to confirm transactions.

You need to type in the just received security phrase of 24 words separated by whitespace.

Create a file in the crowdfunding folder erdpy.json and put the following content into it:

Build & Deploy the contract with the following commands:

Somewhere in the command line output, you will see a line like the following:

In an upcoming article, we will build the frontend for this Smart Contract. You can see a sneak preview below.

Add a comment

Related posts:

loving you as a whole

when you love someone as a whole,. “loving you as a whole” is published by a n o n i m..

Value Iteration

This post is a summary of Lecture 1 of Deep RL Bootcamp 2017 at UC Berkely. All of the figures, equations, and text are taken from the lecture slides and videos available here. RL problems are…

What exactly does it mean to be a business consultant?

Business consultant. It’s one of those nebulous titles almost as enigmatic as one who goes to work each day to perform his job as internal optimization coordinator, product integration analyst, or…