Files
bls-wallet/extension/source/cells/TransformCell.ts
Andrew Morris 1303ffea9b Add network switching (#273)
* Update config system in prep for network switching

* Move builtinNetworks into config

* Move currencyConversionConfig into config

* Select network in ui

* mixtureHasChanged

* Fix issue where ethers Web3Provider assumes network doesn't change, handle addresses changing per network

* Implement per-network information for wallets

* lookupAddress -> pkHashToAddress

* Fix duplication of getting bls network config

* Restore preferred nonce sourcing

* Fix global access of blsNetworksConfig

* Fix global config access

* Fix commented hasChanged

* Fix build failures

* Fix linting issues

* Update extension/config.release.json

Co-authored-by: Jacob Caban-Tomski <jacque006@users.noreply.github.com>

* Update with PR feedback

Switch $preferences to non-$ name.
Add hidden field to networks to hide from end users.
Refactor wallet network data generation. Needs one more pass.

* PR fixes

Fix trailing comma in config json.
Properly inject env vars into config file.

* Move MultiNetowrkConfig to bls-wallet-clients

Add MultiNetworkConfig to clients. Deprecate NetworkConfig.
Update deps in clients.
Add chai-as-promised, ts-node to clients.
Remove need for transpiliation before client tests.
Finish getAndUpdateWalletsNetworkData changes in extension.

* Remove .only from client tests

* Use MultiNetworkConfig from clients lib.

* Fix file misspelling

* Update bls-wallet-clients experimental with main

* Remove empty local.json from CI build

* Update setup script with new extension config

Add troubleshooting section for Deno version

* Update extension & aggregator configs.

Update extension configs to hide all non-deployed networks.
Update aggregator local config to use pks 0 & 1 from main hardhat mnemonic.
Add dangerous command to print private keys from main hardhat mnemonic.

* Default extension network to arbitrum goerli

* Revert changes in aggregator local env

Co-authored-by: Jacob Caban-Tomski <jacque006@users.noreply.github.com>
2022-09-13 20:57:07 -06:00

94 lines
2.5 KiB
TypeScript

import { EventEmitter } from 'events';
import { FormulaCell } from './FormulaCell';
import ICell, { CellEmitter, StrictPartial } from './ICell';
import mixtureHasChanged from './mixtureHasChanged';
export default class TransformCell<Input, T> implements ICell<Awaited<T>> {
events = new EventEmitter() as CellEmitter<Awaited<T>>;
ended: boolean;
formulaCell: FormulaCell<{ input: ICell<Input> }, T>;
constructor(
public input: ICell<Input>,
public mapInput: ($input: Input) => T,
public mapResponse: (
$input: Input,
$output: Awaited<T>,
) => Input | Promise<Input>,
public hasChanged: (
previous: Awaited<T> | undefined,
latest: Awaited<T>,
) => boolean = mixtureHasChanged,
) {
this.ended = input.ended;
this.formulaCell = new FormulaCell(
{ input },
({ $input }) => mapInput($input),
hasChanged,
);
this.formulaCell.events.on('end', () => {
this.ended = true;
this.events.emit('end');
});
}
async read(): Promise<Awaited<T>> {
return await this.formulaCell.read();
}
async write(newValue: Awaited<T>) {
await this.input.write(
await this.mapResponse(await this.input.read(), newValue),
);
}
async update(updates: StrictPartial<Awaited<T>>): Promise<void> {
await this.write({ ...(await this.read()), ...updates });
}
[Symbol.asyncIterator]() {
return this.formulaCell[Symbol.asyncIterator]();
}
/** Creates an ICell for the subscript of another ICell. */
static Sub<Input extends Record<string, unknown>, K extends keyof Input>(
input: ICell<Input>,
key: K,
hasChanged: (
previous: Input[K] | undefined,
latest: Input[K],
) => boolean = mixtureHasChanged,
): ICell<Input[K]> {
return new TransformCell(
input,
($input) => $input[key],
($input, $output) => ({ ...$input, [key]: $output }),
hasChanged,
);
}
/** Like Sub, but also maps undefined|null to defaultValue. */
static SubWithDefault<
Input extends Record<string, unknown>,
K extends keyof Input,
>(
input: ICell<Input>,
key: K,
defaultValue: Exclude<Input[K], undefined | null>,
hasChanged: (
previous: Input[K] | undefined,
latest: Input[K],
) => boolean = mixtureHasChanged,
): ICell<Exclude<Input[K], undefined | null>> {
return new TransformCell(
input,
($input) => $input[key] ?? defaultValue,
($input, $output) => ({ ...$input, [key]: $output }),
hasChanged,
);
}
}