Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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 / ListenToEventsTruffleAnyWeb3.js
Last active July 31, 2023 06:52
Example on how to use truffle to listen to events in your smart contracts
const Web3 = require('web3') // Web3 0.20.4 or web3 1 beta
const truffleContract = require("truffle-contract")
const contractArtifact = require('./build/contracts/TutorialToken.json')
const providerUrl = 'http://localhost:8545'
const provider = new Web3.providers.HttpProvider(providerUrl)
const contract = truffleContract(contractArtifact)
contract.setProvider(provider)
@sogoiii
sogoiii / MyContract.sol
Last active January 22, 2018 03:35
Sample Solidity Events
pragma solidity ^0.4.18;
contract MyContract {
event Transfer(address indexed from, address indexed to, uint256 value); /* This is an event */
function MyContract() {} /* a constructor */
function transfer(address _to) public {
Transfer(msg.sender, _to, msg.value);
@sogoiii
sogoiii / interact.js
Created September 1, 2017 21:53
Interact with a deployed solidity contract
const Web3 = require('web3')
const fs = require('fs')
const ethereumURL = 'http://0.0.0.0:8545'
const provider = new Web3.providers.HttpProvider(ethereumURL)
const web3 = new Web3(provider)
const contractInfo = JSON.parse(fs.readFileSync('./MetaCoinABI.js', 'utf8'))
const MyContract = web3.eth.contract(contractInfo.abi);
const contractInstance = MyContract.at(contractInfo.address);
@sogoiii
sogoiii / index.html
Created July 18, 2017 21:07
Browser to IPFS example
<!DOCTYPE html>
<html>
<head>
<title>JavaScript file upload</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="https://wzrd.in/standalone/buffer"></script>
<script src="https://unpkg.com/ipfs-api@9.0.0/dist/index.js"
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
crossorigin="anonymous"></script>
</head>
@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 / 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 / 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;