Skip to content

Instantly share code, notes, and snippets.

@sogoiii
sogoiii / ListenToEventsWeb3-0-20-4.js
Last active July 31, 2023 06:53
Listening to web3 events in version 0.20.4 and below
const Web3 = require('web3') // Works with web3 0.20.4
const contractArtifact = require('./build/contracts/TutorialToken.json')
const web3 = new Web3()
const providerUrl = 'http://localhost:8545'
const provider = new Web3.providers.HttpProvider(providerUrl)
web3.setProvider(provider)
const networkId = web3.version.network
const contractAddr = contractArtifact.networks[networkId].address
@sogoiii
sogoiii / ListenToEventsWeb3-1-0-0-beta.js
Last active December 26, 2023 23:00
Listening to Solidity events using web3 1.0.0 beta
const Web3 = require('web3') // Works with web3 1.0.0-beta27
const contractArtifact = require('./build/contracts/TutorialToken.json')
const web3 = new Web3()
const providerUrl = 'ws://localhost:8545' // requires # https://github.com/trufflesuite/ganache-cli/releases/tag/v7.0.0-beta.0 or https://github.com/trufflesuite/ganache/releases/tag/v1.1.0-beta.0
const provider = new Web3.providers.WebsocketProvider(providerUrl)
web3.setProvider(provider)
web3.eth.net.getId()
.then(networkId => {
@sogoiii
sogoiii / truffle_contract_example
Created February 15, 2018 02:11
Truffle contract interaction example
import contract from 'truffle-contract'
const contractArtifact = require('./build/contracts/TutorialToken.json')
const web3 = new Web3()
const providerUrl = 'http://localhost:8545'
const provider = new Web3.providers.HttpProvider(providerUrl)
web3.setProvider(provider)
const TutorialToken = contract(contractArtifact)
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 {
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 DB {
uint public data;
function updateData(uint _val) public {
data = _val;
}
}
@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) {
@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 / 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'])
})