Solidity

沒有足夠的資金用於 testrpc 中的交易範例

  • September 11, 2018

我嘗試在 Solidity 中測試我的第一個智能合約,這是一個簡單的子貨幣範例,如下所示:

pragma solidity ^0.4.0;

contract Coin {

 address public owner;
 mapping (address => uint) balances;



 function Coin() {
   owner = msg.sender;
   balances[tx.origin] = 1000;
 }


 function send(address receiver, uint amount) returns (bool success) {
   if (balances[msg.sender] < amount) {
     return  false;
   }
   balances[msg.sender] -= amount;
   balances[receiver] += amount;
   return true;

 }


 function getBalance (address user) constant returns (uint balance) {
   return balances[user];
 }
}

遷移和編譯後,請在控制台中查看我的 truffle 命令

> var a = web3.eth.accounts[0]
> var b = web3.eth.accounts[1]
> var coin = Coin.deployed()
> coin.then(function(instance) {return instance.getBalance.call(a);})
{ [String: '1000'] s: 1, e: 3, c: [ 1000 ] }
> coin.then(function(instance) {return instance.getBalance.call(b);})
> { [String: '0'] s: 1, e: 0, c: [ 0 ] }

在這一點上我同意這些輸出,但是當我想進行交易時,他們說我沒有足夠的資金……:

> coin.then(function(instance) {return instance.send(b,500);})
Error: Error: sender doesn't have enough funds to send tx. The upfront cost is: 724579920256558730963659928732440307373701337785 and the senders account only has: 99835768400000000000
   at runCall (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/lib/runTx.js:97:10)
   at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:3686:9
   at replenish (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:882:17)
   at iterateeCallback (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:867:17)
   at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:840:16
   at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:3691:13
   at apply (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:21:25)
   at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:56:12
   at Object.async.eachSeries (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/async-eventemitter/node_modules/async/lib/async.js:130:20)
   at VM.AsyncEventEmitter.emit (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/async-eventemitter/lib/AsyncEventEmitter.js:42:9)
   at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/node_modules/truffle-contract/node_modules/web3/lib/web3/errors.js:35:16)
   at /usr/local/lib/node_modules/truffle/node_modules/truffle-contract/node_modules/web3/lib/web3/requestmanager.js:86:36
   at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/httpprovider.js:119:13)
   at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:64:18)
   at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:354:12)
   at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:509:12)
   at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:469:24)
   at emitNone (events.js:91:20)
   at IncomingMessage.emit (events.js:185:7)
   at endReadableNT (_stream_readable.js:974:12)
   at _combinedTickCallback (internal/process/next_tick.js:74:11)
   at process._tickDomainCallback (internal/process/next_tick.js:122:9)

我嘗試使用 Testrpc 中可用的其他帳戶,結果相同……可能是我在命令中遺漏了某些內容或設置了我不知道的內容。但是這個成本似乎很重要,不是嗎?

謝謝你的幫助 !

在您的項目文件夾中,您應該有一個 genesis.json 文件(在其中一個目錄中)。

根據該頁面,您似乎可以為帳戶定義餘額金額:

"accounts": {
   "0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
   "0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
   "0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
   "0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
   "102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" }

這個簡單的測試適用於您的契約,如果您使用以下命令執行它truffle test

contract('Coin', function(accounts) {
 it("works", function() {
   var coin = Coin.deployed();

   var a = accounts[0];
   var b = accounts[1];

   getBalances(coin, a, b, function() {
      coin.send(b,500).then(function() {
       getBalances(coin, a, b);
      });
   })
 });
});

function getBalances(coin, a, b, cb) {
 coin.getBalance.call(a).then(function(balanceA) {
   console.log("balanceA: " + balanceA);

   coin.getBalance.call(b).then(function(balanceB) {
     console.log("balanceB: " + balanceB);
     if (cb)
       cb();
   });
 });
}

輸出:

balanceA: 1000
balanceB: 0
balanceA: 500
balanceB: 500

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