Skip to content

Instantly share code, notes, and snippets.

@sogoiii
sogoiii / ethereum-to-graphql-server.js
Last active July 23, 2018 21:16
Server that can be used with the ethereum-to-graphql package
const express = require('express')
const graphqlHTTP = require('express-graphql')
const Web3 = require('web3')
const TFcontract = require('truffle-contract')
const MetaCoinArtifact = require('./build/contracts/Metacoin')
const MetCoinContract = TFcontract(MetaCoinArtifact)
MetCoinContract.setProvider(new Web3.providers.HttpProvider('http://localhost:8545'))
const { genGraphQlProperties } = require('ethereum-to-graphql')
@sogoiii
sogoiii / genOutputFnMapper.js
Created July 23, 2018 21:09
This will create the output mapper for ethereum-to-graphql
const genOutputFn = (outputTypesDef) => {
return (result) => {
const resultsIsArr = Array.isArray(result)
const outputTypesDefIsArray = Array.isArray(outputTypesDef)
const combined = zipOutputWithResults(resultsIsArr, outputTypesDefIsArray, result, outputTypesDef)
return combined.reduce((out, current) => {
let { key, name, result } = current
if (key === 'value') {
if (Array.isArray(result)) {
@sogoiii
sogoiii / describeMultipelIts.js
Last active July 22, 2018 23:00
A nested describe test block with multiple it blocks. Each it block only handles 1 assert.
describe('Testing Mycreated fail cases:', async function () {
let result = ''
it('should fail to create an event because eventName is not a string', async function () {
result = await this.Contract.createEvent(123456, _eventDescription, _eventWebsite, _eventDate, _location, { from: mainAccount })
assert.equal(result.receipt.status, hex.fail, 'TX status should fail')
})
it('should have Mycreatd list be empty', async function () {
let CreatedList = await this.Contract.getMyCreatedEvents.call(4, 1)
assert.deepEqual(convertBigNumArr(CreatedList), ['0', '0', '0', '0'])
@sogoiii
sogoiii / createEventTestBase.js
Created July 22, 2018 22:08
Test handles how to create an event for our smart contracts
it('should fail to create an event because eventName is not a string', async function () {
let op1 = await this.Contract.createEvent(123456, _eventDescription, _eventWebsite, _eventDate, _location, { from: mainAccount })
assert.equal(op1.receipt.status, hex.fail, 'TX status should fail')
let CreatedList = await this.Contract.getMyCreatedEvents.call(4, 1)
assert.deepEqual(convertBigNumArr(CreatedList), ['0', '0', '0', '0'])
let EventList = await this.Contract.getAllEvents.call(4, 1)
assert.deepEqual(convertBigNumArr(EventList), ['0', '0', '0', '0'])
})
@sogoiii
sogoiii / handleTestFnExample.js
Created July 22, 2018 22:04
Example of using handleTestFn to make you Web3js function calls with truffle and ganache-cli.
async function handleTestFn(method, inputs) {
try {
return await method(...inputs)
} catch(e) {
return e.receipt
}
}
it('should fail to create an event because eventName is not a string', async function () {
let op1 = await handleTestFn(this.Contract.createEvent, [123456, _eventDescription, _eventWebsite, _eventDate, _location, { from: mainAccount }])
@sogoiii
sogoiii / sourceFn.js
Created July 22, 2018 18:30
Ethereum-to-graphQL source function
const sourceFn = ({ contract, method, outputMapper, isCall = true, options }) => {
return async function () {
try {
const instance = await contract.deployed()
const fn = (isCall)
? instance[method].call(...Object.values(arguments))
: instance[method](...Object.values(arguments), options)
const data = await fn
return outputMapper(data)
} catch (e) {
pragma solidity ^0.4.18;
contract DB {
uint public data;
function updateData(uint _val) public {
data = _val;
}
}
pragma solidity ^0.4.18;
contract Worker1 {
uint public n;
address public sender;
function setN(uint _n) public {
n = _n;
sender = msg.sender;
}
pragma solidity ^0.4.18;
contract Worker1 {
uint public n;
address public sender;
function setN(uint _n) public {
n = _n;
sender = msg.sender;
pragma solidity ^0.4.18;
contract D {
uint public n;
address public sender;
function callSetN(address _e, uint _n) public {
_e.call(bytes4(keccak256("setN(uint256)")), _n); // E's storage is set, D is not modified
}
function callcodeSetN(address _e, uint _n) public {