-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: @demex-sdk/polynetwork #12
Open
sarah-thong
wants to merge
23
commits into
staging
Choose a base branch
from
feat/bridge-helpers
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c33f0f3
initialise wallet package with basic connection helpers
stevenkhong 9698321
add wip signing handlers
stevenkhong e58dba0
Set up file structure of polynetwork folder
sarah-thong 930413c
Migrate required utils
sarah-thong 2172a31
Add ETHClient (wip)
sarah-thong 28a4255
Add all other polynetwork helper clients
sarah-thong e7db96c
Resolve missing dependencies in bridge helper classes
sarah-thong 2880592
Add more helper classes for Neo Legacy and Neo3 clients
sarah-thong fb7aef3
Resolve missing dependencies
sarah-thong 280558d
Rename clients folder to helpers
sarah-thong e14865b
Create PolynetworkClient class
sarah-thong 0789471
Merge branch 'staging' into feat/bridge-helpers
sarah-thong 1879306
Resolve code conflict
sarah-thong c3ddd55
Remove unnecessary ledger dependencies
sarah-thong b90b392
Remove unnecessary ledger packages
sarah-thong 19642a6
Fix build issues
sarah-thong e045375
Remove unused helper functions
sarah-thong 7972465
Delete unnecessary json files and data fields
sarah-thong 6585c0c
Create initialize function to initialize helper data
sarah-thong 40db44b
Polish polynetwork client initialisation
sarah-thong c2ddd35
Merge branch 'staging' into feat/bridge-helpers
sarah-thong b793533
Fix build errors caused by staging conflicts
sarah-thong c12b336
Merge branch 'staging' into feat/bridge-helpers
stevenkhong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ yarn-error.log | |
*.tsbuildinfo | ||
dist | ||
dist-* | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Bech32AddrType = "main" | "validator" | "consensus"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Carbon } from "@demex-sdk/codecs"; | ||
|
||
export interface PolyNetworkBridge extends Carbon.Coin.Bridge { | ||
isEvmChain: boolean; | ||
}; | ||
|
||
export interface IbcBridge extends Carbon.Coin.Bridge { | ||
chain_id_name: string; | ||
channels: { | ||
src_channel: string; | ||
dst_channel: string; | ||
port_id: string; // for cosmwasm bridges | ||
} | ||
}; | ||
|
||
export interface AxelarBridge extends Carbon.Coin.Bridge { | ||
chain_id_name: string; | ||
bridgeAddress: string; | ||
}; | ||
|
||
export interface BridgeMap { | ||
polynetwork: PolyNetworkBridge[] | ||
ibc: IbcBridge[] | ||
axelar: AxelarBridge[] | ||
}; | ||
|
||
export type BlockchainV2 = ReturnType<TokenClient['getAllBlockchainNames']>[number] | "Native" | "Carbon" | "Tradehub" | "Ibc" | "Polynetwork"; | ||
|
||
export const BRIDGE_IDS = { | ||
polynetwork: 1, | ||
ibc: 2, | ||
axelar: 3, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const BIP44_PURPOSE = 44; | ||
export const NEO_COIN_TYPE = 0x00000378; | ||
export const ETH_COIN_TYPE = 0x0000003c; | ||
|
||
export const SWTH_COIN_TYPE = 118; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./address"; | ||
export * from "./chain"; | ||
export * from "./crypto"; | ||
export * from "./network"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export enum Network { | ||
MainNet = "mainnet", | ||
TestNet = "testnet", | ||
DevNet = "devnet", | ||
Local = "local", | ||
} | ||
|
||
export interface NetworkConfig { | ||
network: Network | ||
chainId: string | ||
|
||
tmRpcUrl: string | ||
restUrl: string | ||
grpcUrl: string | ||
|
||
bech32Prefix: string; | ||
}; | ||
|
||
export interface NetworkConfigProvider { | ||
getConfig(): NetworkConfig; | ||
}; | ||
|
||
export const defaultNetworkConfig: Record<Network, NetworkConfig> = { | ||
[Network.MainNet]: { | ||
network: Network.MainNet, | ||
chainId: "carbon-1", | ||
|
||
tmRpcUrl: "https://tm-api.carbon.network", | ||
restUrl: "https://api.carbon.network", | ||
grpcUrl: "grpc.carbon.network", | ||
|
||
bech32Prefix: "swth", | ||
}, | ||
[Network.TestNet]: { | ||
network: Network.TestNet, | ||
chainId: "carbon-1", | ||
|
||
tmRpcUrl: "https://test-tm-api.carbon.network", | ||
restUrl: "https://test-api.carbon.network", | ||
grpcUrl: "test-grpc.carbon.network", | ||
|
||
bech32Prefix: "tswth", | ||
}, | ||
[Network.DevNet]: { | ||
network: Network.DevNet, | ||
chainId: "carbon-1", | ||
|
||
tmRpcUrl: "https://dev-tm-api.carbon.network", | ||
restUrl: "https://dev-api.carbon.network", | ||
grpcUrl: "dev-grpc.carbon.network", | ||
|
||
bech32Prefix: "swth", | ||
}, | ||
[Network.Local]: { | ||
network: Network.Local, | ||
chainId: "carbon-1", | ||
tmRpcUrl: "http://localhost:26657", | ||
restUrl: "http://localhost:1317", | ||
grpcUrl: "localhost:9090", | ||
|
||
bech32Prefix: "tswth", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./token"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { Carbon } from "@demex-sdk/codecs"; | ||
import Long from "long"; | ||
import { DemexQueryClient, Network, NetworkConfigProvider, OptionalNetworkMap } from "@demex-sdk/core"; | ||
import { AxelarBridge, BlockchainV2, BRIDGE_IDS, BridgeMap, IbcBridge, ibcTokenRegex, regexLPDenom, regexCdpDenom } from "../../env"; | ||
import { SimpleMap } from "../../util"; | ||
|
||
export const TokenBlacklist: OptionalNetworkMap<string[]> = { | ||
[Network.MainNet]: [ | ||
"brkl.1.2.337f55", // wrong token address | ||
"zusdt.1.18.1cbca1", // duplicated token | ||
], | ||
}; | ||
|
||
// This class is just created as a stand-in class for polynetwork bridge helpers (i.e. packages/polynetwork/src/clients/...), this is not final | ||
// Pls make any changes if required | ||
export class TokenClient { | ||
public readonly tokens: SimpleMap<Carbon.Coin.Token> = {}; | ||
public readonly bridges: BridgeMap = { polynetwork: [], ibc: [], axelar: [] }; | ||
|
||
private constructor(public readonly query: DemexQueryClient, public readonly configProvider: NetworkConfigProvider) { } | ||
|
||
public static instance(query: DemexQueryClient, configProvider: NetworkConfigProvider) { | ||
return new TokenClient(query, configProvider); | ||
} | ||
|
||
public async getAllTokens(): Promise<Carbon.Coin.Token[]> { | ||
const result = await this.query.coin.TokenAll({ | ||
pagination: { | ||
limit: new Long(10000), | ||
offset: Long.UZERO, | ||
key: new Uint8Array(), | ||
countTotal: false, | ||
reverse: false, | ||
}, | ||
}); | ||
|
||
const networkConfig = this.configProvider.getConfig(); | ||
const tokenBlacklist = TokenBlacklist[networkConfig.network] ?? []; | ||
return result.tokens.filter((token) => !tokenBlacklist.includes(token.denom)); | ||
}; | ||
|
||
public getBlockchainV2(denom: string | undefined): BlockchainV2 | undefined { | ||
if (!denom) return undefined | ||
const token = this.tokens[denom] | ||
if (this.isNativeToken(denom) || this.isNativeStablecoin(denom) || TokenClient.isPoolToken(denom) || TokenClient.isCdpToken(denom) || this.isGroupedToken(denom)) { | ||
// native denoms "swth" and "usc" should be native. | ||
// pool and cdp tokens are on the Native blockchain, hence 0 | ||
return 'Native' | ||
} | ||
|
||
if (this.isBridgedToken(denom)) { | ||
// brdg tokens will all be chain_id 0 which will also be deprecated in future | ||
// hence for brdg tokens cannot use chain_id to differentiate between blockchains | ||
const bridgeList = this.bridges.axelar | ||
const chainName = bridgeList.find((bridge) => bridge.bridgeAddress === token.bridgeAddress)?.chainName | ||
return chainName | ||
} | ||
const bridge = this.getBridgeFromToken(token) | ||
return bridge?.chainName; | ||
}; | ||
|
||
public getBridgesFromBridgeId(bridgeId: number): Carbon.Coin.Bridge[] | IbcBridge[] | AxelarBridge[] { | ||
switch (bridgeId) { | ||
case BRIDGE_IDS.polynetwork: | ||
return this.bridges.polynetwork | ||
case BRIDGE_IDS.ibc: | ||
return this.bridges.ibc | ||
case BRIDGE_IDS.axelar: | ||
return this.bridges.axelar | ||
default: | ||
return this.bridges.polynetwork | ||
} | ||
}; | ||
|
||
public getBridgeFromToken(token: Carbon.Coin.Token | null): Carbon.Coin.Bridge | IbcBridge | undefined { | ||
if (!token || !token.bridgeId) return undefined | ||
const bridgeList = this.getBridgesFromBridgeId(token.bridgeId.toNumber()) | ||
return bridgeList.find(bridge => token.chainId.equals(bridge.chainId)) | ||
}; | ||
|
||
public isNativeToken(denom: string): boolean { | ||
return denom === "swth"; | ||
}; | ||
|
||
public isNativeStablecoin(denom: string): boolean { | ||
return denom === "usc"; | ||
}; | ||
|
||
public static isPoolToken(denom: string): boolean { | ||
return this.isPoolTokenNew(denom) || this.isPoolTokenLegacy(denom); | ||
}; | ||
|
||
public static isPoolTokenNew(denom: string): boolean { | ||
return denom.match(regexLPDenom) !== null; | ||
}; | ||
|
||
public static isPoolTokenLegacy(denom: string): boolean { | ||
return denom.match(/^([a-z\d.-]+)-(\d+)-([a-z\d.-]+)-(\d+)-lp\d+$/i) !== null; | ||
}; | ||
|
||
public static isCdpToken(denom: string): boolean { | ||
return denom.match(regexCdpDenom) !== null; | ||
}; | ||
|
||
public static isIBCDenom(denom: string): boolean { | ||
return denom.match(ibcTokenRegex) !== null; | ||
}; | ||
|
||
public isBridgedToken(denom: string): boolean { | ||
const bridgedTokenRegex = new RegExp(/^brdg\//) | ||
return bridgedTokenRegex.test(denom) | ||
}; | ||
|
||
public isGroupedToken(denom: string): boolean { | ||
const groupedTokenRegex = new RegExp(/^cgt\/\d+$/) | ||
return groupedTokenRegex.test(denom) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from "./client"; | ||
export * from "./env"; | ||
export * from "./helpers"; | ||
export * from "./query"; | ||
export * from "./util"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think many of the functionalities from these packages can be replaced with existing packages like
@cosmjs/tendermint-rpc
. we should avoid adding packages carelessly to guard against bloating our package.