Skip to content
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
wants to merge 23 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
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 Nov 19, 2024
9698321
add wip signing handlers
stevenkhong Nov 21, 2024
e58dba0
Set up file structure of polynetwork folder
sarah-thong Nov 22, 2024
930413c
Migrate required utils
sarah-thong Nov 23, 2024
2172a31
Add ETHClient (wip)
sarah-thong Nov 23, 2024
28a4255
Add all other polynetwork helper clients
sarah-thong Nov 24, 2024
e7db96c
Resolve missing dependencies in bridge helper classes
sarah-thong Nov 24, 2024
2880592
Add more helper classes for Neo Legacy and Neo3 clients
sarah-thong Nov 24, 2024
fb7aef3
Resolve missing dependencies
sarah-thong Nov 24, 2024
280558d
Rename clients folder to helpers
sarah-thong Nov 24, 2024
e14865b
Create PolynetworkClient class
sarah-thong Nov 24, 2024
0789471
Merge branch 'staging' into feat/bridge-helpers
sarah-thong Dec 5, 2024
1879306
Resolve code conflict
sarah-thong Dec 5, 2024
c3ddd55
Remove unnecessary ledger dependencies
sarah-thong Dec 5, 2024
b90b392
Remove unnecessary ledger packages
sarah-thong Dec 5, 2024
19642a6
Fix build issues
sarah-thong Dec 11, 2024
e045375
Remove unused helper functions
sarah-thong Dec 20, 2024
7972465
Delete unnecessary json files and data fields
sarah-thong Dec 20, 2024
6585c0c
Create initialize function to initialize helper data
sarah-thong Dec 20, 2024
40db44b
Polish polynetwork client initialisation
sarah-thong Dec 20, 2024
c2ddd35
Merge branch 'staging' into feat/bridge-helpers
sarah-thong Dec 23, 2024
b793533
Fix build errors caused by staging conflicts
sarah-thong Dec 23, 2024
c12b336
Merge branch 'staging' into feat/bridge-helpers
stevenkhong Jan 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ yarn-error.log
*.tsbuildinfo
dist
dist-*

.env
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"packageManager": "yarn@1.22.19",
"dependencies": {
"@changesets/cli": "^2.27.9"
"@changesets/cli": "^2.27.9",
"turbo": "2.3.0"
}
}
13 changes: 12 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
"module": "./dist-es/index.js",
"types": "./dist-types/index.d.ts",
"dependencies": {
"@demex-sdk/codecs": "0.0.3",
"@cityofzion/neon-core-next": "npm:@cityofzion/neon-core@^5.0.0",
"@cosmjs/stargate": "^0.32.4",
"@cosmjs/tendermint-rpc": "^0.32.4",
"@demex-sdk/codecs": "0.0.3",
"@improbable-eng/grpc-web": "^0.15.0",
"@improbable-eng/grpc-web-node-http-transport": "^0.15.0",
"base58check": "^2.0.0",
"bignumber.js": "^9.1.2",
"bip32": "^2.0.6",
"bip39": "^3.0.4",
"ethers": "^6.13.4",
"secp256k1": "^4.0.2",
"secp256r1": "^0.0.3",
Copy link
Contributor

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.

"tslib": "^2.6.2"
},
"exports": {
Expand All @@ -39,6 +47,9 @@
"dist-*/**"
],
"devDependencies": {
"@types/crypto-js": "4.2.0",
"@types/secp256k1": "^4.0.2",
"@types/wif": "^2.0.2",
"concurrently": "7.0.0",
"eslint": "^9.13.0"
}
Expand Down
5 changes: 0 additions & 5 deletions packages/core/src/client/client.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/client/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/core/src/env/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Bech32AddrType = "main" | "validator" | "consensus";
33 changes: 33 additions & 0 deletions packages/core/src/env/chain.ts
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,
};
5 changes: 5 additions & 0 deletions packages/core/src/env/crypto.ts
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;
4 changes: 4 additions & 0 deletions packages/core/src/env/index.ts
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";
63 changes: 63 additions & 0 deletions packages/core/src/env/network.ts
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",
},
};
1 change: 1 addition & 0 deletions packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./token";
118 changes: 118 additions & 0 deletions packages/core/src/helpers/token.ts
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)
};
};
4 changes: 3 additions & 1 deletion packages/core/src/index.ts
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";
10 changes: 5 additions & 5 deletions packages/core/src/query/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AdlQueryClient, AllianceQueryClient, AuthQueryClient, AuthzQueryClient,
import { BlockchainClient } from "./chain";
import { GrpcQueryClient } from "./grpc";

export interface CarbonQueryClientOpts {
export interface DemexQueryClientOpts {
tmClient: Tendermint37Client;
grpcClient?: GrpcQueryClient;
}
Expand Down Expand Up @@ -80,12 +80,12 @@ const extendQueryClient = (tmClient: Tendermint37Client, rpcClient: Rpc) => Quer
},
}))

type CarbonQueryClient = ReturnType<typeof extendQueryClient>;
namespace CarbonQueryClient {
export const instance = (opts: CarbonQueryClientOpts) => {
type DemexQueryClient = ReturnType<typeof extendQueryClient>;
namespace DemexQueryClient {
export const instance = (opts: DemexQueryClientOpts) => {
const rpcClient = opts.grpcClient ?? createProtobufRpcClient(new QueryClient(opts.tmClient));
return extendQueryClient(opts.tmClient, rpcClient);
}
}

export { CarbonQueryClient };
export { DemexQueryClient };
Loading