Skip to content

Commit

Permalink
Merge pull request #493 from enkryptcom/devop/rootstock-changelly-bla…
Browse files Browse the repository at this point in the history
…st-telos-rango-swaps

Add changelly swap to Rootstock, Rango swap to Telos and Blast
  • Loading branch information
kvhnuke committed Sep 9, 2024
2 parents 6582c23 + 49702db commit d849356
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 3 deletions.
37 changes: 37 additions & 0 deletions packages/swap/src/common/supportedNetworks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { SignerType } from "@enkryptcom/types";
import { NetworkInfo, NetworkType, SupportedNetworkName } from "../types";

/**
* Sometimes you can get decimals and token info from here `https://raw.githubusercontent.com/MyEtherWallet/ethereum-lists/master/dist/master-file.json`
*/
const NetworkDetails: Record<SupportedNetworkName, NetworkInfo> = {
[SupportedNetworkName.Bitcoin]: {
id: SupportedNetworkName.Bitcoin,
Expand Down Expand Up @@ -239,6 +242,40 @@ const NetworkDetails: Record<SupportedNetworkName, NetworkInfo> = {
signerType: [SignerType.secp256k1btc],
type: NetworkType.Bitcoin,
},
[SupportedNetworkName.Rootstock]: {
id: SupportedNetworkName.Rootstock,
decimals: 18,
logoURI:
"https://coin-images.coingecko.com/coins/images/5070/large/RBTC-logo.png?1718152038",
name: "Rootstock",
symbol: "RBTC",
cgId: "rootstock",
rank: 19,
signerType: [SignerType.secp256k1],
type: NetworkType.EVM,
},
[SupportedNetworkName.Blast]: {
id: SupportedNetworkName.Blast,
decimals: 18,
logoURI: "https://assets.coingecko.com/coins/images/35494/standard/Blast.jpg",
name: "Blast",
symbol: "ETH",
cgId: "ethereum",
rank: 20,
signerType: [SignerType.secp256k1],
type: NetworkType.EVM,
},
[SupportedNetworkName.Telos]: {
id: SupportedNetworkName.Telos,
decimals: 18,
logoURI: "https://assets.coingecko.com/coins/images/7588/standard/tlos_png.png",
name: "Telos",
symbol: "TLOS",
cgId: "telos",
rank: 21,
signerType: [SignerType.secp256k1],
type: NetworkType.EVM,
},
};
export const isSupportedNetwork = (networkName: SupportedNetworkName) =>
!!NetworkDetails[networkName];
Expand Down
3 changes: 3 additions & 0 deletions packages/swap/src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const TOKEN_LISTS: {
[NetworkNames.ZkSync]: `https://raw.githubusercontent.com/enkryptcom/dynamic-data/main/swaplists/${SupportedNetworkName.Zksync}.json`,
[NetworkNames.Base]: `https://raw.githubusercontent.com/enkryptcom/dynamic-data/main/swaplists/${SupportedNetworkName.Base}.json`,
[NetworkNames.MaticZK]: `https://raw.githubusercontent.com/enkryptcom/dynamic-data/main/swaplists/${SupportedNetworkName.MaticZK}.json`,
[NetworkNames.Rootstock]: `https://raw.githubusercontent.com/enkryptcom/dynamic-data/main/swaplists/${SupportedNetworkName.Rootstock}.json`,
[NetworkNames.Blast]: `https://raw.githubusercontent.com/enkryptcom/dynamic-data/main/swaplists/${SupportedNetworkName.Blast}.json`,
[NetworkNames.Telos]: `https://raw.githubusercontent.com/enkryptcom/dynamic-data/main/swaplists/${SupportedNetworkName.Telos}.json`,
};

const CHANGELLY_LIST =
Expand Down
12 changes: 12 additions & 0 deletions packages/swap/src/providers/changelly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Changelly extends ProviderClass {

toTokens: ProviderToTokenResponse;

/** Dynamically generated Changelly swap token data from the Enkrypt dynamic-lists repo {@link CHANGELLY_LIST} */
changellyList: ChangellyCurrency[];

contractToTicker: Record<string, string>;
Expand All @@ -66,16 +67,25 @@ class Changelly extends ProviderClass {
async init(): Promise<void> {
if (!Changelly.isSupported(this.network)) return;
this.changellyList = await fetch(CHANGELLY_LIST).then((res) => res.json());

/** changelly blockchain name -> enkrypt supported swap network name */
const changellyToNetwork: Record<string, SupportedNetworkName> = {};
// Generate mapping of changelly blockchain -> enkrypt blockchain
Object.keys(supportedNetworks).forEach((net) => {
changellyToNetwork[supportedNetworks[net].changellyName] =
net as unknown as SupportedNetworkName;
});

/** List of changelly blockchain names */
const supportedChangellyNames = Object.values(supportedNetworks).map(
(s) => s.changellyName
);

this.changellyList.forEach((cur) => {
// We must support the changelly network
if (!supportedChangellyNames.includes(cur.blockchain)) return;

// Can currency can be swapped from?
if (
cur.enabledFrom &&
cur.fixRateEnabled &&
Expand All @@ -84,6 +94,8 @@ class Changelly extends ProviderClass {
) {
this.fromTokens[cur.token.address] = cur.token;
}

// Can currency can be swapped to?
if (cur.enabledTo && cur.fixRateEnabled && cur.token) {
if (!this.toTokens[changellyToNetwork[cur.blockchain]])
this.toTokens[changellyToNetwork[cur.blockchain]] = {};
Expand Down
8 changes: 8 additions & 0 deletions packages/swap/src/providers/changelly/supported.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { isPolkadotAddress, isEVMAddress } from "../../utils/common";
import { SupportedNetworkName } from "../../types";

/**
* ```sh
* curl https://partners.mewapi.io/changelly-v2 -X POST -H Accept:application/json -H Content-Type:application/json --data '{"id":"1","jsonrpc":"2.0","method":"getCurrenciesFull","params":{}}'
* ````
*/
const supportedNetworks: {
[key in SupportedNetworkName]?: {
changellyName: string;
Expand Down Expand Up @@ -50,6 +55,9 @@ const supportedNetworks: {
[SupportedNetworkName.Dogecoin]: {
changellyName: "doge",
},
[SupportedNetworkName.Rootstock]: {
changellyName: "rootstock",
},
};

export default supportedNetworks;
39 changes: 37 additions & 2 deletions packages/swap/src/providers/rango/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,25 @@ import { isEVMAddress } from "../../utils/common";

const RANGO_PUBLIC_API_KEY = "ee7da377-0ed8-4d42-aaf9-fa978a32b18d";
const rangoClient = new RangoClient(RANGO_PUBLIC_API_KEY);

/**
* `name` is the blockchain id on Rango
*
* You can use the Rango API to get a list of tokens to figure out the Rango name of a network
*
* @see https://rango-api.readme.io/reference/meta
*
* ```sh
* curl 'https://api.rango.exchange/basic/meta?apiKey=c6381a79-2817-4602-83bf-6a641a409e32' -H 'Accept:application/json'
* ```
*/
const supportedNetworks: {
[key in SupportedNetworkName]?: { chainId: string; name: string };
[key in SupportedNetworkName]?: {
/** Standard base10 chain ID, can be obtained from `https://chainlist.org` */
chainId: string;
/** Rango name (Rango's identifier for the chain) of a network */
name: string;
};
} = {
[SupportedNetworkName.Ethereum]: {
chainId: "1",
Expand Down Expand Up @@ -86,6 +103,14 @@ const supportedNetworks: {
chainId: "1284",
name: "MOONBEAM",
},
[SupportedNetworkName.Blast]: {
chainId: "81457",
name: "BLAST",
},
[SupportedNetworkName.Telos]: {
chainId: "40",
name: "TELOS",
},
};

class Rango extends ProviderClass {
Expand Down Expand Up @@ -142,16 +167,26 @@ class Rango extends ProviderClass {
network: SupportedNetworkName,
blockchains: BlockchainMeta[]
) {
if (!Object.keys(supportedNetworks).includes(network as unknown as string))
// We must support this network
if (!Object.keys(supportedNetworks).includes(network as unknown as string)) {
return false;
}

if (blockchains.length) {
// Join Rango networks and our supported networks by their chain id

// Extract our info about this supported network
const { chainId } = Object.entries(supportedNetworks).find(
(chain) => chain[0] === (network as unknown as string)
)[1];

// Does Rango support this chain id?
return !!blockchains.find(
(chain: BlockchainMeta) => Number(chain.chainId) === Number(chainId)
)?.enabled;
}

// Rango didn't give us anything so just assume Rango supports this network
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/swap/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export enum SupportedNetworkName {
Zksync = NetworkNames.ZkSync,
Base = NetworkNames.Base,
MaticZK = NetworkNames.MaticZK,
Blast = NetworkNames.Blast,
Telos = NetworkNames.Telos,
Rootstock = NetworkNames.Rootstock,
Solana = NetworkNames.Solana,
}

// eslint-disable-next-line no-shadow
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export enum NetworkNames {
RolluxTest = "TRLX",
Rollux = "RLX",
CagaAnkara = "CagaAnkara",
Solana = "SOL",
Telos = "TLOS",
Blast = "blast",
Sanko = "sanko",
Degen = "degen",
Ham = "ham",
Solana = "SOLANA",
XLayer = "XLayer",
ZChains = "ZCD",
ImmutableZkevm = "ImmutableZkevm",
Expand Down

1 comment on commit d849356

@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.