Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JCGit2018/9f79d5ef6b966f193987ae707c271856 to your computer and use it in GitHub Desktop.

Select an option

Save JCGit2018/9f79d5ef6b966f193987ae707c271856 to your computer and use it in GitHub Desktop.

Revisions

  1. @xavierlepretre xavierlepretre revised this gist Feb 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ module.exports = function expectedExceptionPromise(action, gasToUse) {
    }
    })
    .then(function (txObj) {
    return typeof txn === "string"
    return typeof txObj === "string"
    ? web3.eth.getTransactionReceiptMined(txObj) // regular tx hash
    : typeof txObj.receipt !== "undefined"
    ? txObj.receipt // truffle-contract function call
  2. @xavierlepretre xavierlepretre revised this gist Jan 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ module.exports = function expectedExceptionPromise(action, gasToUse) {
    // We are in Geth
    if (typeof receipt.status !== "undefined") {
    // Byzantium
    assert.strictEqual(receipt.status, "0x0", "should have reverted");
    assert.strictEqual(parseInt(receipt.status), 0, "should have reverted");
    } else {
    // Pre Byzantium
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
  3. @xavierlepretre xavierlepretre revised this gist Nov 18, 2017. 1 changed file with 25 additions and 21 deletions.
    46 changes: 25 additions & 21 deletions expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    "use strict";

    /**
    * @param {!Function.<!Promise>} action.
    * @param {!Number | !string | !BigNumber} gasToUse.
    @@ -20,26 +22,28 @@ module.exports = function expectedExceptionPromise(action, gasToUse) {
    ? web3.eth.getTransactionReceiptMined(txObj.transactionHash) // deployment
    : txObj; // Unknown last case
    })
    .then(function (receipt) {
    // We are in Geth or TestRPC
    if (typeof receipt.status !== "undefined") {
    // Byzantium
    assert.strictEqual(receipt.status, "0x0", "should have reverted");
    } else {
    // Pre Byzantium
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    .then(
    function (receipt) {
    // We are in Geth
    if (typeof receipt.status !== "undefined") {
    // Byzantium
    assert.strictEqual(receipt.status, "0x0", "should have reverted");
    } else {
    // Pre Byzantium
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    }
    },
    function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1 ||
    (e + "").indexOf("out of gas") > -1 ||
    (e + "").indexOf("invalid opcode") > -1 ||
    (e + "").indexOf("revert") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
    } else {
    throw e;
    }
    }
    })
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1 ||
    (e + "").indexOf("out of gas") > -1 ||
    (e + "").indexOf("invalid opcode") > -1 ||
    (e + "").indexOf("revert") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
    } else {
    throw e;
    }
    });
    );
    };
  4. @xavierlepretre xavierlepretre revised this gist Nov 18, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    /**
    * @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 {
  5. @xavierlepretre xavierlepretre revised this gist Nov 18, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ module.exports = function expectedExceptionPromise(action, gasToUse) {
    : txObj; // Unknown last case
    })
    .then(function (receipt) {
    // We are in Geth
    // We are in Geth or TestRPC
    if (typeof receipt.status !== "undefined") {
    // Byzantium
    assert.strictEqual(receipt.status, "0x0", "should have reverted");
  6. @xavierlepretre xavierlepretre revised this gist Nov 18, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,8 @@ module.exports = function expectedExceptionPromise(action, gasToUse) {
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1 ||
    (e + "").indexOf("out of gas") > -1 ||
    (e + "").indexOf("invalid opcode") > -1) {
    (e + "").indexOf("invalid opcode") > -1 ||
    (e + "").indexOf("revert") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
  7. @xavierlepretre xavierlepretre revised this gist Oct 26, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,6 @@ module.exports = function expectedExceptionPromise(action, gasToUse) {
    // We are in Geth
    if (typeof receipt.status !== "undefined") {
    // Byzantium
    console.log(receipt);
    assert.strictEqual(receipt.status, "0x0", "should have reverted");
    } else {
    // Pre Byzantium
  8. @xavierlepretre xavierlepretre revised this gist Oct 26, 2017. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,15 @@ module.exports = function expectedExceptionPromise(action, gasToUse) {
    : txObj; // Unknown last case
    })
    .then(function (receipt) {
    // We are in Geth or the tx wrongly passed
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    // We are in Geth
    if (typeof receipt.status !== "undefined") {
    // Byzantium
    console.log(receipt);
    assert.strictEqual(receipt.status, "0x0", "should have reverted");
    } else {
    // Pre Byzantium
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    }
    })
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1 ||
  9. @xavierlepretre xavierlepretre revised this gist Sep 12, 2017. 1 changed file with 33 additions and 26 deletions.
    59 changes: 33 additions & 26 deletions expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,33 @@
    var expectedExceptionPromise = function (action, gasToUse) {
    return new Promise(function (resolve, reject) {
    try {
    resolve(action());
    } catch(e) {
    reject(e);
    }
    })
    .then(function (txn) {
    // https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
    return web3.eth.getTransactionReceiptMined(txn);
    })
    .then(function (receipt) {
    // We are in Geth
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    })
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") || (e + "").indexOf("out of gas") || (e + "").indexOf("invalid opcode") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
    } else {
    throw e;
    }
    });
    };
    module.exports = function expectedExceptionPromise(action, gasToUse) {
    return new Promise(function (resolve, reject) {
    try {
    resolve(action());
    } catch(e) {
    reject(e);
    }
    })
    .then(function (txObj) {
    return typeof txn === "string"
    ? web3.eth.getTransactionReceiptMined(txObj) // regular tx hash
    : typeof txObj.receipt !== "undefined"
    ? txObj.receipt // truffle-contract function call
    : typeof txObj.transactionHash === "string"
    ? web3.eth.getTransactionReceiptMined(txObj.transactionHash) // deployment
    : txObj; // Unknown last case
    })
    .then(function (receipt) {
    // We are in Geth or the tx wrongly passed
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    })
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1 ||
    (e + "").indexOf("out of gas") > -1 ||
    (e + "").indexOf("invalid opcode") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
    } else {
    throw e;
    }
    });
    };
  10. @xavierlepretre xavierlepretre revised this gist Aug 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ var expectedExceptionPromise = function (action, gasToUse) {
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    })
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") || (e + "").indexOf("out of gas" || (e + "").indexOf("invalid opcode") > -1) {
    if ((e + "").indexOf("invalid JUMP") || (e + "").indexOf("out of gas") || (e + "").indexOf("invalid opcode") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
  11. @xavierlepretre xavierlepretre revised this gist Aug 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ var expectedExceptionPromise = function (action, gasToUse) {
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    })
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") || (e + "").indexOf("out of gas") > -1) {
    if ((e + "").indexOf("invalid JUMP") || (e + "").indexOf("out of gas" || (e + "").indexOf("invalid opcode") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
  12. @xavierlepretre xavierlepretre revised this gist Oct 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ var expectedExceptionPromise = function (action, gasToUse) {
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    })
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1) {
    if ((e + "").indexOf("invalid JUMP") || (e + "").indexOf("out of gas") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
  13. @xavierlepretre xavierlepretre revised this gist Sep 7, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@ var expectedExceptionPromise = function (action, gasToUse) {
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
    } else {
    throw e;
    }
  14. @xavierlepretre xavierlepretre revised this gist Sep 7, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@ var expectedExceptionPromise = function (action, gasToUse) {
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1) {
    // We are in TestRPC
    } else if ((e + "").indexOf("please check your gas amount") > -1) {
    // We are in Geth for a deployment
    } else {
    throw e;
    }
  15. @xavierlepretre xavierlepretre revised this gist Jul 29, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion expected_exception_testRPC_and_geth.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,6 @@ var expectedExceptionPromise = function (action, gasToUse) {
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    })
    .catch(function (e) {
    console.log(e);
    if ((e + "").indexOf("invalid JUMP") > -1) {
    // We are in TestRPC
    } else {
  16. @xavierlepretre xavierlepretre renamed this gist Jul 29, 2016. 1 changed file with 0 additions and 0 deletions.
  17. @xavierlepretre xavierlepretre revised this gist Jul 29, 2016. 1 changed file with 25 additions and 25 deletions.
    50 changes: 25 additions & 25 deletions chai_test_testRPC_and_geth_throw.js
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,25 @@
    new Promise(function (resolve, reject) {
    try {
    resolve(myContract.myMethod({
    from: myAccount,
    gas: 300000 // Large number
    }));
    } catch(e) {
    reject(e);
    }
    })
    .then(function (txn) {
    return web3.eth.getTransactionReceiptMined(txn); // https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
    })
    .then(function (receipt) {
    assert.equal(receipt.gasUsed, 300000, "should have used all the gas");
    })
    .then(done)
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1) {
    // We are in TestRPC
    done();
    } else {
    done(e);
    }
    });
    var expectedExceptionPromise = function (action, gasToUse) {
    return new Promise(function (resolve, reject) {
    try {
    resolve(action());
    } catch(e) {
    reject(e);
    }
    })
    .then(function (txn) {
    // https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
    return web3.eth.getTransactionReceiptMined(txn);
    })
    .then(function (receipt) {
    // We are in Geth
    assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
    })
    .catch(function (e) {
    console.log(e);
    if ((e + "").indexOf("invalid JUMP") > -1) {
    // We are in TestRPC
    } else {
    throw e;
    }
    });
    };
  18. @xavierlepretre xavierlepretre renamed this gist Jul 21, 2016. 1 changed file with 0 additions and 0 deletions.
  19. @xavierlepretre xavierlepretre created this gist Jul 21, 2016.
    25 changes: 25 additions & 0 deletions Chai test TestRPC and Geth throw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    new Promise(function (resolve, reject) {
    try {
    resolve(myContract.myMethod({
    from: myAccount,
    gas: 300000 // Large number
    }));
    } catch(e) {
    reject(e);
    }
    })
    .then(function (txn) {
    return web3.eth.getTransactionReceiptMined(txn); // https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
    })
    .then(function (receipt) {
    assert.equal(receipt.gasUsed, 300000, "should have used all the gas");
    })
    .then(done)
    .catch(function (e) {
    if ((e + "").indexOf("invalid JUMP") > -1) {
    // We are in TestRPC
    done();
    } else {
    done(e);
    }
    });