什么是钱包智能合约?
钱包就像你的银行账户,通过它我们可以收款、汇款、查询余额。
重要条款:
- uint-无符号整数。
- 地址- 它是为交易提供的一组唯一字符串。
- msg.sender-当前用户的地址。
- msg.value-当前用户投入的成本。
- public-函数的可见性。
- view -当函数是只读的时使用这个关键字。
- 修饰符-修饰符就像一个函数,用于函数的起始点,以便在函数执行之前检查所需的条件并防止浪费以太币。
将钱存入所有者拥有的合约账户
Solidity
// pragma is the directive through
// which we write the smart contract
pragma solidity 0.6.0;
// Creating a contract
contract wallet{
// Specify the owner's address
// which is publicly visible
address payable public Owner;
// mapping is created for mapping
// address=>uint for transaction
mapping(address=>uint) Amount;
// Defining a constructor
// constructor is payable, which means
// it cost ether during the deployment
constructor() public payable{
// msg.sender is the address of the
// person who has currently deployed contract
Owner = msg.sender
// msg.value is the value of the ether we are
// giving during the deploying of contract
Amount[Owner] = msg.value;
}
// modifier(onlyOwner) it will check
// the required condition, here condition
// is the only owner can call this function
modifier onlyOwner(){
// require is used whether the
// owner is the person who has
// deployed the contract or not
require(Owner == msg.sender);
_;
}
// Defining a function which is
// used to send money
function sendMoney(
address payable receiver, uint amount)
public payable onlyOwner {
// receiver account balance should be>0
require( receiver.balance>0);
// amount should not be negative,
// otherwise it will throw error
require(amount>0);
Amount[Owner] -= amount;
Amount[receiver] += amount;
}
// Defining function for Receiving money
// to our smart contract account
// not to an owners account
function ReceiveMoney() public payable{
}
// This function will return the current
// balance of the contract account owned
// by the owner
function CheckBalance_contractAccount()
public view onlyOwner returns(uint){
return address(this).balance;
}
// Defining a function that will return
// the current balance of the owner's account
function CheckBalance()
public view onlyOwner returns(uint){
return Amount[Owner];
}
}
Solidity
// pragma is the directive through which
// we write the smart contract
pragma solidity 0.6.0;
// Creating a contract
contract wallet{
// Specify the owner's address
// which is publicly visible
address payable public Owner;
// Mapping is done from
// address=>uint for transaction
mapping(address => uint) Amount;
// Constructor 'payable' is created,
// which means it cost ether during the
// deployment
constructor() public payable{
// msg.sender is the address of the person
// who has currently deployed contract
Owner = msg.sender
// msg.value is the value of the ether we
// are giving during the deploying of contract
Amount[Owner] = msg.value;
}
// Modifier is created
modifier onlyOwner(){
// require is used whether the owner is the
// person who has deployed the contract or not
require(Owner == msg.sender);
_;
}
// Defining a function to send money,
// we have used modifier(onlyOwner)
// it will check the required condition,
// here condition is the only owner can
// call this function
function sendMoney(
address payable receiver, uint amount)
public payable onlyOwner {
// receiver account balance should be > 0
require( receiver.balance>0);
// amount should not be negative ,
// otherwise it throw error
require(amount >0);
Amount[Owner] -= amount;
Amount[receiver] += amount;
}
// Defining a function to receive money
function ReceiveMoney() public payable{
// Receiving money in owners
// account directly
Amount[Owner] += msg.value;
}
// Defining a function to return
// the balance of the contract
// account owned by the owner
function CheckBalance_contractAccount()
public view onlyOwwner returns(uint){
// return the balance of contract
// account owned by owner
return address(this).balance;
}
// Defining a function to check the
// current balance of the owner account
function CheckBalance()
public view onlyOwner returns(uint){
// return the current balance
// of the owner's account
return Amount[Owner];
}
}
在上面的代码中,钱存入了合约账户,而不是所有者的账户。让我们讨论一个代码,我们将在其中看到如何将以太币直接发送到所有者的帐户中。
将钱存入所有者的帐户而不是合同帐户。
坚固性
// pragma is the directive through which
// we write the smart contract
pragma solidity 0.6.0;
// Creating a contract
contract wallet{
// Specify the owner's address
// which is publicly visible
address payable public Owner;
// Mapping is done from
// address=>uint for transaction
mapping(address => uint) Amount;
// Constructor 'payable' is created,
// which means it cost ether during the
// deployment
constructor() public payable{
// msg.sender is the address of the person
// who has currently deployed contract
Owner = msg.sender
// msg.value is the value of the ether we
// are giving during the deploying of contract
Amount[Owner] = msg.value;
}
// Modifier is created
modifier onlyOwner(){
// require is used whether the owner is the
// person who has deployed the contract or not
require(Owner == msg.sender);
_;
}
// Defining a function to send money,
// we have used modifier(onlyOwner)
// it will check the required condition,
// here condition is the only owner can
// call this function
function sendMoney(
address payable receiver, uint amount)
public payable onlyOwner {
// receiver account balance should be > 0
require( receiver.balance>0);
// amount should not be negative ,
// otherwise it throw error
require(amount >0);
Amount[Owner] -= amount;
Amount[receiver] += amount;
}
// Defining a function to receive money
function ReceiveMoney() public payable{
// Receiving money in owners
// account directly
Amount[Owner] += msg.value;
}
// Defining a function to return
// the balance of the contract
// account owned by the owner
function CheckBalance_contractAccount()
public view onlyOwwner returns(uint){
// return the balance of contract
// account owned by owner
return address(this).balance;
}
// Defining a function to check the
// current balance of the owner account
function CheckBalance()
public view onlyOwner returns(uint){
// return the current balance
// of the owner's account
return Amount[Owner];
}
}