This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
gasPrice and tx.data middlewares #1013
Merged
Merged
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e0f505c
add provider engine
Velenir 0feaae0
hack hackity hack
Velenir 328b0b9
make work without WS provider
Velenir 0229f78
Merge branch 'develop' into composed_provider_x
Velenir 8bf4972
remove fetchGasPrice at tx sending endpoints
Velenir 1b1adca
account for object changes in logging
Velenir 5ea8ebc
conditional middleware helper
Velenir bc01b57
add gasPrice middleware
Velenir f45a913
add tx.data earmark middleware
Velenir 2910d57
rename encoder
Velenir 6695bee
comment out decoder
Velenir 3100ea0
fix and add tests
Velenir 4b141e9
cleanup and remove extra
Velenir 77eb65e
remove earmarking from gasPrice
Velenir 1a6364c
Merge branch 'develop' into gas_n_data_mwares
Velenir 825f48f
make createConditionalMiddleware generic
Velenir 18aaa3f
Merge branch 'develop' into gas_n_data_mwares
Velenir 99a23a6
Proxy composed provider (#1026)
Velenir 1ff9fb0
Merge branch 'develop' into gas_n_data_mwares
Velenir 5475ccb
Merge branch 'develop' into gas_n_data_mwares
Velenir a9cd3cc
fix types from merge
Velenir 3f391ad
change SNETINEL
Velenir 4fc4d94
cleanup
Velenir 4f66683
fix typo
Velenir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { logDebug } from 'utils' | ||
|
||
// 0.1 Gwei, reasonable gasPrice that would still allow flags replacement | ||
export const MIN_GAS_PRICE = (1e8).toString(10) | ||
|
||
export const earmarkGasPrice = (gasPrice: string, userPrint: string): string => { | ||
if (!userPrint) return gasPrice | ||
|
||
// don't replace 8000 -> 1201, only if most significant digit is untouched | ||
// 80000 -> 81201 | ||
if (userPrint.length >= gasPrice.length) { | ||
// if flags still don't fit even in MIN_GAS_PRICE | ||
if (userPrint.length >= MIN_GAS_PRICE.length) return gasPrice | ||
gasPrice = MIN_GAS_PRICE | ||
} | ||
|
||
const markedGasPrice = gasPrice.slice(0, -userPrint.length) + userPrint | ||
|
||
logDebug('Gas price', gasPrice, '->', markedGasPrice) | ||
return markedGasPrice | ||
} | ||
|
||
// simple concatetaion, with '0x' for empty data to have `0x<userPrint>` at the least | ||
export const earmarkTxData = (data = '0x', userPrint: string): string => data + userPrint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import RpcEngine, { | ||
JsonRpcEngine, | ||
JsonRpcMiddleware, | ||
JsonRpcRequest, | ||
JsonRpcResponse, | ||
JsonRpcError, | ||
} from 'json-rpc-engine' | ||
import providerFromEngine from 'eth-json-rpc-middleware/providerFromEngine' | ||
import { Provider } from '@gnosis.pm/dapp-ui' | ||
import { WebsocketProvider, TransactionConfig } from 'web3-core' | ||
import { numberToHex } from 'web3-utils' | ||
|
||
// custom providerAsMiddleware | ||
function providerAsMiddleware(provider: Provider | WebsocketProvider): JsonRpcMiddleware { | ||
// MMask provider uses sendAsync | ||
// WS provider doesn't have sendAsync | ||
const sendFName = 'sendAsync' in provider ? 'sendAsync' : 'send' | ||
|
||
return (req, res, _next, end): void => { | ||
// send request to provider | ||
|
||
provider[sendFName](req, (err: JsonRpcError<unknown>, providerRes: JsonRpcResponse<unknown>) => { | ||
// forward any error | ||
if (err) return end(err) | ||
// copy provider response onto original response | ||
Object.assign(res, providerRes) | ||
end() | ||
}) | ||
} | ||
} | ||
|
||
const createConditionalMiddleware = <T extends unknown>( | ||
condition: (req: JsonRpcRequest<T>) => boolean, | ||
handle: (req: JsonRpcRequest<T>, res: JsonRpcResponse<T>) => boolean | Promise<boolean>, // handled -- true, not --false | ||
): JsonRpcMiddleware => { | ||
return async (req: JsonRpcRequest<T>, res: JsonRpcResponse<T>, next, end): Promise<void> => { | ||
// if not condition, skip and got to next middleware | ||
if (!condition(req)) return next() | ||
|
||
// if handled fully, end here | ||
if (await handle(req, res)) return end() | ||
// otherwise continue to next middleware | ||
next() | ||
} | ||
} | ||
|
||
interface ExtraMiddlewareHandlers { | ||
fetchGasPrice(): Promise<string | undefined> | ||
earmarkTxData(data?: string): Promise<string> | ||
} | ||
|
||
export const composeProvider = ( | ||
provider: Provider, | ||
{ fetchGasPrice, earmarkTxData }: ExtraMiddlewareHandlers, | ||
): Provider => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const engine = new (RpcEngine as any)() as JsonRpcEngine | ||
|
||
engine.push( | ||
createConditionalMiddleware<[]>( | ||
req => req.method === 'eth_gasPrice', | ||
async (_req, res) => { | ||
const fetchedPrice = await fetchGasPrice() | ||
|
||
// got price | ||
if (fetchedPrice) { | ||
res.result = numberToHex(fetchedPrice) | ||
// handled | ||
return true | ||
} | ||
|
||
// not handled | ||
return false | ||
}, | ||
), | ||
) | ||
|
||
engine.push( | ||
createConditionalMiddleware<TransactionConfig[]>( | ||
req => req.method === 'eth_sendTransaction', | ||
async req => { | ||
const txConfig = req.params?.[0] | ||
// no parameters, which shouldn't happen | ||
if (!txConfig) return false | ||
|
||
const earmarkedData = await earmarkTxData(txConfig.data) | ||
|
||
txConfig.data = earmarkedData | ||
// don't mark as handled | ||
// pass modified tx on | ||
return false | ||
}, | ||
), | ||
) | ||
|
||
const walletMiddleware = providerAsMiddleware(provider) | ||
|
||
engine.push(walletMiddleware) | ||
|
||
const composedProvider: Provider = providerFromEngine(engine) | ||
|
||
const providerProxy = new Proxy(composedProvider, { | ||
get: function(target, prop, receiver): unknown { | ||
if (prop === 'sendAsync' || prop === 'send') { | ||
// composedProvider handles it | ||
return Reflect.get(target, prop, receiver) | ||
} | ||
// MMask or other provider handles it | ||
return Reflect.get(provider, prop, receiver) | ||
}, | ||
}) | ||
|
||
return providerProxy | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.