Skip to content

Instantly share code, notes, and snippets.

@sogoiii
sogoiii / metacoin.js
Last active March 22, 2017 23:28
Truffle 3.0 tests in async await
var MetaCoin = artifacts.require("./MetaCoin.sol");
contract('MetaCoin', function(accounts) {
it("should put 10000 MetaCoin in the first account", async function() {
let meta = await MetaCoin.deployed();
let balance = await meta.getBalance.call(accounts[0]);
assert.equal(balance.valueOf(), 10000, "10000 wasn't in teh first account")
});
@sogoiii
sogoiii / compareSimpleTest.js
Created June 30, 2017 18:53
Comparing simple truffle test as promise chain or async/await
const status = () => {
return MetaCoin.deployed()
.then(instance => {
return instance.getBalance.call(accounts[0]);
})
.then(balance => {
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
})
}
@sogoiii
sogoiii / asyncIsPromise.js
Created June 30, 2017 18:55
Async returns a promise
const operationOne = function async (data) {
let step1 = await somePromise(data)
let step2 = await anotherPromise(step1)
return step2.data
}
actionOne()
.then(operationOne)
.then(data => {
//more work
})
@sogoiii
sogoiii / MetacoinSol_snippet.sol
Created June 30, 2017 18:59
MetaCoin.sol Snippet 1
contract MetaCoin {
//...Code
uint endTime;
//...Code
modifier onlyAfterDate() {
if(now <= endTime) {
throw;
}
_;
}
@sogoiii
sogoiii / timeTravel.js
Created June 30, 2017 19:00
Promisify evm_increateTime
const timeTravel = function (time) {
return new Promise((resolve, reject) => {
web3.currentProvider.sendAsync({
jsonrpc: "2.0",
method: "evm_increaseTime",
params: [time], // 86400 is num seconds in day
id: new Date().getTime()
}, (err, result) => {
if(err){ return reject(err) }
return resolve(result)
@sogoiii
sogoiii / success_time_travel_test.js
Created June 30, 2017 19:01
Truffle async test with time travel
it("should successfully call specialFn because enough time passed", async function () {
let meta = await MetaCoin.new();
await timeTravel(86400 * 3) //3 days later
await mineBlock() // workaround for https://github.com/ethereumjs/testrpc/issues/336
let status = await meta.specialFn.call();
assert.equal(status, true, "specialFn should be callable after 1 day")
})
@sogoiii
sogoiii / large_promise_chain_test.js
Created June 30, 2017 19:02
A large promise chain test in Truffle
it("should send coin correctly", function() {
var meta;
// Get initial balances of first and second account.
var account_one = accounts[0];
var account_two = accounts[1];
var account_one_starting_balance;
var account_two_starting_balance;
var account_one_ending_balance;
@sogoiii
sogoiii / large_async_await_test.js
Created June 30, 2017 19:03
Large async await test in Truffle
it("should send coin correctly", async function () {
// Get initial balances of first and second account.
var account_one = accounts[0];
var account_two = accounts[1];
var amount = 10;
let meta = await MetaCoin.deployed();
let balance1 = await meta.getBalance.call(account_one);
let balance2 = await meta.getBalance.call(account_two);
@sogoiii
sogoiii / EtherAddressLookupBlackListAnalysis.js
Created July 12, 2017 19:28
EtherAddressLookup analysis on their blacklist
const blacklist = require('./blacklist')
const levenshtein = require('fast-levenshtein')
const math = require('mathjs')
const comparison = 'myetherwallet'
const allScores = blacklist.map(item => {
return {
domain: item,
score: levenshtein.get(item.replace(/\./g,''), comparison)
}
@sogoiii
sogoiii / turfffle_test_snippet_2.js
Last active August 10, 2017 23:10
Truffle test snippet 2
it("should fail because function does not exist on contract", async function () {
let meta = await MetaCoin.deployed();
try {
await meta.someNonExistentFn.call();
} catch (e) {
return true;
}
throw new Error("I should never see this!")
})