benediamond / anonymous-zether

A private payment system for Ethereum-based blockchains, with no trusted setup.
Other
16 stars 9 forks source link

TransferOccured is not a function #4

Closed JohnSt99 closed 3 years ago

JohnSt99 commented 3 years ago

Issue on windows 10.

How to reproduce:

in node console on the packages/protocol folder run the commands:

__dirname = '[my actual path to]/anonymous-zether/packages/protocol';
const Client = require(path.join(__dirname, '../anonymous.js/src/client.js'));
Web3 = require('web3');
web3 = new Web3('http://localhost:9545');
contract = require("@truffle/contract");
path = require('path');
deployedJSON = require(path.join(__dirname, 'build/contracts/ZSC.json'));
var provider = new Web3.providers.WebsocketProvider("ws://localhost:9545");
var deployed = contract(deployedJSON);
deployed.setProvider(provider);
deployed.deployed();
web3.eth.getAccounts().then(function(result) { accounts = result;});
var home;
web3.eth.getAccounts().then((accounts) => { home = accounts[accounts.length - 1];});
var alice = new Client(web3,deployed,home)

Output error on last command:

Thrown:
TypeError: zsc.events.TransferOccurred is not a function
    at new Client (C:\Users\jsm.DESKTOP-7VJ0LVK\Desktop\anonymous-zether\packages\anonymous.js\src\client.js:45:20)

zsc.events contains:

> deployed.events
{
  '0x306c363b04ebfc48d0bc8a89a5fb2e4230c37937d0530d50b8334d50eec63ef0': {
    anonymous: false,
    inputs: [ [Object], [Object] ],
    name: 'TransferOccurred',
    type: 'event',
    constant: undefined,
    payable: undefined,
    signature: '0x306c363b04ebfc48d0bc8a89a5fb2e4230c37937d0530d50b8334d50eec63ef0'
  }
}

Removing the parentheses and brackets from zsc.events.TransferOccurred({}) in packages/anonymous.js/src/client.js the error becomes:

Thrown:
TypeError: Cannot read property 'on' of undefined
    at new Client (C:\Users\jsm.DESKTOP-7VJ0LVK\Desktop\anonymous-zether\packages\anonymous.js\src\client.js:47:14)

as expected since there is no attribute TransferOccured in the event

JohnSt99 commented 3 years ago

running the following commands returns the object that might have been the one client requires instead:

> deployed.at(deployed.address).then(function(result) {dbg = result});
> dbg.contract.events 
{
  TransferOccurred: [Function: bound ],
  '0x306c363b04ebfc48d0bc8a89a5fb2e4230c37937d0530d50b8334d50eec63ef0': [Function: bound ],
  'TransferOccurred((bytes32,bytes32)[],(bytes32,bytes32))': [Function: bound ],
  allEvents: [Function: bound ]
}
benediamond commented 3 years ago

interesting. does this mean you were able to fix the error? so it's enough to initialize your client with dgb instead of deployed?

JohnSt99 commented 3 years ago

Yeah, an additition of the following lines in the readme file would be very helfpul for future people interested in the project:

Contracts must be imported in node using the contract.at command where the contract is a contract object (from @truffle/contract) using the compiled contract.json files

An example is shown below:

contract = require("@truffle/contract");
path = require('path');
deployedJSON  = require(path.join(__dirname, 'build/contracts/ZSC.json'));
var provider = new Web3.providers.WebsocketProvider("ws://localhost:9545");
var deployed = contract(deployedJSON);
deployed.setProvider(provider);
deployed.deployed();
deployed.at(deployed.address).then(function(result) {dbg = result});
var alice = new Client(web3,dbg.contract,home);

Before running the commands alice.deposit() / alice.withdraw() it is necessary to mint funds, otherwise it will trigger an ERC20 revert error (insufficient funds). An example is shown below:

cash.mint(home, 150, {from: home}).then(console.log)
cash.approve(dbg.address, 150, {from: home}).then(console.log)
cash.balanceOf.call(home).then(function(result) {balance = result});
assert.equal(balance, 150, "Minting failed");