Skip to content

Commit

Permalink
Merge pull request #497 from enkryptcom/devop/add-xlayer-network
Browse files Browse the repository at this point in the history
Add XLayer network
  • Loading branch information
kvhnuke committed Aug 19, 2024
2 parents d2b5901 + e6534af commit 08a7920
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/extension/src/libs/market-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MarketData {
const fiat = await this.getFiatValue(fiatSymbol);
if (market && fiat) {
return balanceBN
.multipliedBy(market.current_price)
.multipliedBy(market.current_price ?? 0)
.multipliedBy(fiat.exchange_rate)
.toFixed(2);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/libs/market-data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface CoinGeckoTokenMarket {
symbol: string;
name: string;
image: string;
current_price: number;
current_price: null | number;
market_cap: number;
market_cap_rank: number;
high_24h: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class BitcoinNetwork extends BaseNetwork {
}
const userBalance = fromBase(balance, this.decimals);
const usdBalance = new BigNumber(userBalance).times(
marketData.length ? marketData[0]!.current_price : 0
marketData[0]?.current_price ?? 0
);
const nativeAsset: AssetsType = {
balance: balance,
Expand All @@ -135,10 +135,9 @@ export class BitcoinNetwork extends BaseNetwork {
icon: this.icon,
name: this.name_long,
symbol: this.currencyName,
value: marketData.length ? marketData[0]!.current_price.toString() : "0",
valuef: formatFiatValue(
marketData.length ? marketData[0]!.current_price.toString() : "0"
).value,
value: marketData[0]?.current_price?.toString() ?? "0",
valuef: formatFiatValue(marketData[0]?.current_price?.toString() ?? "0")
.value,
contract: "",
decimals: this.decimals,
sparkline: marketData.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const NetworkEndpoints: Record<string, string> = {
[NetworkNames.ImmutableZkevm]: "https://explorer.immutable.com/",
[NetworkNames.Rari]: "https://mainnet.explorer.rarichain.org/",
[NetworkNames.Forma]: "https://explorer.forma.art/",
[NetworkNames.XLayer]: "https://www.oklink.com/api/v5/explorer/xlayer/",
[NetworkNames.Linea]: "https://api.lineascan.build/",
[NetworkNames.MantaPacific]: "https://pacific-explorer.manta.network/",
[NetworkNames.Mode]: "https://explorer.mode.network/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import {
EthereumRawInfo,
} from "@/types/activity";
import { BaseNetwork } from "@/types/base-network";
import { NetworkNames } from "@enkryptcom/types";
import { numberToHex } from "web3-utils";
import { decodeTx } from "../../../transaction/decoder";
import { NetworkEndpoints } from "./configs";
import { EtherscanTxType } from "./types";
const TTL = 30000;
const getAddressActivity = async (
address: string,
endpoint: string
endpoint: string,
headers?: Record<string, string>
): Promise<EthereumRawInfo[]> => {
return cacheFetch(
{
// Note: would like to add offset=50 (i.e. results per page) but it seems to cause polygon API to hang
url: `${endpoint}api?module=account&action=txlist&address=${address}&sort=desc`,
headers,
},
TTL
).then((res) => {
Expand Down Expand Up @@ -52,9 +55,16 @@ export default async (
address: string
): Promise<Activity[]> => {
address = address.toLowerCase();
let headers: undefined | Record<string, string>;
if (network.name === NetworkNames.XLayer) {
// api console: https://www.oklink.com/account/my-api
// api header spec: https://www.oklink.com/docs/en/#quickstart-guide-api-authentication
// api docs: https://www.oklink.com/docs/en/#evm-rpc-data-address-get-normal-transactions-by-address
headers = { "OK-ACCESS-KEY": "df87e7eb-061f-44b1-84bc-83722fad717c" };
}
const enpoint =
NetworkEndpoints[network.name as keyof typeof NetworkEndpoints];
const activities = await getAddressActivity(address, enpoint);
const activities = await getAddressActivity(address, enpoint, headers);
const Promises = activities.map((activity) => {
return decodeTx(activity, network as EvmNetwork).then((txData) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ const supportedNetworks: Record<SupportedNetworkNames, SupportedNetwork> = {
tbName: "degen",
cgPlatform: CoingeckoPlatform.Degen,
},
[NetworkNames.XLayer]: {
tbName: "xlayer",
cgPlatform: CoingeckoPlatform.XLayer,
},
[NetworkNames.ProofOfPlayApex]: {
tbName: "apex",
cgPlatform: undefined,
Expand Down Expand Up @@ -278,9 +282,8 @@ export default (
balances[address].balance,
tokenInfo[address].decimals
);
const usdBalance = new BigNumber(userBalance).times(
market.current_price
);
const currentPrice = market.current_price ?? 0;
const usdBalance = new BigNumber(userBalance).times(currentPrice);
const asset: AssetsType = {
balance: toBN(balances[address].balance).toString(),
balancef: formatFloatingPointValue(userBalance).value,
Expand All @@ -289,8 +292,8 @@ export default (
icon: market.image,
name: market.name,
symbol: market.symbol,
value: market.current_price.toString(),
valuef: formatFiatValue(market.current_price.toString()).value,
value: currentPrice.toString(),
valuef: formatFiatValue(currentPrice.toString()).value,
contract: address,
decimals: tokenInfo[address].decimals,
sparkline: new Sparkline(market.sparkline_in_7d.price, 25).dataValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const TokenList: Record<SupportedNetworkNames, string> = {
[NetworkNames.Blast]: `https://tokens.coingecko.com/${CoingeckoPlatform.Blast}/all.json`,
[NetworkNames.Sanko]: `https://tokens.coingecko.com/${CoingeckoPlatform.Sanko}/all.json`,
[NetworkNames.Degen]: `https://tokens.coingecko.com/${CoingeckoPlatform.Degen}/all.json`,
[NetworkNames.XLayer]: `https://tokens.coingecko.com/${CoingeckoPlatform.XLayer}/all.json`,
[NetworkNames.ProofOfPlayApex]: `https://raw.githubusercontent.com/enkryptcom/dynamic-data/main/tokenlists/apex.json`,
[NetworkNames.Godwoken]: `https://tokens.coingecko.com/${CoingeckoPlatform.Godwoken}/all.json`,
[NetworkNames.Linea]: `https://tokens.coingecko.com/${CoingeckoPlatform.Linea}/all.json`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type SupportedNetworkNames =
| NetworkNames.Sanko
| NetworkNames.Degen
| NetworkNames.Blast
| NetworkNames.XLayer
| NetworkNames.ProofOfPlayApex
| NetworkNames.Godwoken
| NetworkNames.Linea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const decodeTx = async (
.getMarketInfoByContracts([tx.to!], network.coingeckoPlatform!)
.then((marketInfo) => {
if (marketInfo[tx.to!]) {
currentPriceUSD = marketInfo[tx.to!]!.current_price;
currentPriceUSD = marketInfo[tx.to!]!.current_price ?? 0;
CGToken = marketInfo[tx.to!]!.id;
}
});
Expand All @@ -76,7 +76,7 @@ const decodeTx = async (
}
if (CGToken === network.coingeckoID && network.coingeckoID) {
await marketData.getMarketData([CGToken!]).then((marketInfo) => {
currentPriceUSD = marketInfo[0]!.current_price;
currentPriceUSD = marketInfo[0]!.current_price ?? 0;
});
}
return {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/extension/src/providers/ethereum/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import zcdNode from "./zcd";
import immutableZkevmNode from "./immutable-zkevm";
import rariNode from "./rari";
import formaNode from "./forma";
import xlayerNode from "./xlayer";
import godwokenNode from "./godwoken";
import lineaNode from "./linea";
import mantaPacificNode from "./manta-pacific";
Expand Down Expand Up @@ -117,6 +118,7 @@ export default {
immutableZkevm: immutableZkevmNode,
rari: rariNode,
forma: formaNode,
xlayer: xlayerNode,
godwoken: godwokenNode,
linea: lineaNode,
mantaPacific: mantaPacificNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ async function getPreconfiguredTokens(
.value,
balanceUSD: nativeAssetUsdBalance.toNumber(),
balanceUSDf: formatFiatValue(nativeAssetUsdBalance.toString()).value,
value: nativeAssetMarketData[index]?.current_price.toString() ?? "0",
value: nativeAssetMarketData[index]?.current_price?.toString() ?? "0",
valuef: formatFiatValue(
nativeAssetMarketData[index]?.current_price.toString() ?? "0"
nativeAssetMarketData[index]?.current_price?.toString() ?? "0"
).value,
decimals: assetDecimals,
sparkline: nativeAssetMarketData[index]
Expand Down
28 changes: 28 additions & 0 deletions packages/extension/src/providers/ethereum/networks/xlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NetworkNames, CoingeckoPlatform } from "@enkryptcom/types";
import { EvmNetwork, EvmNetworkOptions } from "../types/evm-network";
import wrapActivityHandler from "@/libs/activity-state/wrap-activity-handler";
import { EtherscanActivity } from "../libs/activity-handlers";
import assetsInfoHandler from "@/providers/ethereum/libs/assets-handlers/assetinfo-mew";

const xlayerOptions: EvmNetworkOptions = {
name: NetworkNames.XLayer,
name_long: "X Layer",
homePage: "https://www.okx.com/xlayer",
blockExplorerTX: "https://www.okx.com/web3/explorer/xlayer/tx/[[txHash]]",
blockExplorerAddr:
"https://www.okx.com/web3/explorer/xlayer/address/[[address]]",
chainID: "0xc4",
isTestNetwork: false,
currencyName: "OKB",
currencyNameLong: "OKB",
node: "wss://ws.xlayer.tech",
icon: require("./icons/xlayer.png"),
coingeckoID: "okb",
coingeckoPlatform: CoingeckoPlatform.XLayer,
assetsInfoHandler,
activityHandler: wrapActivityHandler(EtherscanActivity),
};

const xlayer = new EvmNetwork(xlayerOptions);

export default xlayer;
10 changes: 5 additions & 5 deletions packages/extension/src/providers/ethereum/types/evm-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ export class EvmNetwork extends BaseNetwork {
.value,
balanceUSD: nativeUsdBalance.toNumber(),
balanceUSDf: formatFiatValue(nativeUsdBalance.toString()).value,
value: nativeMarketData?.current_price.toString() ?? "0",
value: nativeMarketData?.current_price?.toString() ?? "0",
valuef: formatFiatValue(
nativeMarketData?.current_price.toString() ?? "0"
nativeMarketData?.current_price?.toString() ?? "0"
).value,
decimals: this.decimals,
sparkline: nativeMarketData
Expand Down Expand Up @@ -265,12 +265,12 @@ export class EvmNetwork extends BaseNetwork {
if (marketInfo) {
const usdBalance = new BigNumber(
fromBase(token.balance ?? "0", token.decimals)
).times(marketInfo.current_price);
).times(marketInfo.current_price ?? 0);
asset.balanceUSD = usdBalance.toNumber();
asset.balanceUSDf = formatFiatValue(usdBalance.toString()).value;
asset.value = marketInfo.current_price.toString();
asset.value = marketInfo.current_price?.toString() ?? "0";
asset.valuef = formatFiatValue(
marketInfo.current_price.toString()
marketInfo.current_price?.toString() ?? "0"
).value;
asset.sparkline = new Sparkline(
marketInfo.sparkline_in_7d.price,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class KadenaNetwork extends BaseNetwork {

const userBalance = fromBase(balance, this.decimals);
const usdBalance = new BigNumber(userBalance).times(
marketData.length ? marketData[0]!.current_price : 0
marketData[0]?.current_price ?? 0
);

const nativeAsset: AssetsType = {
Expand All @@ -119,10 +119,9 @@ export class KadenaNetwork extends BaseNetwork {
icon: this.icon,
name: this.name_long,
symbol: this.currencyName,
value: marketData.length ? marketData[0]!.current_price.toString() : "0",
valuef: formatFiatValue(
marketData.length ? marketData[0]!.current_price.toString() : "0"
).value,
value: marketData[0]?.current_price?.toString() ?? "0",
valuef: formatFiatValue(marketData[0]?.current_price?.toString() ?? "0")
.value,
contract: "",
decimals: this.decimals,
sparkline: marketData.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ export class SubstrateNetwork extends BaseNetwork {
sparkline: market[idx]
? new Sparkline(market[idx]?.sparkline_in_7d.price, 25).dataValues
: "",
value: market[idx]?.current_price.toString() || "0",
value: market[idx]?.current_price?.toString() || "0",
valuef: formatFloatingPointValue(
market[idx]?.current_price.toString() || "0"
market[idx]?.current_price?.toString() || "0"
).value,
baseToken: st,
};
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export enum NetworkNames {
Sanko = "sanko",
Degen = "degen",
Ham = "ham",
XLayer = "XLayer",
ZChains = "ZCD",
ImmutableZkevm = "ImmutableZkevm",
Forma = "Forma",
Expand Down Expand Up @@ -137,6 +138,7 @@ export enum CoingeckoPlatform {
Blast = "blast",
Sanko = "sanko",
Degen = "degen",
XLayer = "x-layer",
ImmutableZkevm = "immutable",
Godwoken = "godwoken",
Linea = "linea",
Expand Down

1 comment on commit 08a7920

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.