Solidity

問題:智能合約之間的支付而不減少發送者合約的餘額

  • May 1, 2021

我在 Truffle 工作。我有兩個智能合約AB. 它們的程式碼相同。A應該將一些乙太幣轉移到B並儲存有關此付款的一些資訊。我資助AMETAMASK. 但是,每當我嘗試從Ato進行交易時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{}
}

定義AB在部署和資助A之後METAMASK在此處輸入圖像描述

之後,當我將 ether 轉移到B,with pay()function in 時A,我嘗試使用payView()inAcashView()in查看結果B在此處輸入圖像描述

如您所見,餘額A並沒有減少。該交易記錄在第 68 塊中,但是當我看到該塊時,它說交易發生在我的錢包之間,而A不是在Aand之間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 從不記錄兩個合約之間的互動。

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