Web3js

無法從一個契約轉移到另一個契約

  • January 2, 2019

我試圖通過實現銀行合約和客戶合約來了解智能合約如何互動。最終使用者創建Client契約。然後,他使用Client契約從契約中存款或取款Bank

發生錯誤

但是,我無法將錢從Client合約轉移到Bank合約,並且當使用者呼叫addDeposit()方法 in時提示拋出錯誤Client。提示說Error: Returned error: VM Exception while processing transaction: revert

下面是TestContract.js

const Bank = artifacts.require("Bank");
var Client = artifacts.require("Client");
// ether = 10**18;

contract("TestContracts", function(accounts) {
   const alice = accounts[1];
   it("add deposit using client contract", async () => {
       const bank = await Bank.deployed();
       const client = await Client.deployed(bank.address, alice); //the owner of the contract is alice

       client.addFund({ from: alice, value: web3.utils.toWei("30","ether") });

       balance = web3.utils.fromWei(await client.checkBalance());
       assert(balance, 30, "balance of the contract wrong");

       await client.addDeposit(5);
       let deposit = web3.utils.fromWei(await client.checkDeposit()); //ERROR HERE!!!!
       assert(deposit, 5, "balance of the contract wrong");
   })

這是Client契約

contract Client {
   address owner; // the client contract connect with the account who creates it
   Bank bank; // the bank that this client contract connected with

   constructor (address _referBank, address _owner) public payable {
       owner = _owner;
       bank = Bank(_referBank);
       bank.enroll(address(this));
   }

   function isClientActive() public view returns(bool) {
       return bank.isClientActive(address(this));
   }

   function addFund() public payable {
       require(msg.sender == owner, "only owner are allow to send money to client contract");
   }

   function addDeposit(uint amount) public {
       bank.transfer(amount);
   }

   function withdraw(uint amount) public payable {
       bank.withdraw(amount);
   }

   function checkDeposit() public view returns(uint) {
       return bank.checkDeposit(address(this));
   }

   function checkBalance() public view returns(uint) {
       return address(this).balance;
   }

   // transfer the balance from the client's contract to the owner account
   function () public payable {
       require(msg.sender == owner, "only owner are allow to send money to client contract");
   }
}

下面是Bank契約

contract Bank {
   struct Client {
       uint deposit;
       bool active;
   }

   address owner;
   mapping(address => Client) public clientList;
   uint clientCounter;

   constructor() public payable {
       require(msg.value == 30 ether, "Initial funding of 30 ether required for rewards");
       /* Set the owner to the creator of this contract */
       owner = msg.sender;
       clientCounter = 0;
   }

   function enroll(address _addr) public {
       clientList[_addr].deposit = 0;
       clientList[_addr].active = true;
       clientCounter++;
   }

   function isClientActive(address _addr) public view returns(bool) {
       return clientList[_addr].active;
   }

   function getClientCounter() public view returns(uint) {
       return clientCounter;
   }

   // add the deposit to the sender account
   function addDeposit() public payable {
       if (clientList[msg.sender].active != true) {
           revert("the client's address does not exist");
       } else {
           clientList[msg.sender].deposit += msg.value;
       }
   }

   // transfer the amount of ether to the provided address
   function withdraw(uint amount) public payable {
       if (clientList[msg.sender].deposit < amount) {
           revert("not enough deposit to make the withdraw");
       } else {
           clientList[msg.sender].deposit -= amount;
           msg.sender.transfer(amount);
       }
   }

   // return the deposit of the provide address
   function checkDeposit(address _addr) public view returns (uint) {
       return clientList[_addr].deposit;
   }

   // received money from the client contract
   function () public payable {
       if (!isClientActive(msg.sender)) {
           revert("client does not exist");
       } else {
           clientList[msg.sender].deposit += msg.value;
       }
   }
}

問題出在函式中(在客戶端契約中):

function addDeposit(uint amount) public {
   bank.transfer(amount);
 }

因為bank不是地址。您可以將銀行地址保存在 ocnstructor 中並使用它進行轉賬或更好,使用客戶端中的addDeposit功能bank,如下所示:

function addDeposit(uint amount) public {
   bank.addDeposit.value(amount)();
 }

我在混音中對其進行了測試,並且效果很好。

希望這對您有所幫助。

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