当前位置:网站首页>The first smart contract program faucet sol
The first smart contract program faucet sol
2022-07-19 05:18:00 【qq_ forty-two million forty-two thousand one hundred and fifty-】
On the first code
pragma solidity 0.6.4;
contract Faucet {
// Accept any incoming amount
receive() external payable {
}
// The name of this function is withdraw, It receives a message called withdraw_amount、 The type is unsigned integer (uint) Parameters of
function withdraw(uint withdraw_amount) public {
// Set the upper limit of coin withdrawal , It uses built-in Solidity function require To test a prerequisite ,
// namely withdraw_amount Less than or equal to 100000000000000000 wei
// If you use more withdraw_amount call withdraw function ,
// Here require Function will cause contract execution to stop and fail due to exceptions
require(withdraw_amount <= 100000000000000000);
//msg An object is an input that all contracts can access , It represents the transaction that triggers the execution of this contract .
//sender Attribute is the address of the initiator who initiated the transaction .
//transfer Function is a built-in function , It is used to transfer Ethereum from the contract to the transaction address that invokes the contract
// Send Ethereum to the sending address of the transaction message that triggers the execution of this contract .
//transfer The function only accepts an amount parameter .
// Put the previously defined withdraw_amount The value of this variable is passed as a parameter to withdraw function .
msg.sender.transfer(withdraw_amount);
}
}
compile Faucet contract
We have completed our first contract , Now you need to use Solidity The compiler the Solidity Code to EVM Bytecode , Only in this way can it be EVM Executed .
Solidity The compiler of is an independent executable program , It is usually contained in various programming frameworks , It will also be integrated in some IDE in . For the sake of simplicity , We use Remix This popular IDE.
1、 Open and install MetaMask The plug-in Chrome browser , visit Remix IDE Website of Chinese version http://remix.app.hubwiz.com/
2、 We add a new file by clicking the plus button on the left side of the toolbar , Name the new file Faucet.sol.
3、 Copy code , compiler ; Note that the version of the compiler should be consistent with that declared in the code
4、 Compile successfully , There will be one Faucet Contract name
Deploy Faucet contract
We already have a contract , And compiled it into bytecode . Now we need to put this contract “ register ” To Ethereum blockchain . We still use Ropsten Test the network to test the contract , therefore Ropsten This is the goal that we need to register the contract .
1、 First , Switch to “Run” TAB , stay “ Environmental Science ” Drop down menu , choice “ Injected Web3”. This will establish Remix IDE and MetaMask The connection between wallets , And then through MetaMask Connect to Ropsten Test network . After completing this setting , Will you be in “ Environmental Science ” In the column Ropsten word . Again , Your... Appears in the input box of account selection MetaMask Wallet address
2、Remix IDE Will build a special “creation” transaction , then MetaMask Will ask for your approval . If you are in MetaMask See in the , The transaction created by this contract does not contain ether , It's just 258 byte ( The compiled contract ), This deal is gas need 10 gwei. Click on “SUBMIT” To approve this deal ( Register the contract on the blockchain ).
3、 Please note that ,Faucet The contract now has its own address ,Remix Shown in “Faucet at 0x72e…c7829”( Due to different addresses , These random letters and numbers will also be different ). The small clipboard icon on the right allows you to copy the contract address to the clipboard , We will use this contract address later
Now we have a contract recorded on the blockchain , You can view it through the address . We are https://ropsten.etherscan.io
You can see . Click the icon on the clipboard to copy the contract address , Copy the address of the contract in the search box . You will see the history of the contract
So far, , This contract has only one transaction history : Its own contracts create transactions . As you can see , This contract also doesn't have any ether ( The balance is zero ). This is because we did not include any ether in the transaction that created the contract .
Recharge the contract
We can send some ether to this contract . Your clipboard should still contain the contract address ( without , From Remix Copy it again ). open MetaMask
Like other transfer operations , send out 1 ether To the contract address .
Soon , If you refresh Etherscan Block browser , You will see a new transaction under the contract address , And the account balance is updated to 1 ether 了 .
When you send a transaction to the contract address , If you do not specify which function of the contract to call , It will call the default function . Because we added payable attribute , This function can receive Ethereum and deposit it into the contract balance . The deal triggers the contract at EVM Be executed on , And update the balance . In this way, we have finished recharging !
Withdraw money from the contract
next , Let's try to withdraw ether from the contract . To withdraw money , We need to build a call withdraw Function transaction , And deliver withdraw_amount As a parameter . For the sake of simplicity , have access to Remix Build this deal , And then use MetaMask Send and get our approval .
return Remix Interface , stay Run Contract found on tab . Click to open the deployed contract , Marked as withdraw With field input marks uint256 withdraw_amount.
This is the contract in Remix Interface in . It allows us to build a transaction , Used to call the functions defined in the contract . Let's enter a withdraw_amount value , And then click “withdraw” Button to generate this transaction .
1、 First , Let's see withdraw_amount This parameter . We try to withdraw from the contract 0.1 ETH, This is the maximum amount allowed by the contract . Remember that all currency units in Ethereum should be used wei Let's say ? therefore ,withdraw_amount Parameters must also use wei The format of . there 0.1 ETH, The corresponding is 100000000000000000 wei.
2、 stay Remix Click on the “withdraw” Create a currency withdrawal transaction MetaMask A trading window will pop up requesting your confirmation , Click on “SUBMIT” You can call the contract to withdraw money
3、Etherscan Show that the transaction invokes the contract withdraw function
We can see that this transaction takes the contract as the address , And it doesn't contain any ether . spending 0.1 ETH after , The balance of the contract has changed to 0.9 ETH. But it's strange , We don't see one “ External payment ” Transactions of are displayed in the contract address history .
4、 Where is this external payment transaction ? A new area will appear in the contract address history page , be known as “Internal Txns”. Because of this 0.1 ETH The transfer is initiated by the contract , So this kind of transaction is called internal transaction ( Also known as message ). Click on “Internal Txns” Internal transactions can be seen on the label
summary
We use Solidity I wrote a Faucet contract , And use Remix IDE Compile the contract into EVM Bytecode . We use Remix Build a deal , Then register this contract with Ropsten Testing the blockchain network . After registration , This contract has an Ethereum address , We can send Ethereum to this address . Final , We have built a system for withdraw Function call , And successfully extracted 0.1 ETH. The contract checked our request , Sent back to our calling address using internal transaction 0.1 ETH.
It seems quite simple , What we have just done is to use software to successfully realize the transfer operation of digital currency on a decentralized world computer .
边栏推荐
- 接上期内容:轮播图剩余两种方法
- Excel计算本月剩余天数
- ES6 latest commonly used knowledge dictionary (which can help you solve interview questions, problems in programming, etc.)
- JS native object plus attributes
- 网络命令:网卡信息,netstat,arp
- Wechat applet cloud development and use method-1
- 实习项目1-个性化主页配置
- 【C语言—零基础第十课】数组王国奇遇记
- BUUCTF 杂项——二维码
- 滚动轮加载的两种js方法及模态框拖拽归总
猜你喜欢
Internship project 3- change owner
Email (including attachments, Netease, QQ)
百度地图 实现 热力图
es6新增-运算符的扩展
Internship project 2 - Homepage configuration - my data module
Use echars to realize water drop, ring, segmentation, stacking, organization chart, map outline and other charts
硬核结构体,暴力解读
es6新增-对象部分
【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
Bi design: distributed high concurrency epidemic prevention health management system based on vue+socket+redis
随机推荐
ThreadLocal thread safety example and its principle
【Es6】forEach,for...in ,for...of专栏,让你通过项目案例快速分辨各种for语句的使用方式及区别(完整版)内部有详细注释
【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
Two JS methods of rolling wheel loading and modal box dragging
数据可视化
STL container -- basic operation of map
web3js开发技术
【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
C语言练习题
es6新增-数组部分
Base64 and file conversion
Uniapp uses uview to realize folding panel
LeetCode53. 最大子数组和
Class object automatic injection attribute operation tool
Es6最新常用知识宝典(能够帮助你解决面试题困惑,编写程序中出现的问题等)
学习C语言第二天
PAT乙级1002:写出这个数
BUUCTF 杂项——二维码
es6新增-数组/对象的解构赋值
轮播图节流阀原理及本地存储归总