Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #202 from web3well/use-custom-aggregator
Browse files Browse the repository at this point in the history
Enable eth_setPreferredAggregator
  • Loading branch information
jacque006 authored May 17, 2022
2 parents 91d04a6 + c8ed676 commit 43d39ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 12 additions & 0 deletions extension/source/Controllers/Network/createEthMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IProviderHandlers {
getProviderState: (
req: JRPCRequest<unknown>,
) => Promise<{ accounts: string[]; chainId: string; isUnlocked: boolean }>;
setPreferredAggregator: (req: JRPCRequest<unknown>) => Promise<string>;
submitBatch: (req: JRPCRequest<SendTransactionParams>) => Promise<string>;
}

Expand All @@ -32,6 +33,7 @@ export function createWalletMiddleware({
getAccounts,
requestAccounts,
getProviderState,
setPreferredAggregator,
submitBatch,
}: IProviderHandlers): JRPCMiddleware<string, unknown> {
if (!getAccounts) {
Expand Down Expand Up @@ -75,6 +77,13 @@ export function createWalletMiddleware({
res.result = await getProviderState(req);
}

async function setPreferredAggregatorWrapper(
req: JRPCRequest<SendTransactionParams>,
res: JRPCResponse<unknown>,
): Promise<void> {
res.result = await setPreferredAggregator(req);
}

async function submitTransaction(
req: JRPCRequest<SendTransactionParams>,
res: JRPCResponse<unknown>,
Expand All @@ -91,6 +100,9 @@ export function createWalletMiddleware({
[PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE]: createAsyncMiddleware(
getProviderStateFromController,
),
eth_setPreferredAggregator: createAsyncMiddleware<any, any>(
setPreferredAggregatorWrapper,
),
eth_sendTransaction: createAsyncMiddleware<any, any>(submitTransaction),
});
}
3 changes: 1 addition & 2 deletions extension/source/Controllers/Network/createJsonRpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
mergeMiddleware,
} from '@toruslabs/openlogin-jrpc';
import { Aggregator } from 'bls-wallet-clients';
import { AGGREGATOR_URL } from '../../env';

import PollingBlockTracker from '../Block/PollingBlockTracker';
import { ProviderConfig } from '../constants';
Expand Down Expand Up @@ -98,7 +97,7 @@ function createAggregatorMiddleware(): JRPCMiddleware<unknown, unknown> {
if (hash in knownTransactions) {
const knownTx = knownTransactions[hash];

const aggregator = new Aggregator(AGGREGATOR_URL);
const aggregator = new Aggregator(knownTx.aggregatorUrl);
const bundleReceipt = await aggregator.lookupReceipt(hash);

if (bundleReceipt === undefined) {
Expand Down
18 changes: 16 additions & 2 deletions extension/source/Controllers/QuillController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export default class QuillController extends BaseController<

private preferencesController!: PreferencesController;

// This is just kept in memory because it supports setting the preferred
// aggregator for the particular tab only.
private tabPreferredAggregators: Record<number, string> = {};

getRequestAccountTabIds: () => Record<string, number>;
getOpenQuillTabsIds: () => Record<number, boolean>;

Expand Down Expand Up @@ -522,9 +526,16 @@ export default class QuillController extends BaseController<
};
},

setPreferredAggregator: async (req: any) => {
// eslint-disable-next-line prefer-destructuring
this.tabPreferredAggregators[req.tabId] = req.params[0];

return 'ok';
},

submitBatch: async (req: any) => {
const txParams = getAllReqParam<SendTransactionParams[]>(req);
const from = txParams[0].from;
const { from } = txParams[0];

const actions = txParams.map((tx) => {
return {
Expand All @@ -541,7 +552,9 @@ export default class QuillController extends BaseController<
};

const bundle = await this.keyringController.signTransactions(from, tx);
const agg = new Aggregator(AGGREGATOR_URL);
const aggregatorUrl =
this.tabPreferredAggregators[req.tabId] ?? AGGREGATOR_URL;
const agg = new Aggregator(aggregatorUrl);
const result = await agg.add(bundle);

if ('failures' in result) {
Expand All @@ -552,6 +565,7 @@ export default class QuillController extends BaseController<
...txParams[0],
nonce: nonce.toString(),
value: txParams[0].value || '0',
aggregatorUrl,
};

return result.hash;
Expand Down
1 change: 1 addition & 0 deletions extension/source/Controllers/knownTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const knownTransactions: Record<
SendTransactionParams & {
nonce: string;
value: BigNumberish;
aggregatorUrl: string;
}
> = {};

Expand Down

0 comments on commit 43d39ef

Please sign in to comment.