Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy 'main' to production #136

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add node selectors to connection settings
  • Loading branch information
samsiegart committed Jan 19, 2024
commit e0b82f741f54307b827f75a0dca863866b873533
122 changes: 116 additions & 6 deletions wallet/src/components/ConnectionSettingsDialog.tsx
Original file line number Diff line number Diff line change
@@ -8,9 +8,13 @@ import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import TextField from '@mui/material/TextField';
import { makeStyles } from '@mui/styles';
import { useMemo, useState } from 'react';
import { useMemo, useState, useEffect } from 'react';
import { withApplicationContext } from '../contexts/Application';
import { NetworkConfigSource, networkConfigUrl } from '../util/connections';
import {
ConnectionConfig,
NetworkConfigSource,
networkConfigUrl,
} from '../util/connections';
import isEqual from 'lodash-es/isEqual';
import { maybeSave } from '../util/storage';

@@ -43,6 +47,43 @@ const ErrorLabel = ({ children }) => {
);
};

const useNodeSuggestions = (networkConfigHref: string) => {
const [apiSuggestions, setApiSuggestions] = useState<string[]>([]);
const [rpcSuggestions, setRpcSuggestions] = useState<string[]>([]);

useEffect(() => {
let didConfigChange = false;
setRpcSuggestions([]);
setApiSuggestions([]);

const updateSuggestions = async () => {
const url = new URL(networkConfigHref);
const res = await fetch(url);
if (!res.ok) {
throw Error(`Cannot fetch network: ${res.status}`);
}
const networkConfig = await res.json();

if (didConfigChange) return;
setApiSuggestions(networkConfig.apiAddrs);
setRpcSuggestions(networkConfig.rpcAddrs);
};

updateSuggestions().catch(() =>
console.error(
'error fetching node suggestions from config',
networkConfigHref,
),
);

return () => {
didConfigChange = true;
};
}, [networkConfigHref]);

return { apiSuggestions, rpcSuggestions };
};

const ConnectionSettingsDialog = ({
onClose,
open,
@@ -60,10 +101,16 @@ const ConnectionSettingsDialog = ({
networkConfigUrl.toSource(connectionConfig.href),
);

const [config, setConfig] = useState(
connectionConfig || { href: networkConfigUrl.fromSource(configSource) },
const [config, setConfig] = useState<ConnectionConfig>(
connectionConfig || {
href: networkConfigUrl.fromSource(configSource),
rpc: undefined,
api: undefined,
},
);

const { apiSuggestions, rpcSuggestions } = useNodeSuggestions(config.href);

const errors = new Set();

try {
@@ -93,10 +140,10 @@ const ConnectionSettingsDialog = ({
}
setConnectionConfig(config);
disconnect(true);
const { href } = config;
const { href, rpc, api } = config;
const isKnown = allConnectionConfigs.some(c => c.href === href);
if (!isKnown) {
setAllConnectionConfigs(conns => [{ href }, ...conns]);
setAllConnectionConfigs(conns => [{ href, rpc, api }, ...conns]);
}
}
onClose();
@@ -119,11 +166,13 @@ const ConnectionSettingsDialog = ({
case 'testnet':
case 'devnet':
setConfig({
...config,
href: `https://${value}.agoric.net/network-config`,
});
break;
case 'localhost':
setConfig({
...config,
href: `${window.location.origin}/wallet/network-config`,
});
break;
@@ -149,7 +198,9 @@ const ConnectionSettingsDialog = ({
options={smartConnectionHrefs}
sx={{ width: 360, mt: 2 }}
onChange={(_, newValue) =>
newValue &&
setConfig({
...config,
href: newValue,
})
}
@@ -164,12 +215,71 @@ const ConnectionSettingsDialog = ({
onChange={ev =>
ev.target.value !== config.href &&
setConfig({
...config,
href: ev.target.value,
})
}
/>
)}
/>
<Autocomplete
value={config.rpc}
id="rpc"
options={rpcSuggestions}
sx={{ width: 360, mt: 2 }}
onChange={(_, newValue) =>
setConfig({
...config,
rpc: newValue ?? undefined,
})
}
renderOption={(props, option) => <li {...props}>{option}</li>}
freeSolo
selectOnFocus
handleHomeEndKeys
renderInput={params => (
<TextField
{...params}
label="RPC Node (Default)"
onChange={ev =>
ev.target.value !== config.rpc &&
setConfig({
...config,
rpc: ev.target.value,
})
}
/>
)}
/>
<Autocomplete
value={config.api}
id="api"
options={apiSuggestions}
sx={{ width: 360, mt: 2 }}
onChange={(_, newValue) =>
setConfig({
...config,
api: newValue ?? undefined,
})
}
renderOption={(props, option) => <li {...props}>{option}</li>}
freeSolo
selectOnFocus
handleHomeEndKeys
renderInput={params => (
<TextField
{...params}
label="API Node (Default)"
onChange={ev =>
ev.target.value !== config.api &&
setConfig({
...config,
api: ev.target.value,
})
}
/>
)}
/>
<ErrorLabel>
{errors.has(Errors.INVALID_URL) ? 'Enter a valid URL' : ''}
</ErrorLabel>
4 changes: 2 additions & 2 deletions wallet/src/contexts/Provider.tsx
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ export type KeplrUtils = {
interactiveSigner: InteractiveSigner;
backgroundSigner: BackgroundSigner;
};
rpc: string;
};

const useDebugLogging = (state, watch) => {
@@ -212,7 +213,7 @@ const Provider = ({ children }) => {
assert(keplr, 'Missing window.keplr');
const { getBytes } = Random;

const chainInfo = await suggestChain(connectionConfig.href, {
const chainInfo = await suggestChain(connectionConfig, {
fetch,
keplr,
random: Math.random,
@@ -236,7 +237,6 @@ const Provider = ({ children }) => {
address: accounts[0]?.address,
signers: { interactiveSigner, backgroundSigner },
chainId: chainInfo.chainId,
// @ts-expect-error used?
rpc: chainInfo.rpc,
});
};
35 changes: 24 additions & 11 deletions wallet/src/util/SuggestChain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NetworkConfig } from '@agoric/casting/src/netconfig';
import type { ChainInfo, Keplr } from '@keplr-wallet/types';
import { bech32Config, stableCurrency, stakeCurrency } from './chainInfo';
import { ConnectionConfig } from './connections';

export const AGORIC_COIN_TYPE = 564;
export const COSMOS_COIN_TYPE = 118;
@@ -10,18 +11,29 @@ export const makeChainInfo = (
caption: string,
randomFloat: number,
walletUrlForStaking?: string,
config?: ConnectionConfig,
): ChainInfo => {
const { chainName, rpcAddrs, apiAddrs } = networkConfig;
const index = Math.floor(randomFloat * rpcAddrs.length);
const rpcIndex = Math.floor(randomFloat * rpcAddrs.length);

const rpcAddr = rpcAddrs[index];
const rpc = rpcAddr.match(/:\/\//) ? rpcAddr : `http://${rpcAddr}`;
let rpc: string;
if (config?.rpc) {
rpc = config.rpc;
} else {
const rpcAddr = rpcAddrs[rpcIndex];
rpc = rpcAddr.match(/:\/\//) ? rpcAddr : `http://${rpcAddr}`;
}

const rest = apiAddrs
? // pick the same index
apiAddrs[index]
: // adapt from rpc
rpc.replace(/(:\d+)?$/, ':1317');
let rest: string;
if (config?.api) {
rest = config.api;
} else {
rest = apiAddrs
? // pick the same index as rpc node
apiAddrs[rpcIndex]
: // adapt from rpc
rpc.replace(/(:\d+)?$/, ':1317');
}

return {
rpc,
@@ -41,7 +53,7 @@ export const makeChainInfo = (
};

export async function suggestChain(
networkConfigHref: string,
config: ConnectionConfig,
{
fetch,
keplr,
@@ -53,8 +65,8 @@ export async function suggestChain(
},
caption?: string,
) {
console.log('suggestChain: fetch', networkConfigHref); // log net IO
const url = new URL(networkConfigHref);
console.log('suggestChain: fetch', config.href); // log net IO
const url = new URL(config.href);
const res = await fetch(url);
if (!res.ok) {
throw Error(`Cannot fetch network: ${res.status}`);
@@ -76,6 +88,7 @@ export async function suggestChain(
caption,
random(),
walletUrlForStaking,
config,
);
console.log('chainInfo', chainInfo);
await keplr.experimentalSuggestChain(chainInfo);
7 changes: 7 additions & 0 deletions wallet/src/util/connections.ts
Original file line number Diff line number Diff line change
@@ -26,3 +26,10 @@ export const networkConfigUrl = {
};

export type NetworkConfigSource = ReturnType<typeof networkConfigUrl.toSource>;

export type ConnectionConfig = {
href: string;
api?: string;
rpc?: string;
accessToken?: string;
};
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -108,13 +108,27 @@
"@endo/promise-kit" "^0.2.56"
node-fetch "^2.6.0"

"@agoric/cosmic-proto@0.2.2-dev-a5437cf.0", "@agoric/cosmic-proto@0.3.1-dev-ff36f02.0+ff36f02", "@agoric/cosmic-proto@^0.3.0":
"@agoric/cosmic-proto@0.2.2-dev-a5437cf.0":
version "0.2.2-dev-a5437cf.0"
resolved "https://registry.yarnpkg.com/@agoric/cosmic-proto/-/cosmic-proto-0.2.2-dev-a5437cf.0.tgz#cba81f0455ba1875d3f389b69d93e36d5569bfb3"
integrity sha512-ocwgjLUJcuKeDP7/RHWQXDqpDDCEEBqsX1JQNjmnkljE5dNkUBiDNMXurzmD+SZJqGDZ1HxaQFfF/9zo99q4vA==
dependencies:
protobufjs "^7.0.0"

"@agoric/cosmic-proto@0.3.1-dev-ff36f02.0+ff36f02":
version "0.3.1-dev-ff36f02.0"
resolved "https://registry.yarnpkg.com/@agoric/cosmic-proto/-/cosmic-proto-0.3.1-dev-ff36f02.0.tgz#f6d6cf21b62e4fffbbc3c5634d3754d17d793f70"
integrity sha512-y266YxgI6ZN8UxqDezFjfcHTTOlo9FhFM22cN9EQVah0Z4FbNzXplYwrNRZZljGvtbb33w+oZNfv89fZ2aB5Gw==
dependencies:
protobufjs "^7.0.0"

"@agoric/cosmic-proto@^0.3.0":
version "0.3.0"
Comment on lines +111 to +126
Copy link
Member

@0xpatrickdev 0xpatrickdev Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth opening a ticket to get @agoric/cosmic-proto resolving down to a single version. Seeing 3 here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yea, created #137

resolved "https://registry.yarnpkg.com/@agoric/cosmic-proto/-/cosmic-proto-0.3.0.tgz#c9d31d3946c91fbb1630f89d8ba63a662bcdacc5"
integrity sha512-cIunby6gs53sGkHx3ALraREbfVQXvsIcObMjQQ0/tZt5HVqwoS7Y1Qj1Xl0ZZvqE8B1Zyk7QMDj829mbTII+9g==
dependencies:
protobufjs "^7.0.0"

"@agoric/ertp@0.16.3-dev-ff36f02.0+ff36f02", "@agoric/ertp@^0.16.3-dev-6bce049.0":
version "0.16.3-dev-ff36f02.0"
resolved "https://registry.yarnpkg.com/@agoric/ertp/-/ertp-0.16.3-dev-ff36f02.0.tgz#6c3cc73cd79383782d16a141cd5901b2de9e1637"