Skip to content

Instantly share code, notes, and snippets.

View aquiladev's full-sized avatar
🛩️
Flying

Sergii Bomko aquiladev

🛩️
Flying
View GitHub Profile
@xavierlepretre
xavierlepretre / expected_exception_testRPC_and_geth.js
Last active November 28, 2019 17:57
When TestRPC and Geth throw, they behave in a different manner. This Gist is an example on how to cover such a situation.
"use strict";
/**
* @param {!Function.<!Promise>} action.
* @param {!Number | !string | !BigNumber} gasToUse.
* @returns {!Promise} which throws unless it hit a valid error.
*/
module.exports = function expectedExceptionPromise(action, gasToUse) {
return new Promise(function (resolve, reject) {
try {
web3.eth.filter("pending").watch(function() {
if (eth.mining) return;
console.log(new Date() + "-- Transactions detected, so starting mining.");
miner.start(1);
});
web3.eth.filter('latest', function(error, result) {
console.log(new Date() + "-- Got latest, so stopping mining");
miner.stop();
if (txpool.status.pending > 0) {
@xavierlepretre
xavierlepretre / rxifyWeb3.js
Last active September 12, 2019 22:29
Convert `web3.eth` asynchronous calls into Rx observables.
const Rx = require('rx');
module.exports = {
rxify: function (web3) {
// List synchronous functions masquerading as values.
var syncGetters = {
db: [],
eth: [ "accounts", "blockNumber", "coinbase", "gasPrice", "hashrate",
"mining", "protocolVersion", "syncing" ],
net: [ "listening", "peerCount" ],
@xavierlepretre
xavierlepretre / sequentialPromise.js
Last active April 25, 2023 16:03
Like `Promise.all()` but where all promises are started sequentially.
const Promise = require("bluebird");
/**
* @param {!Array.<function.Promise.<Any>>} promiseArray.
* @returns {!Promise.<Array.<Any>>} The results of the promises passed to the function.
*/
module.exports = function sequentialPromise(promiseArray) {
const result = promiseArray.reduce(
(reduced, promise, index) => {
reduced.results.push(undefined);
@xavierlepretre
xavierlepretre / sequentialPromiseNamed.js
Last active May 25, 2019 10:56
Like `Promise.all()` but where all promises are started sequentially and it takes an object.
const Promise = require("bluebird");
/**
* @param {!Object.<function.<Promise.<Any>>>} promiseObject. Each key maps to a function
* that returns a promise.
* @returns {!Promise.<Object.<Any>>} The results of the promises passed to the function.
*/
module.exports = function sequentialPromiseNamed(promiseObject) {
const result = Object.keys(promiseObject).reduce(
(reduced, key) => {
"use strict";
const Rx = require('rx');
module.exports = function addFilterObservableToWeb3(web3) {
web3.eth.filterObservable = function(_options) {
const filter = web3.eth.filter(_options);
return Rx.Observable.create(function(observer) {
@spalladino
spalladino / falsehoods-that-ethereum-programmers-believe.md
Last active May 20, 2024 21:04
Falsehoods that Ethereum programmers believe

Falsehoods that Ethereum programmers believe

I recently stumbled upon Falsehoods programmers believe about time zones, which got a good laugh out of me. It reminded me of other great lists of falsehoods, such as about names or time, and made me look for an equivalent for Ethereum. Having found none, here is my humble contribution to this set.

About Gas

Calling estimateGas will return the gas required by my transaction

Calling estimateGas will return the gas that your transaction would require if it were mined now. The current state of the chain may be very different to the state in which your tx will get mined. So when your tx i

@hrkrshnn
hrkrshnn / generic.org
Last active April 21, 2024 01:51
Some generic writeup about common gas optimizations, etc.

Upgrade to at least 0.8.4

Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks for free!

The advantages of versions 0.8.* over <0.8.0 are:

  • Safemath by default from 0.8.0 (can be more gas efficient than some library based safemath).
  • Low level inliner from 0.8.2, leads to cheaper runtime gas. Especially relevant when the contract has small functions. For