Solidity

sendTransaction BigNumber 錯誤:new BigNumber() 不是數字:objectObject這bj和C噸這bj和C噸object Object

  • December 31, 2018

我是 Solidity 程式和為其中一個應用程序編寫智能合約的新手。這是我的兩份契約。我正在使用 truffle 和 testrpc 作為工具。我不斷收到錯誤。需要指導。我已經看到了具有類似主題行的早期執行緒,但它們沒有幫助。我也已經嘗試過更改函式的名稱。

//Certificate.sol

contract Certificate {
   address private addressSelf;
   bytes32 private certName;

   function Certificate(bytes32 _certName) {
       addressSelf = msg.sender;  
       certName = _certName;
   }  
}

//Participant.sol

contract Participant {
   address private addressSelf;
   bytes32 private participantName;

   struct CertStruct {
       Certificate certificateData;
       bool isCertificate;
   }

   mapping (address => CertStruct) public certStructs;
   mapping (uint => address) public certIndex;
   uint public noOfCerts;

   function Participant(bytes32 _partName) {
       addressSelf = msg.sender;  // just set the self
       participantName = _partName;
       noOfCerts = 0;
   }

   function isCertGranted(address certAddress) public constant returns(bool) {
       return certStructs[certAddress].isCertificate;
   }

   function grantCertToParticipant(address certAddress, Certificate certificate) payable public returns (bool)
   {
       if(!isCertGranted(certAddress)) return false;

       CertStruct memory newCertStruct;
       newCertStruct.certificateData = certificate;
       newCertStruct.isCertificate = true;
       certStructs[certAddress] = newCertStruct;
       certIndex[noOfCerts] = certAddress;
       noOfCerts++;

       return true;
   }
}

//participant.js

var CertificateSol = artifacts.require("./Certificate.sol");

var ParticipantSol = artifacts.require("./Participant.sol");

contract('Participant', function() {

   it("should put certificate in the one participant", function() {

       var newCert;
       var newPart;

       return CertificateSol.new('CertName').then(function(newCertInstance) {
           newCert = newCertInstance;
       }

       return ParticipantSol.new('Name of Participant').then(function(newPartInstance) {
           newPart = newPartInstance;

       newPart.grantCertToParticipant.sendTransaction(newCert.address, newCert, { from : web3.eth.accounts[0], gas: 3000000 }).then(function(returnFlag){

           newPart.noOfCerts.call().then(function(certNumber){
             console.log('certNumber is ' + certNumber.toString(10));
         });
       }).catch(function(err){
         console.log('I am at grantCertToParticipant err ' + err);
       });

   });    
}

首先,您不需要顯式發送事務,呼叫一個方法就足夠了:

newPart.grantCertToParticipant(newCert.address, newCert, { from : web3.eth.accounts[0], gas: 3000000 })

其次,newCert它是一個合約代理對象,所以不要將它傳遞給 web3 呼叫。只需發送 newCert.address 就足夠了。我將只在方法簽名中留下地址:

function grantCertToParticipant(address certAddress) payable public returns (bool)

不幸的是,這條線沒有多大意義:

newCertStruct.certificateData = certificate; 

因為您不能儲存the whole契約,而只能以地址的形式引用,所以它是多餘的。

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