Skip to content

Commit

Permalink
Adding ovm-truffle-provider-wrapper (#12)
Browse files Browse the repository at this point in the history
Adding ovm-truffle-provider-wrapper
  • Loading branch information
willmeister authored Feb 20, 2020
1 parent bbca409 commit 9973526
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 28 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build-test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ jobs:
path: packages/examples/node_modules
key: ${{ runner.os }}-${{ matrix.node }}-${{ hashFiles('packages/examples/package.json') }}

- name: Cache ovm-truffle-provider-wrapper deps
uses: actions/cache@v1
id: cache_ovm-truffle-provider-wrapper
with:
path: packages/ovm-truffle-provider-wrapper/node_modules
key: ${{ runner.os }}-${{ matrix.node }}-${{ hashFiles('packages/ovm-truffle-provider-wrapper/package.json') }}

# END DEPENDENCY CACHING

- name: Install Dependencies
Expand Down
16 changes: 3 additions & 13 deletions packages/docs/src/core/src/integrating-tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ Using With Truffle

To use the transpiler with Truffle, set truffle's ``compilers.solc.version`` configuration to ``@eth-optimism/solc-transpiler``, and configure the ``EXECUTION_MANAGER_ADDRESS`` environment variable.

Currently, Truffle does not provide a clean way to use custom chain IDs, so we also need a custom function in Truffle's configuration file.
Currently, Truffle does not provide a clean way to use custom chain IDs, so we have created the ``@eth-optimism/ovm-truffle-provider-wrapper`` library to seamlessly wrap your provider of choice to handle this.

example truffle-config.json:

.. code-block:: json
const HDWalletProvider = require('truffle-hdwallet-provider');
const wrapProvider = require('@eth-optimism/ovm-truffle-provider-wrapper');
const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat';
// Set this to the desired Execution Manager Address -- required for the transpiler
Expand All @@ -75,17 +75,7 @@ example truffle-config.json:
test: {
network_id: 108,
provider: function() {
const wallet = new HDWalletProvider(mnemonic, "http://127.0.0.1:8545/", 0, 10);
const sendAsync = wallet.sendAsync
wallet.sendAsync = function (...args) {
if (args[0].method === 'eth_sendTransaction') {
// HACK TO PROPERLY SET CHAIN ID
args[0].params[0].chainId = 108
}
sendAsync.apply(this, args)
};
return wallet;
return wrapProvider(new HDWalletProvider(mnemonic, "http://127.0.0.1:8545/", 0, 10));
},
gasPrice: 0,
gas: 9000000,
Expand Down
1 change: 1 addition & 0 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"@eth-optimism/solc-transpiler": "~0.0.1-alpha.7",
"@eth-optimism/ovm-truffle-provider-wrapper": "~0.0.1-alpha.8",
"ethereum-waffle": "2.1.0",
"rimraf": "^2.6.3",
"truffle": "^5.1.12",
Expand Down
20 changes: 5 additions & 15 deletions packages/examples/truffle-config-ovm.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const HDWalletProvider = require('truffle-hdwallet-provider');

const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat';
const HDWalletProvider = require("truffle-hdwallet-provider");
const wrapProvider = require("@eth-optimism/ovm-truffle-provider-wrapper");
const mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat";

// Set this to the desired Execution Manager Address -- required for the transpiler
process.env.EXECUTION_MANAGER_ADDRESS = process.env.EXECUTION_MANAGER_ADDRESS || "0xA193E42526F1FEA8C99AF609dcEabf30C1c29fAA";
const gasPrice = process.env.OVM_DEFAULT_GAS_PRICE || 0;
const gas = process.env.OVM_DEFAULT_GAS || 9000000;
const chainId = process.env.OVM_CHAIN_ID || 108;


module.exports = {
/**
Expand All @@ -21,17 +21,7 @@ module.exports = {
test: {
network_id: 108,
provider: function() {
const wallet = new HDWalletProvider(mnemonic, "http://127.0.0.1:8545/", 0, 10);
const sendAsync = wallet.sendAsync

wallet.sendAsync = function (...args) {
if (args[0].method === 'eth_sendTransaction') {
// HACK TO PROPERLY SET CHAIN ID
args[0].params[0].chainId = chainId
}
sendAsync.apply(this, args)
};
return wallet;
return wrapProvider(new HDWalletProvider(mnemonic, "http://127.0.0.1:8545/", 0, 10));
},
gasPrice: gasPrice,
gas: gas,
Expand Down
29 changes: 29 additions & 0 deletions packages/ovm-truffle-provider-wrapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# OVM Truffle Provider Wrapper
The OVM uses a specific `chainId`, which Truffle, at the moment, does not allow to be configured globally within a project, so this package simply wraps the provider that is used in order to set the `chainId` field on all transactions.

## Configuration
ChainId defaults to 108 but is configurable by setting the `OVM_CHAIN_ID` environment variable.

## Example Usage in truffle-config.js:
```$javascript
const HDWalletProvider = require('truffle-hdwallet-provider');
const wrapProvider = require('@eth-optimism/ovm-truffle-provider-wrapper');
const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat';
module.exports = {
networks: {
// Note: Requires running the rollup-full-node locally.
test: {
provider: function () {
return wrapProvider(new HDWalletProvider(mnemonic, "http://127.0.0.1:8545/", 0, 10));
},
},
},
compilers: {
solc: {
// Add path to the solc-transpiler
version: "../../node_modules/@eth-optimism/solc-transpiler",
}
}
}
```
19 changes: 19 additions & 0 deletions packages/ovm-truffle-provider-wrapper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = (provider: any) => {
if (typeof provider !== 'object' || !provider['sendAsync']) {
throw Error(
'Invalid provider. Exepcted provider to conform to Truffle provider interface!'
)
}

const chainId = process.env.OVM_CHAIN_ID || 108
const sendAsync = provider.sendAsync

provider.sendAsync = function(...args) {
if (args[0].method === 'eth_sendTransaction') {
// To properly set chainID for all transactions.
args[0].params[0].chainId = chainId
}
sendAsync.apply(this, args)
}
return provider
}
41 changes: 41 additions & 0 deletions packages/ovm-truffle-provider-wrapper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@eth-optimism/ovm-truffle-provider-wrapper",
"version": "0.0.1-alpha.8",
"description": "Optimism Truffle Provider Wrapper",
"main": "build/index.js",
"files": [
"build/index.js"
],
"scripts": {
"all": "yarn clean && yarn build && yarn fix && yarn lint",
"build": "tsc -p .",
"clean": "rimraf build/",
"fix": "prettier --config ../../prettier-config.json --write 'index.ts' ",
"lint": "tslint --format stylish --project ."
},
"keywords": [
"optimism",
"rollup",
"optimistic",
"ethereum",
"client"
],
"homepage": "https://github.com/ethereum-optimism/optimism-monorepo/tree/master/packages/ovm-truffle-provider-wrapper#readme",
"license": "MIT",
"author": "Optimism PBC",
"repository": {
"type": "git",
"url": "https://github.com/ethereum-optimism/optimism-monorepo.git"
},
"dependencies": {
},
"devDependencies": {
"@types/node": "^12.0.7",
"rimraf": "^2.6.3",
"ts-node": "^8.2.0",
"typescript": "^3.5.1"
},
"publishConfig": {
"access": "public"
}
}
8 changes: 8 additions & 0 deletions packages/ovm-truffle-provider-wrapper/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./../../tsconfig.json",
"compilerOptions": {
"outDir": "./build",
"resolveJsonModule": true
},
"include": ["*.ts"]
}
6 changes: 6 additions & 0 deletions packages/ovm-truffle-provider-wrapper/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["./../../tslint.json"],
"rules": {
"prettier": [true, "../../prettier-config.json"]
}
}

0 comments on commit 9973526

Please sign in to comment.