@0xcert/ethereum-generic-provider
Advanced tools
Comparing version 2.0.0-alpha3 to 2.0.0-alpha4
@@ -5,4 +5,4 @@ { | ||
{ | ||
"version": "2.0.0-alpha3", | ||
"tag": "@0xcert/ethereum-generic-provider_v2.0.0-alpha3", | ||
"version": "2.0.0-alpha4", | ||
"tag": "@0xcert/ethereum-generic-provider_v2.0.0-alpha4", | ||
"date": "Wed, 19 Jun 2019 10:31:05 GMT", | ||
@@ -9,0 +9,0 @@ "comments": {} |
@@ -5,3 +5,3 @@ # Change Log - @0xcert/ethereum-generic-provider | ||
## 2.0.0-alpha3 | ||
## 2.0.0-alpha4 | ||
Wed, 19 Jun 2019 10:31:05 GMT | ||
@@ -8,0 +8,0 @@ |
@@ -44,2 +44,4 @@ /// <reference types="node" /> | ||
resolve(): Promise<any>; | ||
retry(): Promise<void>; | ||
cancel(): Promise<void>; | ||
protected resolveCurrentState(): Promise<any>; | ||
@@ -46,0 +48,0 @@ protected loopUntilCompleted(): Promise<void>; |
@@ -11,3 +11,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const normalize_address_1 = require("@0xcert/ethereum-utils/dist/lib/normalize-address"); | ||
const ethereum_utils_1 = require("@0xcert/ethereum-utils"); | ||
const scaffold_1 = require("@0xcert/scaffold"); | ||
@@ -118,2 +118,63 @@ const events_1 = require("events"); | ||
} | ||
retry() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const tx = yield this.getTransactionObject(); | ||
if (!tx) { | ||
throw new Error('Mutation not found'); | ||
} | ||
else if (tx.blockNumber) { | ||
throw new Error('Mutation already accepted onto the blockchain'); | ||
} | ||
const gasPrice = yield this._provider.post({ | ||
method: 'eth_gasPrice', | ||
params: [], | ||
}); | ||
const oldGasPrice = tx.gasPrice; | ||
const retryGasPrice = gasPrice.result * this._provider.retryGasPriceMultiplier; | ||
const newGasPrice = retryGasPrice >= oldGasPrice ? retryGasPrice : oldGasPrice * this._provider.retryGasPriceMultiplier; | ||
const attrs = { | ||
from: tx.from, | ||
data: tx.input, | ||
nonce: tx.nonce, | ||
value: tx.value, | ||
gas: tx.gas, | ||
gasPrice: `0x${Math.ceil(newGasPrice).toString(16)}`, | ||
}; | ||
if (tx.to) { | ||
attrs['to'] = tx.to; | ||
} | ||
const res = yield this._provider.post({ | ||
method: 'eth_sendTransaction', | ||
params: [attrs], | ||
}); | ||
this._id = res.result; | ||
}); | ||
} | ||
cancel() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const tx = yield this.getTransactionObject(); | ||
if (!tx) { | ||
throw new Error('Mutation not found'); | ||
} | ||
else if (tx.blockNumber) { | ||
throw new Error('Mutation already accepted onto the blockchain'); | ||
} | ||
else if (tx.from.toLowerCase() !== this._provider.accountId.toLowerCase()) { | ||
throw new Error('You are not the maker of this mutation so you cannot cancel it.'); | ||
} | ||
const newGasPrice = `0x${Math.ceil(tx.gasPrice * 1.1).toString(16)}`; | ||
const attrs = { | ||
from: tx.from, | ||
to: tx.from, | ||
nonce: tx.nonce, | ||
value: '0x0', | ||
gasPrice: newGasPrice, | ||
}; | ||
const res = yield this._provider.post({ | ||
method: 'eth_sendTransaction', | ||
params: [attrs], | ||
}); | ||
this._id = res.result; | ||
}); | ||
} | ||
resolveCurrentState() { | ||
@@ -126,4 +187,4 @@ return __awaiter(this, void 0, void 0, function* () { | ||
if (tx && tx.to) { | ||
this._senderId = normalize_address_1.normalizeAddress(tx.from); | ||
this._receiverId = normalize_address_1.normalizeAddress(tx.to); | ||
this._senderId = ethereum_utils_1.normalizeAddress(tx.from); | ||
this._receiverId = ethereum_utils_1.normalizeAddress(tx.to); | ||
this._confirmations = yield this.getLastBlock() | ||
@@ -130,0 +191,0 @@ .then((lastBlock) => lastBlock - parseInt(tx.blockNumber || lastBlock)) |
@@ -18,2 +18,3 @@ /// <reference types="node" /> | ||
gasPriceMultiplier?: number; | ||
retryGasPriceMultiplier?: number; | ||
sandbox?: Boolean; | ||
@@ -29,2 +30,3 @@ } | ||
gasPriceMultiplier?: number; | ||
retryGasPriceMultiplier?: number; | ||
sandbox: Boolean; | ||
@@ -31,0 +33,0 @@ protected _gatewayConfig: GatewayConfig; |
@@ -30,2 +30,3 @@ "use strict"; | ||
this.gasPriceMultiplier = typeof options.gasPriceMultiplier !== 'undefined' ? options.gasPriceMultiplier : 1.1; | ||
this.retryGasPriceMultiplier = typeof options.retryGasPriceMultiplier !== 'undefined' ? options.retryGasPriceMultiplier : 2; | ||
this.sandbox = typeof options.sandbox !== 'undefined' ? options.sandbox : false; | ||
@@ -32,0 +33,0 @@ this._client = options.client && options.client.currentProvider |
{ | ||
"files": {}, | ||
"arguments": "npm run clean && npx tsc " | ||
"files": { | ||
"packages/0xcert-ethereum-generic-provider/CHANGELOG.json": "31dab44bfa97d789b08c7a0b94f7bb63336c1366", | ||
"packages/0xcert-ethereum-generic-provider/CHANGELOG.md": "c0529144380ebc2bdac39c243eebf01970ddd600", | ||
"packages/0xcert-ethereum-generic-provider/README.md": "22522c3dc727189e74e28372635710c1bdc9bfb2", | ||
"packages/0xcert-ethereum-generic-provider/nodemon.json": "82b893373db9861f1df4b55d8ea68a5d37b118de", | ||
"packages/0xcert-ethereum-generic-provider/package.json": "c3580506646f707caf47bfb8a1dd2352037841d5", | ||
"packages/0xcert-ethereum-generic-provider/src/core/errors.ts": "b232b31b2020e1dd06dbe71b1c4b15e274e5ae55", | ||
"packages/0xcert-ethereum-generic-provider/src/core/helpers.ts": "03f94c8c3f6db51521af66c8c7b74db8e517831f", | ||
"packages/0xcert-ethereum-generic-provider/src/core/mutation.ts": "154a03ce372dbb144d4f91e33ef6d1c820367d5d", | ||
"packages/0xcert-ethereum-generic-provider/src/core/provider.ts": "3e785596baa7c5b7f27158ee38dc531efa6c51db", | ||
"packages/0xcert-ethereum-generic-provider/src/core/types.ts": "7e961a1be60cdcc411e61bcc43d9655067132da3", | ||
"packages/0xcert-ethereum-generic-provider/src/index.ts": "bf5e4056d37d3b3deedc0cfb677226e0557fa842", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/helpers/build-gateway-config-instance-method.test.ts": "cd45eb1eb36135bbbcc2ea900f6e1a7101f4d21d", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/mutation/cancel-instance-method.test.ts": "a1a3cc8b966d4b7bd6798e28615f1c5d7d3e979d", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/mutation/complete-instance-method.test.ts": "e4d506706c64b0bb03fc88eca457380da91a7209", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/mutation/resolve-instance-method.test.ts": "1c4508c4056ece6fc0d15b786515c528870e567f", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/mutation/retry-instance-method.test.ts": "03ddd3ba813f3f5a895eedab9ce5fcf66e69239d", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/account-change-event.test.ts": "a18113ecbfabcedfee471cb5906945ee9e4e78bd", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/account-id-instance-variable.test.ts": "80f43f7cd257883c978ca1e453d6e3f75ee4c788", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/gateway-config-instance-variable.test.ts": "bcfef75bf130cedde2882d72801c2e360df77739", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/get-available-accounts-instance-variable.test.ts": "66a9e082c5066dee4908803e71629351bd33471b", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/get-network-version-instance-method.test.ts": "0c6a8f19567ee84bbd3c5b7afba4a525e31e9bf8", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/is-current-account-instance-variable.test.ts": "4889657d3fbc8dc9d086727c83203fdf4bafe840", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/is-unsafe-recipient-id-instance-variable.test.ts": "a3fa894be43170265e6735944ef2ab9c687596be", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/post-instance-method.test.ts": "de0dd69410a8fcd9365e087a87a99c82005bd9a5", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/sign-instance-method.test.ts": "55612ab56b456a143529c61d5b1b60c3ebde4b1d", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/core/provider/unsafe-recipient-ids-instance-variable.test.ts": "9d8cad6b360ad42785aa9f9924e60e4c7a731eea", | ||
"packages/0xcert-ethereum-generic-provider/src/tests/index.test.ts": "32b15d9df953143675cc1df689805aff9c31f0bb", | ||
"packages/0xcert-ethereum-generic-provider/tsconfig.json": "aaa461c172cf0c93d58ffeef8e3ead76fe299b2e", | ||
"packages/0xcert-ethereum-generic-provider/tslint.json": "c57b3f0cdb7aa74ab2ab02888380f613589cbe66", | ||
"common/config/rush/npm-shrinkwrap.json": "2f2d9986f7c1d779a61e645ffd67426c179b336e" | ||
}, | ||
"arguments": "npm run lint && npx nyc npx specron test " | ||
} |
{ | ||
"name": "@0xcert/ethereum-generic-provider", | ||
"version": "2.0.0-alpha3", | ||
"version": "2.0.0-alpha4", | ||
"description": "Basic implementation of communication provider for the Ethereum blockchain.", | ||
@@ -70,6 +70,6 @@ "main": "./dist/index.js", | ||
"devDependencies": { | ||
"@0xcert/ethereum-sandbox": "2.0.0-alpha3", | ||
"@0xcert/ethereum-sandbox": "2.0.0-alpha4", | ||
"@types/node": "^10.12.24", | ||
"@specron/cli": "^0.5.6", | ||
"@specron/spec": "^0.5.6", | ||
"@specron/cli": "0.6.0", | ||
"@specron/spec": "0.6.0", | ||
"nyc": "^14.1.1", | ||
@@ -81,9 +81,9 @@ "promised-timeout": "^0.5.1", | ||
"typescript": "^3.1.1", | ||
"web3": "1.0.0-beta.37" | ||
"web3": "1.2.0" | ||
}, | ||
"dependencies": { | ||
"@0xcert/ethereum-utils": "2.0.0-alpha3", | ||
"@0xcert/scaffold": "2.0.0-alpha3", | ||
"@0xcert/ethereum-utils": "2.0.0-alpha4", | ||
"@0xcert/scaffold": "2.0.0-alpha4", | ||
"events": "^3.0.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { normalizeAddress } from '@0xcert/ethereum-utils/dist/lib/normalize-address'; | ||
import { normalizeAddress } from '@0xcert/ethereum-utils'; | ||
import { MutationBase, MutationContext, MutationEvent } from '@0xcert/scaffold'; | ||
@@ -243,2 +243,73 @@ import { EventEmitter } from 'events'; | ||
/** | ||
* Retries mutation with a higher gas price if possible. It uses the provider's retryGasMultiplier to | ||
* calculate gas price. | ||
* @notice Returns error if a mutation is already accepted onto the blockchain. | ||
*/ | ||
public async retry() { | ||
const tx = await this.getTransactionObject(); | ||
if (!tx) { | ||
throw new Error('Mutation not found'); | ||
} else if (tx.blockNumber) { | ||
throw new Error('Mutation already accepted onto the blockchain'); | ||
} | ||
const gasPrice = await this._provider.post({ | ||
method: 'eth_gasPrice', | ||
params: [], | ||
}); | ||
const oldGasPrice = tx.gasPrice; | ||
const retryGasPrice = gasPrice.result * this._provider.retryGasPriceMultiplier; | ||
// We first calculate new gas price based on current network conditions and | ||
// retryGasPriceMultiplier if calculated gas price is lower than the original gas price, then we | ||
// use the retryGasPriceMultiplier with the original gas price. | ||
const newGasPrice = retryGasPrice >= oldGasPrice ? retryGasPrice : oldGasPrice * this._provider.retryGasPriceMultiplier; | ||
const attrs = { | ||
from: tx.from, | ||
data: tx.input, | ||
nonce: tx.nonce, | ||
value: tx.value, | ||
gas: tx.gas, | ||
gasPrice: `0x${Math.ceil(newGasPrice).toString(16)}`, | ||
}; | ||
if (tx.to) { | ||
attrs['to'] = tx.to; | ||
} | ||
const res = await this._provider.post({ | ||
method: 'eth_sendTransaction', | ||
params: [attrs], | ||
}); | ||
this._id = res.result; | ||
} | ||
/** | ||
* Cancels mutation if possible. | ||
* @notice Returns error if a mutation is already accepted onto the blockchain or if you are not the | ||
* mutation maker. | ||
*/ | ||
public async cancel() { | ||
const tx = await this.getTransactionObject(); | ||
if (!tx) { | ||
throw new Error('Mutation not found'); | ||
} else if (tx.blockNumber) { | ||
throw new Error('Mutation already accepted onto the blockchain'); | ||
} else if (tx.from.toLowerCase() !== this._provider.accountId.toLowerCase()) { | ||
throw new Error('You are not the maker of this mutation so you cannot cancel it.'); | ||
} | ||
const newGasPrice = `0x${Math.ceil(tx.gasPrice * 1.1).toString(16)}`; | ||
const attrs = { | ||
from: tx.from, | ||
to: tx.from, | ||
nonce: tx.nonce, | ||
value: '0x0', | ||
gasPrice: newGasPrice, | ||
}; | ||
const res = await this._provider.post({ | ||
method: 'eth_sendTransaction', | ||
params: [attrs], | ||
}); | ||
this._id = res.result; | ||
} | ||
/** | ||
* Helper method that resolves current mutation status. | ||
@@ -245,0 +316,0 @@ */ |
@@ -68,2 +68,7 @@ import { Encode, Encoder } from '@0xcert/ethereum-utils'; | ||
/** | ||
* Retry gas price multiplier. Defaults to 2. | ||
*/ | ||
retryGasPriceMultiplier?: number; | ||
/** | ||
* Sandbox mode. False by default. | ||
@@ -115,2 +120,7 @@ */ | ||
/** | ||
* Retry gas price multiplier. Defaults to 2. | ||
*/ | ||
public retryGasPriceMultiplier?: number; | ||
/** | ||
* Sandbox mode. False by default. | ||
@@ -162,2 +172,3 @@ */ | ||
this.gasPriceMultiplier = typeof options.gasPriceMultiplier !== 'undefined' ? options.gasPriceMultiplier : 1.1; | ||
this.retryGasPriceMultiplier = typeof options.retryGasPriceMultiplier !== 'undefined' ? options.retryGasPriceMultiplier : 2; | ||
this.sandbox = typeof options.sandbox !== 'undefined' ? options.sandbox : false; | ||
@@ -345,3 +356,2 @@ | ||
if (payload.method === 'eth_sendTransaction' && payload.params.length) { | ||
if (this.sandbox || typeof payload.params[0].gas === 'undefined') { | ||
@@ -348,0 +358,0 @@ const res = await this.request({ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
222407
100
3077
+ Added@0xcert/ethereum-utils@2.0.0-alpha4(transitive)
+ Added@0xcert/scaffold@2.0.0-alpha4(transitive)
- Removed@0xcert/ethereum-utils@2.0.0-alpha3(transitive)
- Removed@0xcert/scaffold@2.0.0-alpha3(transitive)