Solidity

如何向應付函式添加更多邏輯?

  • November 21, 2018

如何在付款功能中添加買方(在我的情況下是一家公司)在執行付款時添加一些有關其自身的詳細資訊(例如註冊號和公司名稱)的可能性?

以下是我需要修改以添加上述程式碼:

contract KYCPurchase {
 uint public price = 2 ether;
 address[] public buyer;

 function buy() public payable {
   require(msg.value >= price);

   // Keep a list of buyers who have transferred enough ether
   buyer.push(msg.sender);

 }

謝謝!

重要的是要注意應付函式不必具有零參數

您可以像這樣簡單地重寫您的契約:

pragma solidity ^0.4.24;

contract KYCPurchase {
 uint public price = 2 ether;

 struct Company {
     string registeredNumber;
     string name;
 }
 mapping (address => Company) companies;

 function buy(string registeredNumber, string name) public payable {
   require(msg.value >= price);

   // Keep a list of buyers who have transferred enough ether
   companies[msg.sender] = Company(registeredNumber, name);
 }
}

我製作了registeredNumbera 字元串,因為它更具包容性 - 在某些司法管轄區,它們也可以包含字母 - 但可以隨意將其切換為 anuint256或任何你需要的

引用自:https://ethereum.stackexchange.com/questions/62814