http://www.7klian.com

详解如安在以太坊智能合约中集成可证明的Oracle-part3

  7    using SafeMath for uint;
198        uint amountSentUsd = tenantPayment.mul(ETHUSD).div(1e18);
139
243        landlordAddress.transfer(leaseDeposit.div(ETHUSD).mul(1e18));
270            leaseDeposit
22}
 14    uint leaseBalanceWei;
 55
199
 29        uint16 monthlyAmountUsd;
186        require(!lease.leaseFullyPaid, “Lease has already been fully paid”);
15    uint8,
158        tenantPayment = msg.value;
 6
245        emit leaseDepositCollected(
140        emit leaseCreated(
227    function collectLeaseDeposit(address payable tenantAddr) public onlyLandlord {
 62        address tenantAddress,
最后,第45行是回退函数。假如智能合约随机吸收(也许是从房东哪里吸收的以太坊)以增补可证明的查询,它会优雅地收到以太坊,并将其添加到余额中。
268        emit leaseDepositReclaimed(
13    Lease storage lease = tenantLease[tenantAddress];
20        leaseDeposit
 8        transferAmount,
45function() external payable {}
 46    event leaseCreated(
第41–43行声明白另一个实用措施成果,以显示智能合约的当前余额。为了得到余额,我们必需将this(智能合约实例)转换为address范例,然后从中读取balance。
43}
254        require(lease.leaseDepositUsd > 0, “Lease deposit has already been removed”);
206        leaseBalanceWei = leaseBalanceWei.add(tenantPayment);
 94        require(provable_getPrice(“URL”) < address(this).balance, “Not enough Ether in contract, please add more”);
142            0,
28        lease.leaseDepositUsd,
145            leasePaymentWindowSeconds,
 75
202            “Lease payment must be greater than or equal to the monthly amount with a maximum offset of $5”);
106            _payLeaseDeposit();
 30        uint16 leaseDepositUsd;
157        tenantAddress = msg.sender;
第37–39行是一个实用函数,用于检索当前的ETHUSD汇率。
162
19    uint64,
29        lease.leasePaymentWindowSeconds,
 7    workingState = State.reclaimingLeaseDeposit;
159        workingState = State.payingLeaseDeposit;
 51        uint32 leasePaymentWindowSeconds,
15    lease.leaseDepositUsd = 0;
152    function payLeaseDeposit() public payable {
174        lease.depositPaymentWindowEnd = 0;
101        require(msg.sender == provable_cbAddress(), “Calling address does match usingProvable contract address “);
123        ) public onlyLandlord {
第2行确保余额大于0。
130            monthlyAmountUsd,
 36    }
 57        address tenantAddress,
 39    mapping (address => Lease) tenantLease;
135            false,
32        lease.leaseDepositPaid,
 2    require(leaseBalanceWei > 0, “Lease balance must be greater than 0”);
 28        uint8 monthsPaid;
182
 22    }
 68        uint amountCollected
175        lease.leasePaymentWindowEnd = uint64(now + lease.leasePaymentWindowSeconds);
第2-4行是反复逻辑。
216            );
 82    event fundsWithdrawn(
 34        bool leaseDepositPaid;
 1function withdrawFunds() public onlyLandlord {
 76    event leaseFullyPaid(
102        validIds[myId] = false;
283        );
215                monthsPaid
  1pragma solidity ^0.5.17;
 95        bytes32 queryId = provable_query(“URL”, “json(https://api.pro.coinbase.com/products/ETH-USD/ticker).price”);
42    return uint(address(this).balance);
 53        bool leaseFullyPaid
 60
 10    address payable tenantAddress;
16    uint16,
143            monthlyAmountUsd,
37function getRate() public view returns (uint) {
 43        _;
 61    event leasePaymentPaid(
275        require(leaseBalanceWei > 0, “Lease balance must be greater than 0”);
 56    event leaseDepositPaid(
278        landlordAddress.transfer(transferAmount);
10    );
112            _reclaimLeaseDeposit();
197        Lease storage lease = tenantLease[tenantAddress];
153        Lease storage lease = tenantLease[msg.sender];
以下是完整的智能合约代码:
260
128            numberOfMonths,
 70
 67        address tenantAddress,
183    function payLease() public payable {
113        }
 23
 88            landlordAddress = msg.sender;
277        leaseBalanceWei = 0;
19        tenantAddress,
 32        uint64 leasePaymentWindowEnd;
11}
244
 35        bool leaseFullyPaid;
 84        uint leaseBalanceWei
23    Lease memory lease = tenantLease[tenant];
第3-5行显示了与_collectLeaseDeposit和_reclaimLeaseDeposit沟通的提款方法。智能合约将余额转移给房东。
 81
190        tenantPayment = msg.value;
249    }
 11
276        uint transferAmount = leaseBalanceWei;
170            amountSentUsd <= lease.leaseDepositUsd + 5,
34    );
154        require(!lease.leaseDepositPaid, “Lease deposit is already paid.”);
192        fetchUsdRate();
230        require(lease.leasePaymentWindowEnd <= now, “Lease payment must be overdue past payment window to collect lease deposit”);
118            uint16 monthlyAmountUsd,
200        require(
205        lease.monthsPaid = uint8(monthsPaid);
163    function _payLeaseDeposit() internal {
272    }
126
114    }
111        } else if (workingState == State.reclaimingLeaseDeposit) {
31        lease.depositPaymentWindowEnd,
238    function _collectLeaseDeposit() internal {
189        tenantAddress = msg.sender;
194
第24–34行汇报函数返回Lease工具上的所有属性。
236    }
136            false,
18    emit leaseDepositReclaimed(
第7-10行发出了fundsWithdrawn事件。
 12    uint ETHUSD;
 87    constructor () public payable {
 42        require(msg.sender == landlordAddress, “Must be the landlord to create a lease”);
234        workingState = State.collectingLeaseDeposit;
218            lease.leasePaymentWindowEnd = lease.leasePaymentWindowEnd + lease.leasePaymentWindowSeconds;
115
137            false
 83        uint transferAmount,
 37
20    uint64,
41function getContractBalance() public view returns (uint) {
184        Lease storage lease = tenantLease[msg.sender];
38    return ETHUSD;
16    tenantAddress.transfer(leaseDeposit.div(ETHUSD).mul(1e18));
107        } else if (workingState == State.payingLease) {
286    function getLease(address tenant) public view returns (
226
264        uint leaseDeposit = lease.leaseDepositUsd;
 9        leaseBalanceWei
13function getLease(address tenant) public view returns (
221                tenantAddress,
214                lease.numberOfMonths,
25        lease.numberOfMonths,
151
237
 4    leaseBalanceWei = 0;
第13–35行暗示一个实用函数,用于在给定租户地点的环境下检索激活Lease上的所有属性。
187        require(lease.leasePaymentWindowEnd >= now, “Lease payment must fit into payment window”);
220            emit leasePaymentPaid(
267
104
100        require(validIds[myId], “Provable query IDs do not match, no valid call was made to provable_query()”);
 21        idle
 9}
 15
172
134            depositPaymentWindowEnd,

 1function reclaimLeaseDeposit() public {
213                tenantAddress,
 49        uint16 monthlyAmountUsd,
 74    );
36
247            leaseDeposit
168        require(
  5contract LeaseGenerator is usingProvable {
 3    require(lease.leaseFullyPaid, “Lease must be fully paid to take back lease deposit”);
 52        bool leaseDepositPaid,
44
 47        uint8 numberOfMonths,
191        workingState = State.payingLease;
124
285
204        uint monthsPaid = uint256(lease.monthsPaid).add(amountSentUsd.add(10).div(uint256(lease.monthlyAmountUsd)));
188
14    uint8,
177        emit leaseDepositPaid(
288        uint8,

261    function _reclaimLeaseDeposit() internal {
 3    uint transferAmount = leaseBalanceWei;
287        uint8,
  8
 38    mapping (bytes32 => bool) validIds;
229        require(!lease.leaseFullyPaid, “Cannot collect lease deposit if lease is already paid”);
22    bool) {
 6    tenantAddress = msg.sender;
148            false
 69    );
30        lease.leasePaymentWindowEnd,
219
33        lease.leaseFullyPaid
149        );
  3import “./provableAPI.sol”;
185        require(lease.leaseDepositPaid, “Lease deposit must be paid before making lease payments”);
105        if (workingState == State.payingLeaseDeposit) {
 73        uint amountReclaimed
第1行例示了我们的public函数,用于收回租赁押金。租户已全额付清租金,这函数由租户挪用。
40
 71    event leaseDepositReclaimed(
231        require(lease.leaseDepositUsd > 0, “Lease deposit has already been removed”);
116    function createNewLease(
169            amountSentUsd >= lease.leaseDepositUsd – 5 &&
 91    }
 44    }
203
21    bool,
193    }
21    );
 24    State workingState;
127        tenantLease[tenantAddr] = Lease(
 40
256        tenantAddress = msg.sender;
258        fetchUsdRate();
262        workingState = State.idle;
 72        address tenantAddress,
 31        uint32 leasePaymentWindowSeconds;
 92
 20        reclaimingLeaseDeposit,
 25
 19        collectingLeaseDeposit,
 85    );
 18        payingLease,
144            leaseDepositUsd,
160        fetchUsdRate();
 99    function __callback(bytes32 myId, string memory result) public {
171            “Deposit payment must equal to the deposit amount with a maximum offset of $5”);
 5
195    function _payLease() internal {
 13    uint tenantPayment;
129            0,
 77        address tenantAddress,
240        Lease storage lease = tenantLease[tenantAddress];
150    }
209            lease.leaseFullyPaid = true;
109        } else if (workingState == State.collectingLeaseDeposit) {
125        uint64 depositPaymentWindowEnd = uint64(now.add(depositPaymentWindowSeconds));
103        ETHUSD = parseInt(result);
251    function reclaimLeaseDeposit() public {
252        Lease storage lease = tenantLease[msg.sender];
225    }
271        );
164        workingState = State.idle;
281            transferAmount,
108            _payLease();
133            0,
35}
 8    fetchUsdRate();
235        fetchUsdRate();
266        tenantAddress.transfer(leaseDeposit.div(ETHUSD).mul(1e18));
10
 45
11function _reclaimLeaseDeposit() internal {
 97    }
 17        payingLeaseDeposit,
 93    function fetchUsdRate() internal {
第11行开始internal函数,以继承收回租赁押金的进程。此函数的其余部门是collectLeaseDeposit的反复逻辑。
217        } else {
284    }
 4    require(lease.leaseDepositUsd > 0, “Lease deposit has already been removed”);
14    uint leaseDeposit = lease.leaseDepositUsd;
173        lease.leaseDepositPaid = true;
17
 78        uint numberOfmonths,
 5    landlordAddress.transfer(transferAmount);
156
166        uint amountSentUsd = tenantPayment.mul(ETHUSD).div(1e18);
第23行提出了有问题的Lease并将其分派给memory。不必预先为它筹备storage,只需要读取它即可。
 54    );
24    return (
 86
120            uint32 leasePaymentWindowSeconds,
196        workingState = State.idle;
12
  9    address payable landlordAddress;
259    }
 65
 64    );
280        emit fundsWithdrawn(
165        Lease storage lease = tenantLease[tenantAddress];
121            uint32 depositPaymentWindowSeconds,
 27        uint8 numberOfMonths;
269            tenantAddress,
228        Lease storage lease = tenantLease[tenantAddr];
 89            provable_setCustomGasPrice(100000000000);
27        lease.monthlyAmountUsd,
 66    event leaseDepositCollected(
第6-8行是反复逻辑。
17    uint16,
第1-11行是房东挪用的用于提取租赁付款的函数。
208        if (monthsPaid == lease.numberOfMonths) {
233        tenantAddress = tenantAddr;
257        workingState = State.reclaimingLeaseDeposit;
232
132            leasePaymentWindowSeconds,
176
 33        uint64 depositPaymentWindowEnd;
138        );
239        workingState = State.idle;
161    }
39}
274    function withdrawFunds() public onlyLandlord {
 41    modifier onlyLandlord() {
  2import “./SafeMath.sol”;
265        lease.leaseDepositUsd = 0;
179            amountSentUsd
147            false,
131            leaseDepositUsd,
180        );
119            uint16 leaseDepositUsd,
155        require(lease.depositPaymentWindowEnd >= now, “Lease deposit payment must fit into payment window”);
 16    enum State {
178            tenantAddress,
224        }
212            emit leaseFullyPaid(
 80    );
12    workingState = State.idle;
  4
18    uint32,
141            numberOfMonths,
 58        uint amountSentUsd
167
181    }
248        );
273
253        require(lease.leaseFullyPaid, “Lease must be fully paid to take back lease deposit”);
26        lease.monthsPaid,
122            address payable tenantAddr
 90            OAR = OracleAddrResolverI(0xB7D2d92e74447535088A32AD65d459E97f692222);
第13-22行汇报函数它必需返回哪些范例。
279
242        lease.leaseDepositUsd = 0;
207
117            uint8 numberOfMonths,
110            _collectLeaseDeposit();
255
 2    Lease storage lease = tenantLease[msg.sender];
 7    emit fundsWithdrawn(
246            tenantAddress,
 98
211
201            amountSentUsd >= lease.monthlyAmountUsd – 5,
 63        uint amountSentUsd
 26    struct Lease {
282            leaseBalanceWei
146            false,
 48        uint8 monthsPaid,
 50        uint16 leaseDepositUsd,
222                amountSentUsd
 59    );
241        uint leaseDeposit = lease.leaseDepositUsd;
 79        uint monthsPaid
250
210            lease.leasePaymentWindowEnd = 0;
223            );
  6
 96        validIds[queryId] = true;
263        Lease storage lease = tenantLease[tenantAddress];

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

相关文章阅读