Las billeteras son como su cuenta bancaria, a través de las cuales podemos recibir dinero, enviar dinero y consultar el saldo.
Términos importantes:
- uint- entero sin signo.
- dirección- Es un conjunto único de la string proporcionada para la transacción.
- msg.sender- Dirección del usuario actual.
- msg.value- Costo puesto por el usuario actual.
- público- Visibilidad de la función.
- view: esta palabra clave se usa cuando la función es de solo lectura.
- modificador- modificador es como una función, que se usa en el punto inicial de la función para que pueda verificar la condición requerida y evitar el desperdicio de éter antes de la ejecución de la función.
Depósito de dinero en la cuenta de contrato propiedad del propietario
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]; } }
En el código anterior, el dinero se deposita en la cuenta del contrato, no en la cuenta del propietario. Analicemos un código en el que veremos cómo enviar ether directamente a la cuenta del propietario.
Depositar dinero en la cuenta del Propietario en lugar de la Cuenta del Contrato.
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]; } }