6166511419
5 months ago  Karma: 5
how to fix contract.events.NewValueSet(...) is not a function in tutorial

well, I followed this post: https://loomx.io/developers/docs/en/web3js-loom-provider-truffle.html,
and in the end, add a js file to run to check how to interact with web3 and LoopProvider. Here is my code


const Web3 = require('web3');

// import Web3 from 'web3'

const {
  NonceTxMiddleware, SignedTxMiddleware, Client, ClientEvent,
  Contract, Address, LocalAddress, CryptoUtils, LoomProvider
} = require('loom-js');


const privateKey = CryptoUtils.generatePrivateKey()
const publicKey = CryptoUtils.publicKeyFromPrivateKey(privateKey)

// Create the client
const client = new Client(
  'default',
  'ws://127.0.0.1:46657/websocket',
  'ws://127.0.0.1:9999/queryws',
)

// The address for the caller of the function
const fromAddr = LocalAddress.fromPublicKey(publicKey).toString()

// Instantiate web3 client using LoomProvider
const web3 = new Web3(new LoomProvider(client, privateKey))

const ABI = [{"anonymous":false,"inputs":[{"indexed":false,"name":"_value","type":"uint256"}],"name":"NewValueSet","type":"event"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]

const contractAddress = '0x1a31b9b9d281d49001fe7f3f638000a739afc9c3';

// Instantiate the contract and let it ready to be used
const contract = new web3.eth.Contract(ABI, contractAddress, {fromAddr});

var a = contract.events.NewValueSet({});
console.log(a);
a.watch((err, event) => {
  if (err) {
    return console.error(err)
  }

  console.log('New value set', event.returnValues._value)
});


(async function () {
    // Set the value 47
    const tx = await contract.methods.set(47).send()

    // Get the value 47
    const value = await contract.methods.get().call()
})();


en
#web3
#javascript
6166511419
5 months ago  Karma: 5
how to fix contract.events.NewValueSet(...) is not a function in tutorial

well, I followed this post: https://loomx.io/developers/docs/en/web3js-loom-provider-truffle.html,
and in the end, add a js file to run to check how to interact with web3 and LoopProvider. Here is my code


const Web3 = require('web3');

// import Web3 from 'web3'

const {
  NonceTxMiddleware, SignedTxMiddleware, Client, ClientEvent,
  Contract, Address, LocalAddress, CryptoUtils, LoomProvider
} = require('loom-js');


const privateKey = CryptoUtils.generatePrivateKey()
const publicKey = CryptoUtils.publicKeyFromPrivateKey(privateKey)

// Create the client
const client = new Client(
  'default',
  'ws://127.0.0.1:46657/websocket',
  'ws://127.0.0.1:9999/queryws',
)

// The address for the caller of the function
const fromAddr = LocalAddress.fromPublicKey(publicKey).toString()

// Instantiate web3 client using LoomProvider
const web3 = new Web3(new LoomProvider(client, privateKey))

const ABI = [{"anonymous":false,"inputs":[{"indexed":false,"name":"_value","type":"uint256"}],"name":"NewValueSet","type":"event"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]

const contractAddress = '0x1a31b9b9d281d49001fe7f3f638000a739afc9c3';

// Instantiate the contract and let it ready to be used
const contract = new web3.eth.Contract(ABI, contractAddress, {fromAddr});

var a = contract.events.NewValueSet({});
console.log(a);
a.watch((err, event) => {
  if (err) {
    return console.error(err)
  }

  console.log('New value set', event.returnValues._value)
});


(async function () {
    // Set the value 47
    const tx = await contract.methods.set(47).send()

    // Get the value 47
    const value = await contract.methods.get().call()
})();


en
#web3
#javascript

2 ANSWERS
luke2
5 months ago Karma: 1864

Didn't run your code yet, but your ABI looks correct so here's what you can try:

  1. Restart (sometimes its something simple)

  2. Double check your web3 version, if you were using an older version of web3, .events won't work so you'll need to upgrade web3 or use older syntax:

var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at("0x1234...ab67" /* address */);

var event = clientReceipt.Deposit();

// watch for changes
event.watch(function(error, result){
    // result will contain various information
    // including the arguments given to the `Deposit`
    // call.
    if (!error)
        console.log(result);
});

// Or pass a callback to start watching immediately
var event = clientReceipt.Deposit(function(error, result) {
    if (!error)
        console.log(result);
});

Didn't run your code yet, but your ABI looks correct so here's what you can try:

  1. Restart (sometimes its something simple)

  2. Double check your web3 version, if you were using an older version of web3, .events won't work so you'll need to upgrade web3 or use older syntax:

var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at("0x1234...ab67" /* address */);

var event = clientReceipt.Deposit();

// watch for changes
event.watch(function(error, result){
    // result will contain various information
    // including the arguments given to the `Deposit`
    // call.
    if (!error)
        console.log(result);
});

// Or pass a callback to start watching immediately
var event = clientReceipt.Deposit(function(error, result) {
    if (!error)
        console.log(result);
});
6166511419

tried, still error. And, what's your web3 version, mine is 1.0.0-beta.34. So in your code, 'var clientReceipt = ClientReceipt.at("0x1234...ab67" /* address */);' will get a error, shows "ClientReceipt.at is not a functin"

Jun 25 '18 at 9:21 am
6166511419
5 months ago Karma: 5

sorry to post my error messasge:

TypeError: contract.events.NewValueSet(...) is not a function

sorry to post my error messasge:

TypeError: contract.events.NewValueSet(...) is not a function
Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER