Skip to content

Instantly share code, notes, and snippets.

View xavierlepretre's full-sized avatar
🤯

Xavier Leprêtre xavierlepretre

🤯
View GitHub Profile
@xavierlepretre
xavierlepretre / IsConnectedTestRule.java
Created November 14, 2015 17:03
"Assume Android is connected". Runs the individual test if the Android device is connected to the network, in an Espresso test environment.
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.test.InstrumentationRegistry;
import org.junit.Assume;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
@xavierlepretre
xavierlepretre / keybase.md
Created January 23, 2016 15:38
Keybase.io proof of ownership

Keybase proof

I hereby claim:

  • I am xavierlepretre on github.
  • I am xavierlepretre (https://keybase.io/xavierlepretre) on keybase.
  • I have a public key whose fingerprint is A75D D98D B756 36FA AFE8 2BDD 78C5 1285 AAC6 B231

To claim this, I am signing this object:

@xavierlepretre
xavierlepretre / getTransactionReceiptMined.js
Last active February 5, 2023 03:26
Get the Promise of an Ethereum transaction receipt when it is finally mined
const Promise = require("bluebird");
const sequentialPromise = require("./sequentialPromise.js");
/**
* @param {!string | !Array.<!string>} txHash, a transaction hash or an array of transaction hashes.
* @param {Number} interval, in seconds.
* @returns {!Promise.<!object> | !Promise.<!Array.<!object>>} the receipt or an array of receipts.
*/
module.exports = function getTransactionReceiptMined(txHash, interval) {
const self = this;
@xavierlepretre
xavierlepretre / getAccountsPromise.js
Created July 19, 2016 11:22
Get the Promise of Web3 accounts
web3.eth.getAccountsPromise = function () {
return new Promise(function (resolve, reject) {
web3.eth.getAccounts(function (e, accounts) {
if (e != null) {
reject(e);
} else {
resolve(accounts);
}
});
});
@xavierlepretre
xavierlepretre / mistRequestAccountPromise.js
Created July 19, 2016 11:23
Get the Promise of an account on Ethereum Mist.
if (typeof(mist) !== "undefined") {
mist.requestAccountPromise = function () {
return new Promise (function (resolve, reject) {
mist.requestAccount(function(e, account) {
if(e != null) {
reject(e);
} else {
resolve(account);
}
});
@xavierlepretre
xavierlepretre / getFirstAccountPromise.js
Last active March 16, 2018 09:25
Get the Promise of an account on Web3 or Mist.
web3.eth.getFirstAccountPromise = function () {
// https://gist.github.com/xavierlepretre/ed82f210df0f9300493d5ca79893806a
return web3.eth.getAccountsPromise()
.then(function (accounts) {
if (accounts.length > 0) {
return accounts[0];
} else if (typeof(mist) !== "undefined") {
// https://gist.github.com/xavierlepretre/ee456323b2544dd4da22cd5fa6d2894c
return mist.requestAccountPromise();
} else {
@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 / getEventsPromise.js
Last active September 7, 2017 16:42
Get a Promise on events fired
getEventsPromise= function (myFilter, count, timeOut) {
timeOut = timeOut ? timeOut : 30000;
var promise = new Promise(function (resolve, reject) {
count = (typeof count !== "undefined") ? count : 1;
var results = [];
var toClear = setTimeout(function () {
reject("Timed out");
}, timeOut);
myFilter.watch(function (error, result) {
if (error) {
@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" ],