fix: tracked tokens are kept per network, and bundled picks are offered on mainnet only (closes #523)
A network switch now puts the outgoing network's tracked tokens away in a new persisted field, networkTrackedTokens, and brings back the incoming network's list, or an empty one, the way endpoints are already kept per network. trackedTokens stays the active network's list, so its readers are unchanged, and the background's switch keeps the lists too because it goes through the same function. The two add-token screens show their common-token picks, which come from the bundled list of mainnet contracts, on Ethereum Mainnet only; adding by contract address works on every network. Settings redraws its tracked-token list after a switch. Model: opus-5-5
This commit is contained in:
@@ -204,17 +204,21 @@ describe("a chain switch on a worker that never loaded state", () => {
|
||||
expect(after.rpcUrl).toBe(SEPOLIA.defaultRpcUrl);
|
||||
|
||||
// And it took nothing else with it. Without the load these come back
|
||||
// as [], {}, [] and "system" from DEFAULT_STATE — every wallet in the
|
||||
// as [], {}, {} and "system" from DEFAULT_STATE — every wallet in the
|
||||
// extension gone, encrypted secrets included.
|
||||
expect(after.wallets).toEqual(walletFixture());
|
||||
expect(after.hasWallet).toBe(true);
|
||||
expect(after.activeAddress).toBe(ADDRESS);
|
||||
expect(after.allowedSites).toEqual({ [ADDRESS]: [CONNECTED_ORIGIN] });
|
||||
expect(after.trackedTokens).toEqual([
|
||||
{ address: TOKEN, symbol: "DAI", decimals: 18 },
|
||||
]);
|
||||
expect(after.networkTrackedTokens).toEqual({
|
||||
mainnet: [{ address: TOKEN, symbol: "DAI", decimals: 18 }],
|
||||
});
|
||||
expect(after.theme).toBe("dark");
|
||||
|
||||
// The mainnet token is tracked on mainnet only: Sepolia starts with
|
||||
// none of its own.
|
||||
expect(after.trackedTokens).toEqual([]);
|
||||
|
||||
// The user's mainnet endpoint is remembered rather than replaced by
|
||||
// the public default, so switching back returns it.
|
||||
expect(after.networkEndpoints.mainnet).toEqual({
|
||||
@@ -226,6 +230,9 @@ describe("a chain switch on a worker that never loaded state", () => {
|
||||
expect(bg.persisted().rpcUrl).toBe(CUSTOM_RPC);
|
||||
expect(bg.persisted().blockscoutUrl).toBe(CUSTOM_BLOCKSCOUT);
|
||||
expect(bg.persisted().wallets).toEqual(walletFixture());
|
||||
expect(bg.persisted().trackedTokens).toEqual([
|
||||
{ address: TOKEN, symbol: "DAI", decimals: 18 },
|
||||
]);
|
||||
});
|
||||
|
||||
test("compares the requested chain against the stored one, not the default", async () => {
|
||||
|
||||
@@ -97,6 +97,7 @@ const {
|
||||
normalizePersisted,
|
||||
} = require("../src/shared/persistedState");
|
||||
const { stateProblem } = require("../src/shared/stateSchema");
|
||||
const { isKnownNetworkId } = require("../src/shared/networks");
|
||||
const { RESTORABLE_VIEWS } = require("../src/shared/restorableViews");
|
||||
const {
|
||||
bootPopup,
|
||||
@@ -193,6 +194,29 @@ const CONTRACT = [
|
||||
floorOnly: [[1, 2], [null], [{}], [[TOKEN_ADDRESS]]],
|
||||
holds: (v) => everyEntry(v, (t) => isRecord(t) && isText(t.address)),
|
||||
},
|
||||
{
|
||||
field: "networkTrackedTokens",
|
||||
kind: KIND.ENTRIES,
|
||||
hostile: [42, "notarecord", { sepolia: "notalist" }],
|
||||
floorOnly: [
|
||||
[1, 2],
|
||||
{ sepolia: [1, 2] },
|
||||
{ mainnet: [null, {}, [TOKEN_ADDRESS]] },
|
||||
{ notanetwork: [{ address: TOKEN_ADDRESS }] },
|
||||
JSON.parse('{"__proto__":[{"address":"0xabc"}]}'),
|
||||
],
|
||||
// Each list is put straight back onto trackedTokens by
|
||||
// applyChainSwitchFields() when its network becomes the active one, so
|
||||
// it is floored the same way; a key that names no network is dropped.
|
||||
holds: (v) =>
|
||||
isRecord(v) &&
|
||||
Object.getPrototypeOf(v) === Object.prototype &&
|
||||
Object.keys(v).every(
|
||||
(id) =>
|
||||
isKnownNetworkId(id) &&
|
||||
everyEntry(v[id], (t) => isRecord(t) && isText(t.address)),
|
||||
),
|
||||
},
|
||||
{
|
||||
field: "allowedSites",
|
||||
kind: KIND.ENTRIES,
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
// Tracked tokens are kept per network, and the two add-token screens offer the
|
||||
// bundled token list, which holds Ethereum Mainnet contracts, on Ethereum
|
||||
// Mainnet only (https://git.eeqj.de/sneak/AutistMask/issues/523). Driven
|
||||
// through the real popup over a stored profile; the same switch made by the
|
||||
// background is in tests/coldWorkerChainSwitch.test.js.
|
||||
|
||||
const {
|
||||
bootPopup,
|
||||
cleanupPopup,
|
||||
unversionedValidProfile,
|
||||
TOKEN_ADDRESS,
|
||||
} = require("./support/popupBoot");
|
||||
|
||||
const SEPOLIA_TOKEN = {
|
||||
address: TOKEN_ADDRESS,
|
||||
symbol: "TST",
|
||||
decimals: 18,
|
||||
name: "Test Token",
|
||||
};
|
||||
const MAINNET_TOKEN = {
|
||||
address: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
|
||||
symbol: "DAI",
|
||||
decimals: 18,
|
||||
name: "Dai Stablecoin",
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
cleanupPopup();
|
||||
});
|
||||
|
||||
// The popup reopened onto SettingsAddToken, on `networkId`.
|
||||
function bootOnAddToken(networkId, extra) {
|
||||
return bootPopup(
|
||||
unversionedValidProfile({
|
||||
networkId,
|
||||
currentView: "settings-addtoken",
|
||||
...extra,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Picks `networkId` in the Settings network selector, as the user does.
|
||||
async function chooseNetwork(boot, networkId) {
|
||||
const select = boot.node("settings-network");
|
||||
select.value = networkId;
|
||||
for (const fn of select.listeners.change) await fn();
|
||||
await boot.settle();
|
||||
}
|
||||
|
||||
describe.each([
|
||||
["mainnet", false],
|
||||
["sepolia", true],
|
||||
])("on %s", (networkId, hidden) => {
|
||||
test(`the common tokens are hidden: ${hidden}`, async () => {
|
||||
const boot = await bootOnAddToken(networkId);
|
||||
expect(boot.pageErrors).toEqual([]);
|
||||
expect(boot.visibleViews()).toEqual(["settings-addtoken"]);
|
||||
expect(boot.hidden("settings-addtoken-common")).toBe(hidden);
|
||||
|
||||
require("../src/popup/views/addToken").show();
|
||||
expect(boot.visibleViews()).toEqual(["add-token"]);
|
||||
expect(boot.hidden("add-token-common")).toBe(hidden);
|
||||
});
|
||||
});
|
||||
|
||||
test("a token added by contract address on Sepolia is tracked on Sepolia only", async () => {
|
||||
const boot = await bootOnAddToken("sepolia", {
|
||||
networkTrackedTokens: { mainnet: [MAINNET_TOKEN] },
|
||||
});
|
||||
const stored = () => boot.storage.read("autistmask");
|
||||
const { lookupTokenInfo } = require("../src/shared/balances");
|
||||
lookupTokenInfo.mockResolvedValueOnce({
|
||||
symbol: SEPOLIA_TOKEN.symbol,
|
||||
decimals: SEPOLIA_TOKEN.decimals,
|
||||
name: SEPOLIA_TOKEN.name,
|
||||
});
|
||||
|
||||
boot.node("settings-addtoken-address").value = TOKEN_ADDRESS;
|
||||
await boot.click("btn-settings-addtoken-manual");
|
||||
|
||||
expect(lookupTokenInfo).toHaveBeenCalledWith(
|
||||
TOKEN_ADDRESS,
|
||||
expect.any(String),
|
||||
"sepolia",
|
||||
expect.anything(),
|
||||
);
|
||||
expect(stored().trackedTokens).toEqual([SEPOLIA_TOKEN]);
|
||||
|
||||
await chooseNetwork(boot, "mainnet");
|
||||
expect(stored().networkId).toBe("mainnet");
|
||||
expect(stored().trackedTokens).toEqual([MAINNET_TOKEN]);
|
||||
const onMainnet = boot.node("settings-tracked-tokens").innerHTML;
|
||||
expect(onMainnet).toContain("DAI");
|
||||
expect(onMainnet).not.toContain("TST");
|
||||
|
||||
await chooseNetwork(boot, "sepolia");
|
||||
expect(stored().networkId).toBe("sepolia");
|
||||
expect(stored().trackedTokens).toEqual([SEPOLIA_TOKEN]);
|
||||
const onSepolia = boot.node("settings-tracked-tokens").innerHTML;
|
||||
expect(onSepolia).toContain("TST");
|
||||
expect(onSepolia).not.toContain("DAI");
|
||||
});
|
||||
Reference in New Issue
Block a user