|
| 1 | +import { |
| 2 | + IChainInfoImpl, |
| 3 | + ObservableQuery, |
| 4 | + QuerySharedContext, |
| 5 | +} from "@keplr-wallet/stores"; |
| 6 | +import { ChainsResponseV2 } from "./types"; |
| 7 | +import { computed, makeObservable } from "mobx"; |
| 8 | +import { ChainIdHelper } from "@keplr-wallet/cosmos"; |
| 9 | +import { computedFn } from "mobx-utils"; |
| 10 | +import Joi from "joi"; |
| 11 | +import { InternalChainStore } from "../internal"; |
| 12 | +import { simpleFetch } from "@keplr-wallet/simple-fetch"; |
| 13 | + |
| 14 | +const Schema = Joi.object<ChainsResponseV2>({ |
| 15 | + chains: Joi.array().items( |
| 16 | + Joi.object({ |
| 17 | + chain_id: Joi.string(), |
| 18 | + pfm_enabled: Joi.boolean(), |
| 19 | + supports_memo: Joi.boolean(), |
| 20 | + chain_type: Joi.string(), |
| 21 | + }).unknown(true) |
| 22 | + ), |
| 23 | +}).unknown(true); |
| 24 | + |
| 25 | +export class ObservableQueryChainsV2 extends ObservableQuery<ChainsResponseV2> { |
| 26 | + constructor( |
| 27 | + sharedContext: QuerySharedContext, |
| 28 | + protected readonly chainStore: InternalChainStore, |
| 29 | + protected readonly skipURL: string |
| 30 | + ) { |
| 31 | + super(sharedContext, skipURL, "/v1/swap/chains"); |
| 32 | + |
| 33 | + makeObservable(this); |
| 34 | + } |
| 35 | + |
| 36 | + protected override async fetchResponse( |
| 37 | + abortController: AbortController |
| 38 | + ): Promise<{ headers: any; data: ChainsResponseV2 }> { |
| 39 | + const _result = await simpleFetch(this.baseURL, this.url, { |
| 40 | + signal: abortController.signal, |
| 41 | + headers: { |
| 42 | + ...(() => { |
| 43 | + const res: { authorization?: string } = {}; |
| 44 | + if (process.env["SKIP_API_KEY"]) { |
| 45 | + res.authorization = process.env["SKIP_API_KEY"]; |
| 46 | + } |
| 47 | + |
| 48 | + return res; |
| 49 | + })(), |
| 50 | + }, |
| 51 | + }); |
| 52 | + const result = { |
| 53 | + headers: _result.headers, |
| 54 | + data: _result.data, |
| 55 | + }; |
| 56 | + |
| 57 | + const validated = Schema.validate(result.data); |
| 58 | + if (validated.error) { |
| 59 | + console.log("Failed to validate chains response", validated.error); |
| 60 | + throw validated.error; |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + headers: result.headers, |
| 65 | + data: validated.value, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + @computed |
| 70 | + get chains(): { |
| 71 | + chainInfo: IChainInfoImpl; |
| 72 | + pfmEnabled: boolean; |
| 73 | + supportsMemo: boolean; |
| 74 | + chainType: string; |
| 75 | + }[] { |
| 76 | + if (!this.response) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + return this.response.data.chains |
| 81 | + .filter((chain) => { |
| 82 | + const isEVMChain = chain.chain_type === "evm"; |
| 83 | + const chainId = isEVMChain |
| 84 | + ? `eip155:${chain.chain_id}` |
| 85 | + : chain.chain_id; |
| 86 | + |
| 87 | + return this.chainStore.hasChain(chainId); |
| 88 | + }) |
| 89 | + .filter((chain) => { |
| 90 | + const isEVMChain = chain.chain_type === "evm"; |
| 91 | + const chainId = isEVMChain |
| 92 | + ? `eip155:${chain.chain_id}` |
| 93 | + : chain.chain_id; |
| 94 | + |
| 95 | + return this.chainStore.isInChainInfosInListUI(chainId); |
| 96 | + }) |
| 97 | + .map((chain) => { |
| 98 | + const isEVMChain = chain.chain_type === "evm"; |
| 99 | + const chainId = isEVMChain |
| 100 | + ? `eip155:${chain.chain_id}` |
| 101 | + : chain.chain_id; |
| 102 | + |
| 103 | + return { |
| 104 | + chainInfo: this.chainStore.getChain(chainId), |
| 105 | + pfmEnabled: chain.pfm_enabled, |
| 106 | + supportsMemo: chain.supports_memo ?? false, |
| 107 | + chainType: chain.chain_type, |
| 108 | + }; |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + isPFMEnabled = computedFn((chainId: string): boolean => { |
| 113 | + const chain = this.chains.find((chain) => { |
| 114 | + return ( |
| 115 | + chain.chainInfo.chainIdentifier === |
| 116 | + ChainIdHelper.parse(chainId).identifier |
| 117 | + ); |
| 118 | + }); |
| 119 | + if (!chain) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + return chain.pfmEnabled; |
| 124 | + }); |
| 125 | + |
| 126 | + isSupportsMemo = computedFn((chainId: string): boolean => { |
| 127 | + const chain = this.chains.find((chain) => { |
| 128 | + return ( |
| 129 | + chain.chainInfo.chainIdentifier === |
| 130 | + ChainIdHelper.parse(chainId).identifier |
| 131 | + ); |
| 132 | + }); |
| 133 | + if (!chain) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + return chain.supportsMemo; |
| 138 | + }); |
| 139 | + |
| 140 | + isChainTypeEVM = computedFn((chainId: string): boolean => { |
| 141 | + const chain = this.chains.find((chain) => { |
| 142 | + return ( |
| 143 | + chain.chainInfo.chainIdentifier === |
| 144 | + ChainIdHelper.parse(chainId).identifier |
| 145 | + ); |
| 146 | + }); |
| 147 | + if (!chain) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + return chain.chainType === "evm"; |
| 152 | + }); |
| 153 | +} |
0 commit comments