問題:智能合約之間的支付而不減少發送者合約的餘額
我在 Truffle 工作。我有兩個智能合約
A
和B
. 它們的程式碼相同。A
應該將一些乙太幣轉移到B
並儲存有關此付款的一些資訊。我資助A
了METAMASK
. 但是,每當我嘗試從A
to進行交易時B
,
ax1.pay.sendTransaction('address of B',{value:'1000000000000000000', gasPrice:200})
在 Truffle 控制台中,存在兩個問題: 1- 餘額
B
增加但餘額A
不減少。2- 此交易不記錄在賬簿中。A:
pragma solidity ^0.5.12; import "./B.sol"; contract A{ event Payment(address recipient, uint value); event Receive(address sender, uint value); address sender; address payable receiver; uint sentValue; uint receiptValue; uint gasPrice; function pay(address payable recipient) external payable{\\Transfers some ether to B recipient.transfer(msg.value); B(recipient).cash(msg.value, tx.gasprice); receiver=recipient; sentValue=msg.value; gasPrice=tx.gasprice; emit Payment(recipient, msg.value); } function payView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last transfer. return (receiver, uint2str(sentValue), uint2str(gasPrice), uint2str(address(this).balance)); } function cash(uint cashValue, uint GasPrice) external payable{\\Records some data about the last received transaction sender=msg.sender; receiptValue=cashValue; gasPrice=GasPrice; emit Receive(msg.sender, cashValue); } function cashView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last received transaction return (sender, uint2str(receiptValue), uint2str(gasPrice), uint2str(address(this).balance)); } function uint2str(uint _i) internal pure returns (string memory ) { if (_i == 0) { return "0"; } uint j = _i; uint len; while (j != 0) { len++; j /= 10; } bytes memory bstr = new bytes(len); uint k = len - 1; while (_i != 0) { bstr[k--] = byte(uint8(48 + _i % 10)); _i /= 10; } return string(bstr); } function() external payable{} }
乙:
pragma solidity ^0.5.12; import "./A.sol"; contract B{ event Payment(address recipient, uint value); event Receive(address sender, uint value); address sender; address payable receiver; uint sentValue; uint receiptValue; uint gasPrice; function pay(address payable recipient) external payable{\\Transfers some ether to A recipient.transfer(msg.value); A(recipient).cash(msg.value, tx.gasprice); receiver=recipient; sentValue=msg.value; gasPrice=tx.gasprice; emit Payment(recipient, msg.value); } function payView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last transfer. return (receiver, uint2str(sentValue), uint2str(gasPrice), uint2str(address(this).balance)); } function cash(uint cashValue, uint GasPrice) external payable{\\Records some data about the last received transaction sender=msg.sender; receiptValue=cashValue; gasPrice=GasPrice; emit Receive(msg.sender, cashValue); } function cashView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last received transaction return (sender, uint2str(receiptValue), uint2str(gasPrice), uint2str(address(this).balance)); } function uint2str(uint _i) internal pure returns (string memory ) { if (_i == 0) { return "0"; } uint j = _i; uint len; while (j != 0) { len++; j /= 10; } bytes memory bstr = new bytes(len); uint k = len - 1; while (_i != 0) { bstr[k--] = byte(uint8(48 + _i % 10)); _i /= 10; } return string(bstr); } function() external payable{} }
之後,當我將 ether 轉移到
B
,withpay()
function in 時A
,我嘗試使用payView()
inA
和cashView()
in查看結果B
:如您所見,餘額
A
並沒有減少。該交易記錄在第 68 塊中,但是當我看到該塊時,它說交易發生在我的錢包之間,而A
不是在A
and之間B
:這兩件事的原因是什麼?我怎樣才能看到分類帳中的交易以及減少的餘額。
問題 1:為了
B
從 的餘額中發送一些乙太幣A
,使用ax1.pay.sendTransaction('address of B',{value:'1000000000000000000', gasPrice:200}
不合適。每當我們使用{from:'...', value:'...' , gasPrice:..., gas:...}
in.sendTransaction(...)
時,實際上,我們是在告訴“從對應的帳戶餘額中A
減少多少,然後將其添加到”。如果未確定,將考慮在 Ganache。但是,如果我們想將餘額減少到餘額中並添加到餘額中,則應在 Truffle 控制台中修改發送交易的功能和方法。為此,我們添加另一個參數並將其設置為的參數:value:'...'``from:'...'``B``from:'...'``accounts[0]``A``value:'...'``B``pay()``A``pay()``.transfer()
pragma solidity ^0.5.12; import "./B.sol"; contract A . . . function pay(address payable recipient, uint val) external payable{ recipient.transfer(val); A(recipient).cash(val, tx.gasprice); receiver=recipient; sentValue=val; gasPrice=tx.gasprice; emit Payment(recipient, val); } . . . }
在 Truffle 控制台中:
ax1.pay.sendTransaction('address of B', '100000000000000',{gasPrice:200})
現在,可以看到 的餘額
A
將減少與 確定的一樣多val
。但是,必須注意,這種方式val
不能超過999999999999999 wei
問題2:
在 Truffle 中,只有錢包賬戶和合約賬戶之間的互動記錄在賬本中。
我希望這個答案能有所幫助。
當您以如下形式使用命令時:
ax1.pay.sendTransaction(<address>,{from:<address>, value:<value>, gas:<...>, gasPrice:<...>})
您應該在欄位中定義發件人的地址
from:
,否則,accounts[0]
在 Ganache 帳戶將被視為發件人,其餘額將減少。現在,如果您的發件人是合約,則您不能通過此表單使用此命令。因為,欄位中嵌入的地址from:
必須是 Ganache 的,而合約賬戶不是。根據您的目的,我建議將發件人的價值.pay(...)
作為參數嵌入契約的功能中。此外,Truffle 從不記錄兩個合約之間的互動。