Unable to change a state variable in a contract - ethereum

I am developing an Ethereum contract using Truffle and TestRPC. But I am unable to get a state variable to update. I think it might just be that I'm accessing it too early, but other example tests seem to work just fine and are very similar.
I have reduced my contract down to the simplest possible thing that breaks:
pragma solidity ^0.4.11;
contract Adder {
uint public total;
function add(uint amount) {
total += amount;
}
function getTotal() returns(uint){
return total;
}
}
And this is my test:
var Adder = artifacts.require("./Adder.sol");
contract('Adder', accounts => {
it("should start with 0", () =>
Adder.deployed()
.then(instance => instance.getTotal.call())
.then(total => assert.equal(total.toNumber(), 0))
);
it("should increase the total as amounts are added", () =>
Adder.deployed()
.then(instance => instance.add.call(10)
.then(() => instance.getTotal.call())
.then(total => assert.equal(total.toNumber(), 10))
)
);
});
The first test passes ok. But the second test fails because getTotal is still returning 0.

I believe that the issue is that you are always using the .call() method.
This method will, in fact, execute the code but will not save to the blockchain.
You should use the .call() method, only when reading from the blockchain or testing for throws.
Just remove the .call() in the adding function and it should work.
var Adder = artifacts.require("./Adder.sol");
contract('Adder', accounts => {
it("should start with 0", () =>
Adder.deployed()
.then(instance => instance.getTotal.call())
.then(total => assert.equal(total.toNumber(), 0))
);
it("should increase the total as amounts are added", () =>
Adder.deployed()
.then(instance => instance.add(10)
.then(() => instance.getTotal.call())
.then(total => assert.equal(total.toNumber(), 10))
)
);
});
Also, consider declaring the instance variable outside the chain of functions of the promise since the context is not shared. Consider using async/await for tests instead of promises.
var Adder = artifacts.require("./Adder.sol");
contract('Adder', accounts => {
it("should start with 0", async () => {
let instance = await Adder.deployed();
assert.equal((await instance.getTotal.call()).toNumber(), 0);
});
it("should increase the total as amounts are added", async () => {
let instance = await Adder.deployed();
await instance.add(10);
assert.equal((await instance.getTotal.call()).toNumber(), 10);
});
});

Related

Javascript - Return json from fetch in an Object

I'm trying to make an application to get the recipes from https://edamam.com and I'm using fetch and Request object.
I need to make 3 request, and i thought that most beautiful way for do it is make an Object and a method that return the data in JSON.
I declarated into constructor a variable called this.dataJson, and i want to save there the data in JSON from the response. For that purpose i use this.
The problem is that i have a undefined variable.
.then( data => {this.dataJson=data;
console.log(data)} )
This is all my code.
class Recipe{
constructor(url){
this.url=url;
this.dataJson;
this.response;
}
getJson(){
var obj;
fetch(new Request(this.url,{method: 'GET'}))
.then( response => response.json())
.then( data => {this.dataJson=data;
console.log(data)} )
.catch( e => console.error( 'Something went wrong' ) );
}
getData(){
console.log("NO UNDFEIND"+this.dataJson);
}
}
const pa= new Recipe('https://api.edamam.com/search?...');
pa.getJson();
pa.getData();
I'm new studying OOP in JS and more new in Fetch requests...
If you guys can help me... Thanks very much!
Here's a solution using async-await (and a placeholder API):
class Recipe {
constructor(url) {
this.url = url;
this.dataJson;
this.response;
}
// the async keyword ensures that this function returns
// a Promise object -> we can use .then() later (1)
async getJson() {
try {
const response = await fetch(new Request(this.url, {
method: 'GET'
}))
const json = await response.json()
this.dataJson = json
} catch (e) {
console.error('Something went wrong', e)
}
}
getData() {
console.log("NO UNDFEIND:", this.dataJson);
}
}
const pa = new Recipe('https://jsonplaceholder.typicode.com/todos/1');
// 1 - here we can use the "then", as pa.getJson() returns
// a Promise object
pa.getJson()
.then(() => {
pa.getData()
});
If we want to stay closer to your code, then:
class Recipe {
constructor(url) {
this.url = url;
this.dataJson;
this.response;
}
getJson() {
// var obj; // not needed
// the "fetch" always returns a Promise object
return fetch(new Request(this.url, { // return the fetch!
method: 'GET'
}))
.then(response => response.json())
.then(data => {
this.dataJson = data;
// console.log(data) // not needed
})
.catch(e => console.error('Something went wrong'));
}
getData() {
console.log("NO UNDFEIND:", this.dataJson); // different syntax here
}
}
const pa = new Recipe('https://jsonplaceholder.typicode.com/todos/1');
// using "then", because the "fetch" returned a Promise object
pa.getJson()
.then(() => {
pa.getData();
});
The problem with your original code is that you initiate the request (pa.getJson()) and then immediately (on the next line) you want to read the data (pa.getData()). pa.getData() is called synchronously (so it happens in milliseconds), but the request is asynchronous - the data needs time to arrive (probably hundreds of milliseconds) - so, it's not there when you try to read it (it simply hasn't arrived yet).
To avoid this you have to use a technique to handle this asynchronous nature of the request:
use a callback function (blee - so last decade)
use a Promise object with then() (much better) or async-await (yeee!)
and call the pa.getData() when the response has arrived (inside the callback function, in the then() or after awaiting the result).

Error in React mapping when JSON.parse on local storage fails

I have a react app that uses local storage to maintain its state with cookies, but when a cookie exists that has a bad value, the code errors causing the whole app to break. I'm not sure exactly what cookie is the problem, but certain cookies seem to cause `JSON.parse(localStorage[key]) to fail.
const getLocalStorage = () => {
const storage = R.compose(
R.fromPairs,
R.map(([key]) => [key, JSON.parse(localStorage[key])]),
R.toPairs, // [[key, value], [key, value]]
)(localStorage);
return storage;
};
I'd like to just add some error handling so that cookies that cannot be parsed are ignored, but I'm not sure how to do that in this syntax. How can I fix the code so that if JSON.parse fails, it doesn't break?
So you have two options here, the first one is the filter out any cookies that would return an error on parsing before you do your map like so:
const getLocalStorage = () => {
const storage = R.compose(
R.fromPairs,
R.filter([key] => {
try{
JSON.parse(localStorage[key]);
return true;
}
catch(error){
console.log(error);
return false;
}
}).map(([key]) => [key, JSON.parse(localStorage[key])]),
R.toPairs, // [[key, value], [key, value]]
)(localStorage);
return storage;
};
Or you could try using an Array.reduce() like this:
const getLocalStorage = () => {
const storage = R.compose(
R.fromPairs,
R.reduce((acc, curr) =>
{
try{
acc.push[[key, JSON.parse(localStorage[key])]];
}
catch(error){
console.log(error);
}
return acc;
}),
R.toPairs, // [[key, value], [key, value]]
)(localStorage);
return storage;
};

Detect MetaMask logout (Ethereum)

I've looked at the documentation here https://metamask.github.io/metamask-docs/Main_Concepts/Getting_Started
But I'm not sure how to detect a user logging out of MetaMask?
window.ethereum.on('accountsChanged', (accounts) => {
// If user has locked/logout from MetaMask, this resets the accounts array to empty
if (!accounts.length) {
// logic to handle what happens once MetaMask is locked
}
});
Thus, using the above you can detect lock/logout of MetaMask.
window.ethereum.on('accountsChanged', function (accounts) {
let acc = accounts[0]
acc will be undefined if they logged out.
From MetaMask Ethereum Provider API:
ethereum.on('accountsChanged', handler: (accounts: Array<string>) => void);
The MetaMask provider emits this event whenever the return value of the eth_accounts RPC method changes. eth_accounts returns an array that is either empty or contains a single account address. The returned address, if any, is the address of the most recently used account that the caller is permitted to access. Callers are identified by their URL origin, which means that all sites with the same origin share the same permissions.
Metamask documentation suggest you to refresh the page if account is changed.
const setAccountListener = (provider) => {
provider.on("accountsChanged", (_) => window.location.reload());
provider.on("chainChanged", (_) => window.location.reload());
};
Then call this in useEffect
useEffect(() => {
// Load provider
if (provider) {
....
setAccountListener(provider);
// add more logic
} else {
console.error("Please, install Metamask.");
}
};
}, []);
New Feature: _metamask.isUnlocked()
Metamask adds _metamask.isUnlocked() experimental property on ethereum.
const reload = () => window.location.reload();
const handleAccount = (ethereum) => async () => {
const isLocked = !(await ethereum._metamask.isUnlocked());
if (isLocked) {
reload();
}
};
const setListener = (ethereum) => {
ethereum.on("chainChanged", reload);
ethereum.on("accountsChanged", handleAccount(ethereum));
};
const removeListener = (ethereum) => {
ethereum.removeListener("chainChanged", reload);
ethereum.removeListener("accountsChanged", handleAccount(ethereum));
};

Tracking DB querying time - Bookshelf/knex

I would like to monitor the time taken by a query on my API's db. I so created the following function, using bookshelf-signals, a Bookshelf plugin. :
bookshelf.on('fetching', () => {
server.app.fetching = new Date().valueOf();
});
bookshelf.on('counting', () => {
server.app.fetching = new Date().valueOf();
});
bookshelf.on('fetched', () => {
server.statsd.gauge('db_query', new Date().valueOf() - server.app.fetching);
});
... so that I can retrieve the time just before and just after a fetch/count; I did the same with deleting-deleted and saving-saved.
What I think I fail to understand is when fetching and fetched are supposed to be triggered... When I tried to to see when fetching and fetched were triggered, basically it ended up with this :
'fetching event A'
'fetching event B'
'fetching event C'
'fetched event C'
'fetched event B'
'fetched event A'
Resulting in the timers returning wrong values obliviously, do you have any lead/clue ?
I also saw that one could trigger 'query' events on Knex, and thought of using this as an alternative solution. However, it seems that it only works if I specify the table where I query, ie :
knex('whatever_table').on('query', () => {///});
Making it impracticable in the case where I want to apply an event handler on every model...
I think I should stick with Bookshelf, but how can I do with the way the events are handled?
Thank you in advance!
I just wrote some small test code how to trace transaction duration with knex.
https://runkit.com/embed/679qu91ylu4w
/**
* Calculate transaction durations in knex
*
*/
require('sqlite3');
var knex = require("knex")({
client: 'sqlite',
connection: ':memory:',
pool: { min: 1, max: 10 }
});
function isTransactionStart(querySpec) {
return querySpec.sql === 'BEGIN;';
}
function isTransactionEnd(querySpec) {
return querySpec.sql === 'COMMIT;' || querySpec.sql === 'ROLLBACK;';
}
const transactionDurations = {};
knex.on('query', querySpec => {
console.log('On query', querySpec);
if (isTransactionStart(querySpec)) {
if (transactionDurations[querySpec.__knexUid]) {
console.error('New transaction started, before earlier was ended');
return;
}
transactionDurations[querySpec.__knexUid] = new Date().getTime();
}
if (isTransactionEnd(querySpec)) {
const startTime = transactionDurations[querySpec.__knexUid];
if (!startTime) {
console.error('Transaction end detected, but start time not found');
}
const endTime = new Date().getTime();
transactionDurations[querySpec.__knexUid] = null;
console.log('TRANSACTION DURATION', endTime - startTime);
}
});
// just as an example of other available events to show when they are called
knex.on('query-response', (res, querySpec) => {
// console.log('On query response', res, querySpec);
});
knex.on('query-error', (err, querySpec) => {
// console.log('On query error', err, querySpec);
});
try {
a = await Promise.all([
knex.transaction(trx => {
return trx.raw('select 1');
}),
knex.transaction(trx => {
return trx.raw('select 2');
}),
knex.transaction(trx => {
return trx.raw('error me');
})
]);
} catch (e) {
console.log('Got ERROR:', e);
}
The same king of approach should work also for query timing. To prevent timer bookkeeping from leaking memory you should add some cleanup code though.
Query duration timer should be started in query event and stopped in query-response or query-error depending which one triggers first.
To be able to match query - query-response pair querySpec.__knexQueryUid attribute can be used.
Based on Mikael Lepistö snippet I came up with this :
const dbEvents = (server, sdc) => {
knex.on('query', data => {
server.app[data.__knexQueryUid + ''] = new Date().valueOf();
});
knex.on('query-response', (data, obj, builder) => {
sdc.counter('db_queries_time', new Date().valueOf() - server.app[obj.__knexQueryUid + '']);
sdc.increment('nr_db_queries');
});
};
And I then call the function when I start the server - I am working with Hapijs.
EDIT: sdc is a statsd client, I use it to send the DB time :)

(Serverless Framework Module) wait for promise to resolve before return statement

Is it possible for a Serverless Framework module to wait for the "resolve" of a promise before returning?
I'm aware that promises themselves can't do that, but different frameworks/libraries (express, Jasmine, hapijs, etc.) solve this by having a method that defines when to return. I need something like this:
let http = require('http'),
Promise = require('bluebird');
let action = (done) => {
return new Promise((resolve, reject) => {
http
.get('http://domain.com', resolve.bind({}, 'all good!'))
.on('error', reject.bind({}, 'all wrong!'));
})
.then((response) => {
console.log('Result', response);
return done(response); // <----------- I wan't to see this as the response
// of the lambda function
});
};
module.exports.run = (event, context, cb) => cb(null, action(done));
No, promises don't do that. It's impossible to read from the future, and don't want to (cannot) block. Your action is still asynchronous.
But given that your export takes a callback anyway, you can simply invoke that asynchronously:
module.exports.run = (event, context, cb) => {
action().then(res => cb(null, res), err=>cb(err));
};
It would be better though of course if you just returned the promise.