Skip to content

Commit

Permalink
Merge pull request #1217 from chainapsis/delivan/keplr-393,keplr-394
Browse files Browse the repository at this point in the history
Starknet QA - 5
  • Loading branch information
delivan authored Oct 14, 2024
2 parents 053d345 + 798876c commit 9ac27de
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 196 deletions.
162 changes: 121 additions & 41 deletions apps/extension/src/pages/main/components/token-found-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,43 @@ export const TokenFoundModal: FunctionComponent<{
const tokenScans = chainStore.tokenScans.slice();

for (const enable of enables) {
if (
keyRingStore.needKeyCoinTypeFinalize(
keyRingStore.selectedKeyInfo.id,
chainStore.getChain(enable)
)
) {
const modularChainInfo = chainStore.getModularChain(enable);
if ("cosmos" in modularChainInfo) {
if (
keyRingStore.needKeyCoinTypeFinalize(
keyRingStore.selectedKeyInfo.id,
chainStore.getChain(enable)
)
) {
const tokenScan = tokenScans.find((tokenScan) => {
return ChainIdHelper.parse(tokenScan.chainId).identifier === enable;
});

if (tokenScan && tokenScan.infos.length > 1) {
needBIP44Selects.push(enable);
enables.splice(enables.indexOf(enable), 1);
}

if (
tokenScan &&
tokenScan.infos.length === 1 &&
tokenScan.infos[0].coinType != null
) {
await keyRingStore.finalizeKeyCoinType(
keyRingStore.selectedKeyInfo.id,
enable,
tokenScan.infos[0].coinType
);
}
}
} else if ("starknet" in modularChainInfo) {
const tokenScan = tokenScans.find((tokenScan) => {
return ChainIdHelper.parse(tokenScan.chainId).identifier === enable;
});

if (tokenScan && tokenScan.infos.length > 1) {
needBIP44Selects.push(enable);
enables.splice(enables.indexOf(enable), 1);
}

if (
tokenScan &&
tokenScan.infos.length === 1 &&
tokenScan.infos[0].coinType != null
) {
await keyRingStore.finalizeKeyCoinType(
keyRingStore.selectedKeyInfo.id,
enable,
tokenScan.infos[0].coinType
);
}
}
}

Expand Down Expand Up @@ -318,7 +329,11 @@ const FoundChainView: FunctionComponent<{
<Columns sum={1} gutter="0.5rem" alignY="center">
<Box width="2.25rem" height="2.25rem">
<ChainImageFallback
chainInfo={chainStore.getChain(tokenScan.chainId)}
chainInfo={
chainStore.hasChain(tokenScan.chainId)
? chainStore.getChain(tokenScan.chainId)
: chainStore.getModularChain(tokenScan.chainId)
}
size="2rem"
alt="Token Found Modal Chain Image"
/>
Expand All @@ -332,7 +347,12 @@ const FoundChainView: FunctionComponent<{
: ColorPalette["gray-10"]
}
>
{chainStore.getChain(tokenScan.chainId).chainName}
{
(chainStore.hasChain(tokenScan.chainId)
? chainStore.getChain(tokenScan.chainId)
: chainStore.getModularChain(tokenScan.chainId)
).chainName
}
</Subtitle2>
<Body3 color={ColorPalette["gray-300"]}>{numTokens} Tokens</Body3>
</Stack>
Expand Down Expand Up @@ -391,7 +411,11 @@ const FoundTokenView: FunctionComponent<{
<Columns sum={1} gutter="0.5rem" alignY="center">
<Box width="1.75rem" height="1.75rem">
<CurrencyImageFallback
chainInfo={chainStore.getChain(chainId)}
chainInfo={
chainStore.hasChain(chainId)
? chainStore.getChain(chainId)
: chainStore.getModularChain(chainId)
}
currency={asset.currency}
size="1.75rem"
alt="Token Found Modal Token Image"
Expand All @@ -405,11 +429,38 @@ const FoundTokenView: FunctionComponent<{
: ColorPalette["gray-50"]
}
>
{
chainStore
.getChain(chainId)
.forceFindCurrency(asset.currency.coinMinimalDenom).coinDenom
}
{(() => {
if (chainStore.hasChain(chainId)) {
return chainStore
.getChain(chainId)
.forceFindCurrency(asset.currency.coinMinimalDenom).coinDenom;
} else {
const modularChainInfo = chainStore.getModularChain(chainId);
if ("starknet" in modularChainInfo) {
return (
chainStore
.getModularChainInfoImpl(chainId)
.getCurrencies("starknet")
.find(
(cur) =>
cur.coinMinimalDenom === asset.currency.coinMinimalDenom
)?.coinDenom ?? asset.currency.coinDenom
);
} else if ("cosmos" in modularChainInfo) {
return (
chainStore
.getModularChainInfoImpl(chainId)
.getCurrencies("cosmos")
.find(
(cur) =>
cur.coinMinimalDenom === asset.currency.coinMinimalDenom
)?.coinDenom ?? asset.currency.coinDenom
);
} else {
return asset.currency.coinDenom;
}
}
})()}
</Subtitle3>

<Column weight={1} />
Expand All @@ -421,20 +472,49 @@ const FoundTokenView: FunctionComponent<{
: ColorPalette["gray-50"]
}
>
{uiConfigStore.hideStringIfPrivacyMode(
new CoinPretty(
chainStore
.getChain(chainId)
.forceFindCurrency(asset.currency.coinMinimalDenom),
asset.amount
)
.shrink(true)
.trim(true)
.maxDecimals(6)
.inequalitySymbol(true)
.toString(),
2
)}
{(() => {
const currency = (() => {
if (chainStore.hasChain(chainId)) {
return chainStore
.getChain(chainId)
.forceFindCurrency(asset.currency.coinMinimalDenom);
} else {
const modularChainInfo = chainStore.getModularChain(chainId);
if ("starknet" in modularChainInfo) {
return (
chainStore
.getModularChainInfoImpl(chainId)
.getCurrencies("starknet")
.find(
(cur) =>
cur.coinMinimalDenom === asset.currency.coinMinimalDenom
) ?? asset.currency
);
} else if ("cosmos" in modularChainInfo) {
return (
chainStore
.getModularChainInfoImpl(chainId)
.getCurrencies("cosmos")
.find(
(cur) =>
cur.coinMinimalDenom === asset.currency.coinMinimalDenom
) ?? asset.currency
);
} else {
return asset.currency;
}
}
})();
return uiConfigStore.hideStringIfPrivacyMode(
new CoinPretty(currency, asset.amount)
.shrink(true)
.trim(true)
.maxDecimals(6)
.inequalitySymbol(true)
.toString(),
2
);
})()}
</Subtitle3>
</Columns>
);
Expand Down
75 changes: 54 additions & 21 deletions apps/extension/src/pages/setting/advanced/endpoint/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
checkEvmRpcConnectivity,
checkRestConnectivity,
checkRPCConnectivity,
checkStarknetRpcConnectivity,
DifferentChainVersionError,
} from "@keplr-wallet/chain-validator";
import { useNotification } from "../../../../hooks/notification";
Expand Down Expand Up @@ -44,32 +45,43 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {
const intl = useIntl();

const [chainId, setChainId] = useState<string>(
chainStore.chainInfos[0].chainId
chainStore.modularChainInfos[0].chainId
);
const [originalEndpoint, setOriginalEndpoint] = useState<
| {
rpc: string;
rest: string;
rest?: string;
evmRpc?: string;
}
| undefined
>();
const [isLoading, setIsLoading] = useState(false);

const chainInfo = chainStore.getChain(chainId);
const chainInfo = (() => {
const modularChainInfo = chainStore.getModularChain(chainId);

if ("starknet" in modularChainInfo) {
return modularChainInfo.starknet;
}

return chainStore.getChain(chainId);
})();
const hasRestEndpoint = "rest" in chainInfo;
const hasEvmEndpoint = "evm" in chainInfo && chainInfo.evm != null;

const { setValue, watch, register, handleSubmit } = useForm<{
rpc: string;
lcd: string;
lcd?: string;
evmRpc?: string;
}>({
defaultValues: {
rpc: chainStore.getChain(chainId).rpc,
lcd: chainStore.getChain(chainId).rest,
evmRpc: chainStore.getChain(chainId).evm?.rpc,
rpc: chainInfo.rpc,
...(hasRestEndpoint && { lcd: chainInfo.rest }),
...(hasEvmEndpoint && { evmRpc: chainInfo.evm.rpc }),
},
});

const chainList = chainStore.chainInfosInUI.map((chainInfo) => {
const chainList = chainStore.modularChainInfosInUI.map((chainInfo) => {
return {
key: chainInfo.chainId,
label: chainInfo.chainName,
Expand All @@ -78,8 +90,12 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {

useEffect(() => {
setValue("rpc", chainInfo.rpc);
setValue("lcd", chainInfo.rest);
setValue("evmRpc", chainInfo.evm?.rpc);
if (hasRestEndpoint) {
setValue("lcd", chainInfo.rest);
}
if (hasEvmEndpoint) {
setValue("evmRpc", chainInfo.evm.rpc);
}

const msg = new GetChainOriginalEndpointsMsg(chainId);
new InExtensionMessageRequester()
Expand All @@ -92,7 +108,19 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {

setOriginalEndpoint(undefined);
});
}, [chainId, chainInfo, setValue]);
}, [chainId, chainInfo, hasEvmEndpoint, hasRestEndpoint, setValue]);

const isEndpointNothingChanged = (() => {
const isRpcChanged = chainInfo.rpc !== watch("rpc");
const isLcdChanged = hasRestEndpoint
? chainInfo.rest === watch("lcd")
: false;
const isEvmRpcChanged = hasEvmEndpoint
? chainInfo.evm.rpc === watch("evmRpc")
: false;

return !isRpcChanged && !isLcdChanged && !isEvmRpcChanged;
})();

return (
<HeaderLayout
Expand All @@ -108,10 +136,7 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {
color: "primary",
size: "large",
isLoading,
disabled:
chainInfo.rpc === watch("rpc") &&
chainInfo.rest === watch("lcd") &&
chainInfo.evm?.rpc === watch("evmRpc"),
disabled: isEndpointNothingChanged,
}}
onSubmit={handleSubmit(async (data) => {
setIsLoading(true);
Expand All @@ -120,13 +145,17 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {
if (
!originalEndpoint ||
originalEndpoint.rpc !== data.rpc ||
originalEndpoint.rpc !== data.lcd ||
originalEndpoint.rest !== data.lcd ||
originalEndpoint.evmRpc !== data.evmRpc
) {
try {
if (originalEndpoint?.rpc !== data.rpc) {
try {
await checkRPCConnectivity(chainId, data.rpc);
if (chainId.startsWith("starknet:")) {
await checkStarknetRpcConnectivity(chainId, data.rpc);
} else {
await checkRPCConnectivity(chainId, data.rpc);
}
} catch (e) {
if (
// In the case of this error, the chain version is different.
Expand All @@ -147,7 +176,11 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {
}
}

if (originalEndpoint?.rest !== data.lcd) {
if (
hasRestEndpoint &&
data.lcd != undefined &&
originalEndpoint?.rest !== data.lcd
) {
try {
await checkRestConnectivity(chainId, data.lcd);
} catch (e) {
Expand All @@ -171,8 +204,8 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {
}

if (
hasEvmEndpoint &&
data.evmRpc != null &&
chainInfo.evm != null &&
originalEndpoint?.evmRpc !== data.evmRpc
) {
await checkEvmRpcConnectivity(
Expand Down Expand Up @@ -269,8 +302,8 @@ export const SettingAdvancedEndpointPage: FunctionComponent = observer(() => {
</Columns>

<TextInput label="RPC" {...register("rpc")} />
<TextInput label="LCD" {...register("lcd")} />
{chainInfo.evm && (
{hasRestEndpoint && <TextInput label="LCD" {...register("lcd")} />}
{hasEvmEndpoint && (
<React.Fragment>
<TextInput label="EVM RPC" {...register("evmRpc")} />
<Gutter size="0" />
Expand Down
3 changes: 2 additions & 1 deletion apps/extension/src/stores/chain/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class ChainStore extends BaseChainStore<ChainInfoWithCoreTypes> {
@computed
get tokenScans(): TokenScan[] {
return this._tokenScans.filter((scan) => {
if (!this.hasChain(scan.chainId)) {
if (!this.hasChain(scan.chainId) && !this.hasModularChain(scan.chainId)) {
return false;
}

Expand Down Expand Up @@ -413,6 +413,7 @@ export class ChainStore extends BaseChainStore<ChainInfoWithCoreTypes> {
BACKGROUND_PORT,
new RevalidateTokenScansMsg(id)
);

if (res.vaultId === this.keyRingStore.selectedKeyInfo?.id) {
runInAction(() => {
this._tokenScans = res.tokenScans;
Expand Down
3 changes: 2 additions & 1 deletion packages/background/src/chains/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ export class ClearChainEndpointsMsg extends Message<{

export class GetChainOriginalEndpointsMsg extends Message<{
rpc: string;
rest: string;
rest?: string;
evmRpc?: string;
}> {
public static type() {
return "get-chain-original-endpoints";
Expand Down
Loading

0 comments on commit 9ac27de

Please sign in to comment.