Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the ethernet square crowdfunding intelligent contract

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize the ethernet square crowdfunding intelligent contract". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to realize the ethernet square crowdfunding intelligent contract.

Achieving a good idea often takes a lot of effort and a lot of money. We can ask for donations from users, or investment from investment institutions, but this is often difficult. For donations, the domestic atmosphere is not very good, the whereabouts of funds often go nowhere, and donors have long lost confidence in the current form of donation. Venture capital, however, is very difficult for entrepreneurs who have no connections. Blockchain provides a new form of crowdfunding-crowdfunding smart contracts. The fundraiser sets the crowdfunding goal through the crowdfunding contract, as well as the completion time, and sets the actions corresponding to different crowdfunding results (for example, if the target fails, the full money will be returned, and the beneficiary will receive an encrypted token or ETH if the target succeeds). Due to the untamperable nature of the blockchain, mass dating is a very consistent application scenario.

Tokens and distributed autonomous organizations

In this example, we will do better crowdfunding by solving two important issues:

How to manage funds to ensure liquidity

How to spend money after raising money.

Crowdfunding projects before the emergence of blockchain are generally lack of liquidity, investors will not be able to participate in crowdfunding once they miss the crowdfunding deadline; once they participate in crowdfunding, investors can not quit halfway. Smart contracts record investment in the form of tokens and provide liquidity similar to that of the stock market. Investors can choose to trade or continue to hold. After the success of the project, investors can use tokens to exchange in kind or products and services. If the project fails, the investor can withdraw in accordance with the original agreement and continue to hold tokens as a token of memory.

Similarly, the current crowdfunding projects also have the problem of unknown whereabouts of funds. In this project, we use DAO (distributed Autonomous Organization) to record the whereabouts of each fund.

Contract code

Put the code first, and then read it step by step.

Pragma solidity ^ 0.4.16 X interface token {function transfer (address receiver, uint amount);} contract Crowdsale {address public beneficiary; uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price; token public tokenReward; mapping (address = > uint256) public balanceOf; bool fundingGoalReached = false; bool crowdsaleClosed = false; event GoalReached (address recipient, uint totalAmountRaised); event FundTransfer (address backer, uint amount, bool isContribution) / * * Constrctor function * * Setup the owner * / function Crowdsale (address ifSuccessfulSendTo, uint fundingGoalInEthers, uint durationInMinutes, uint etherCostOfEachToken, address addressOfTokenUsedAsReward) {beneficiary = ifSuccessfulSendTo; fundingGoal = fundingGoalInEthers * 1 ether; deadline = now + durationInMinutes * 1 minutes; price = etherCostOfEachToken * 1 ether TokenReward = token (addressOfTokenUsedAsReward);} / * * Fallback function * * The function without name is the default function that is called whenever anyone sends funds to a contract * / function () payable {require (! crowdsaleClosed); uint amount = msg.value; balanceOf [msg.sender] + = amount; amountRaised + = amount; tokenReward.transfer (msg.sender, amount / price) FundTransfer (msg.sender, amount, true);} modifier afterDeadline () {if (now > = deadline) _;} / * * Check if goal was reached * * Checks if the goal or time limit has been reached and ends the campaign * / function checkGoalReached () afterDeadline {if (amountRaised > = fundingGoal) {fundingGoalReached = true; GoalReached (beneficiary, amountRaised) } crowdsaleClosed = true;} / * * Withdraw the funds * * Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached, * sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw * the amount they contributed. * / function safeWithdrawal () afterDeadline {if (! fundingGoalReached) {uint amount = balanceOf [msg.sender]; balanceOf [msg.sender] = 0; if (amount > 0) {if (msg.sender.send (amount)) {FundTransfer (msg.sender, amount, false) } else {balanceOf [msg.sender] = amount;} if (fundingGoalReached & & beneficiary = = msg.sender) {if (beneficiary.send (amountRaised)) {FundTransfer (beneficiary, amountRaised, false) } else {/ / If we fail to send the funds to beneficiary, unlock funders balance fundingGoalReached = false;}

In the constructor

FundingGoal = fundingGoalInEthers * 1 ether;deadline = now + durationInMinutes * 1 minutes

Ether and minutes are the keywords reserved by Etay Fang, 1 ether = = 1000 finney, 2 days = = 48 hours. The keywords of date type are seconds,minutes,hours, days,weeks,years, and the keywords reserved in Thai currency units are wei,finney,szabo,ether. 1 finney = 1000 szabo,1 szabo = = 10 ^ 12 wei. Now is also a keyword reserved by Etay Fong to represent the current time.

Next we instantiate a contract:

TokenReward = token (addressOfTokenUsedAsReward); token is defined at the beginning of the code: interface token {function transfer (address receiver, uint amount) {}}

Here we don't implement the token contract, just tell the compiler that our token is a contract, has a transfer () function, and has the contract at the given address.

Next, let's take a look at how the contract receives funds. The relevant code is as follows:

Function () {require (! crowdsaleClosed); uint amount = msg.value; / /...

This function is very special, it has no name, in solidity we call it the Fallback function function, which has no arguments and no return value. If the contract receives ether, the fallback function must be clearly defined, otherwise an exception will be raised and an ether will be returned. The function that receives ether must have the keyword payable, or it will report an error.

The require statement first determines whether the crowdfunding is over. If the crowdfunding is over, the money will be returned to the caller to avoid unnecessary losses.

After the deployment is approved, you can use your test account to transfer money to the contract address, so you can participate in crowdfunding.

After the crowdfunding is successful, if you continue to transfer money to the contract address, the money will be returned to your account.

At this point, I believe you have a deeper understanding of "how to realize the ethernet square crowdfunding intelligent contract". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report