Skip to content

Commit

Permalink
fix(app): wallet fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Swepool committed Jun 6, 2024
1 parent 87ca603 commit 6130ab9
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 14 deletions.
109 changes: 109 additions & 0 deletions app/src/lib/wallet/cosmos/chain-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import type {ChainInfo as KeplrChainInfo} from "@keplr-wallet/types";
import type {ChainInfo as LeapChainInfo} from "@leapwallet/types";

//This exist according to docs
interface LeapExtendedInfo extends LeapChainInfo {
theme: {
primaryColor: string;
gradient: string;
};
image: string;
}

//todo handle this for main-net
export const keplrChainInfo: KeplrChainInfo = {
chainId: "union-testnet-8",
chainName: "uniontestnet",
rest: "https://api.testnet.bonlulu.uno",
rpc: "https://rpc.testnet.bonlulu.uno",
bip44: {
coinType: 118,
},
bech32Config: {
bech32PrefixAccAddr: "union",
bech32PrefixAccPub: "unionpub",
bech32PrefixValAddr: "unionvaloper",
bech32PrefixValPub: "unionvaloperpub",
bech32PrefixConsAddr: "unionvalcons",
bech32PrefixConsPub: "unionvalconspub",
},
currencies: [
{
coinDenom: "UNO",
coinMinimalDenom: "muno",
coinDecimals: 6,
coinGeckoId: "cosmos",
},
],
feeCurrencies: [
{
coinDenom: "UNO",
coinMinimalDenom: "muno",
coinDecimals: 6,
coinGeckoId: "union",
gasPriceStep: {
low: 0.0025,
average: 0.025,
high: 0.04,
},
},
],
stakeCurrency: {
coinDenom: "UNO",
coinMinimalDenom: "muno",
coinDecimals: 6,
coinGeckoId: "union",
},
}

//todo handle this for main-net
export const leapChainInfo: LeapExtendedInfo = {
chainId: "union-testnet-8",
chainName: "uniontestnet",
rest: "https://api.testnet.bonlulu.uno",
rpc: "https://rpc.testnet.bonlulu.uno",
bip44: {
coinType: 118,
},
bech32Config: {
bech32PrefixAccAddr: "union",
bech32PrefixAccPub: "unionpub",
bech32PrefixValAddr: "unionvaloper",
bech32PrefixValPub: "unionvaloperpub",
bech32PrefixConsAddr: "unionvalcons",
bech32PrefixConsPub: "unionvalconspub",
},
currencies: [
{
coinDenom: "UNO",
coinMinimalDenom: "muno",
coinDecimals: 6,
coinGeckoId: "cosmos",
},
],
feeCurrencies: [
{
coinDenom: "UNO",
coinMinimalDenom: "muno",
coinDecimals: 6,
coinGeckoId: "union",
},
],
gasPriceStep: {
low: 0.0025,
average: 0.025,
high: 0.04,
},
stakeCurrency: {
coinDenom: "UNO",
coinMinimalDenom: "muno",
coinDecimals: 6,
coinGeckoId: "union",
},
theme: {
primaryColor: '#fff',
gradient:
'linear-gradient(180deg, rgba(255, 255, 255, 0.32) 0%, rgba(255, 255, 255, 0) 100%)',
},
image: "https://raw.githubusercontent.com/cosmos/chain-registry/master/testnets/uniontestnet/images/union.png",
}
45 changes: 31 additions & 14 deletions app/src/lib/wallet/cosmos/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { get } from "svelte/store"
import { sleep } from "$lib/utilities/index.ts"
import { persisted } from "svelte-persisted-store"
import type { ChainWalletStore } from "$lib/wallet/types"
import {get} from "svelte/store"
import {sleep} from "$lib/utilities/index.ts"
import {persisted} from "svelte-persisted-store"
import type {ChainWalletStore} from "$lib/wallet/types"
import {keplrChainInfo, leapChainInfo} from "$lib/wallet/cosmos/chain-info.ts";

export const cosmosWalletsInformation = [
{
Expand Down Expand Up @@ -32,7 +33,7 @@ function createCosmosStore(
}
) {
console.log("[cosmosStore] previousState", previousState)
const { subscribe, set, update } = persisted("cosmos-store", previousState, {
const {subscribe, set, update} = persisted("cosmos-store", previousState, {
syncTabs: true,
storage: "session"
})
Expand All @@ -42,13 +43,28 @@ function createCosmosStore(
subscribe,
connect: async (walletId: string) => {
if (!walletId || (walletId !== "keplr" && walletId !== "leap")) return
update(v => ({ ...v, connectionStatus: "connecting", connectedWallet: walletId }))
if (!window[walletId]) {
update(v => ({...v, connectionStatus: "connecting", connectedWallet: walletId}))
const walletApi = window[walletId];
if (!walletApi) {
alert(`Please install ${walletId} wallet`)
return update(v => ({ ...v, connectionStatus: "disconnected" }))
return update(v => ({...v, connectionStatus: "disconnected"}))
}
await window[walletId]?.enable(["union-testnet-8"])
const account = await window[walletId]?.getKey("union-testnet-8")
const chainInfoMap = {
keplr: keplrChainInfo,
leap: leapChainInfo
};
const chainInfo = chainInfoMap[walletId];
if (!chainInfo) {
alert('Chain information is missing for the selected wallet.');
return update(v => ({...v, connectionStatus: "disconnected"}));
}
try {
await walletApi.experimentalSuggestChain(chainInfo);
await walletApi.enable(["union-testnet-8"])
} catch (e) {
return update(v => ({...v, connectionStatus: "disconnected"}));
}
const account = await walletApi.getKey("union-testnet-8")
update(v => ({
...v,
connectionStatus: "connected",
Expand All @@ -58,20 +74,21 @@ function createCosmosStore(
await sleep(2_000)
},
disconnect: async () => {
const cosmosWalletId = get({ subscribe }).connectedWallet as CosmosWalletId
console.log({ cosmosWalletId })
const cosmosWalletId = get({subscribe}).connectedWallet as CosmosWalletId
console.log({cosmosWalletId})
console.log("[cosmos] cosmosDisconnectClick", get(cosmosStore))
if (cosmosWalletId && cosmosWalletId === "keplr" && window[cosmosWalletId]) {
await window[cosmosWalletId]?.disable("union-testnet-8")
update(v => ({ ...v, connectedWallet: "none", connectionStatus: "disconnected" }))
update(v => ({...v, connectedWallet: "none", connectionStatus: "disconnected"}))
}
if (cosmosWalletId && cosmosWalletId === "leap" && window[cosmosWalletId]) {
await window[cosmosWalletId]?.disconnect("union-testnet-8")
update(v => ({ ...v, connectedWallet: "none", connectionStatus: "disconnected" }))
update(v => ({...v, connectedWallet: "none", connectionStatus: "disconnected"}))
}
}
}
}

export const cosmosStore = createCosmosStore()
// cosmosStore.subscribe(value => localStorage.setItem("cosmos-config", JSON.stringify(value)))

0 comments on commit 6130ab9

Please sign in to comment.