@paradex/sdk 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Paradex SDK
2
+
3
+ ## Install
4
+
5
+ ```sh
6
+ npm install --save @paradex/sdk
7
+ ```
8
+
9
+ ```sh
10
+ yarn add @paradex/sdk
11
+ ```
12
+
13
+ ## Examples
14
+
15
+ - [React app](https://github.com/tradeparadex/paradex-react-example)
16
+
17
+ ## Notes on browser usage
18
+
19
+ To use the Paradex SDK in the browser you will need to polyfill `Buffer` from node and define `process.env.NODE_DEBUG` to satisfy `@starkware-industries/starkware-crypto-utils`.
20
+
21
+ ## Get in touch
22
+
23
+ Have a feature to request or a bug to report?
24
+
25
+ Drop us a message on [Discord].
26
+
27
+ [Discord]: https://discord.gg/paradex
28
+ [get in touch]: #get-in-touch
29
+
30
+ ## Warning
31
+
32
+ ⚠️ `@paradex/sdk` is experimental and APIs are subject to change
@@ -0,0 +1,14 @@
1
+ import type { ParadexConfig } from './config';
2
+ import type { EthereumSigner } from './ethereum-signer';
3
+ interface FromEthSignerParams {
4
+ readonly config: ParadexConfig;
5
+ readonly signer: EthereumSigner;
6
+ }
7
+ /**
8
+ * Generates a Paradex account from an Ethereum wallet.
9
+ * @returns The generated Paradex account.
10
+ */
11
+ export declare function fromEthSigner({ config, signer }: FromEthSignerParams): Promise<{
12
+ address: string;
13
+ }>;
14
+ export {};
@@ -0,0 +1,54 @@
1
+ import { keyDerivation } from '@starkware-industries/starkware-crypto-utils';
2
+ import { CallData, hash } from 'starknet';
3
+ /**
4
+ * Generates a Paradex account from an Ethereum wallet.
5
+ * @returns The generated Paradex account.
6
+ */
7
+ export async function fromEthSigner({ config, signer }) {
8
+ const starkKeyTypedData = buildStarkKeyTypedData(config.l1ChainId);
9
+ const signature = await signer.signTypedData(starkKeyTypedData);
10
+ const privateKey = keyDerivation.getPrivateKeyFromEthSignature(signature);
11
+ const publicKey = keyDerivation.privateToStarkKey(privateKey);
12
+ const address = generateAccountAddress({
13
+ publicKey: `0x${publicKey}`,
14
+ accountClassHash: config.paraclearAccountHash,
15
+ accountProxyClassHash: config.paraclearAccountProxyHash,
16
+ });
17
+ return { address };
18
+ }
19
+ /**
20
+ * Returns the typed data that needs to be signed by an Ethereum
21
+ * wallet in order to generate a Paradex account.
22
+ * @returns The typed data object.
23
+ */
24
+ function buildStarkKeyTypedData(l1ChainId) {
25
+ return {
26
+ domain: {
27
+ name: 'Paradex',
28
+ chainId: l1ChainId,
29
+ version: '1',
30
+ },
31
+ primaryType: 'Constant',
32
+ types: {
33
+ Constant: [{ name: 'action', type: 'string' }],
34
+ },
35
+ message: {
36
+ action: 'STARK Key',
37
+ },
38
+ };
39
+ }
40
+ /**
41
+ * Generates an account address based on the account contract and public key.
42
+ */
43
+ function generateAccountAddress({ accountClassHash, accountProxyClassHash, publicKey, }) {
44
+ const callData = CallData.compile({
45
+ implementation: accountClassHash,
46
+ selector: hash.getSelectorFromName('initialize'),
47
+ calldata: CallData.compile({
48
+ signer: publicKey,
49
+ guardian: '0',
50
+ }),
51
+ });
52
+ const address = hash.calculateContractAddressFromHash(publicKey, accountProxyClassHash, callData, 0);
53
+ return address;
54
+ }
@@ -0,0 +1,34 @@
1
+ interface RawBridgedTokenConfig {
2
+ readonly name: string;
3
+ readonly symbol: string;
4
+ readonly decimals: number;
5
+ readonly l1_token_address: `0x${string}`;
6
+ readonly l1_bridge_address: `0x${string}`;
7
+ readonly l2_token_address: string;
8
+ readonly l2_bridge_address: string;
9
+ }
10
+ export interface RawParadexConfig {
11
+ readonly starknet_gateway_url: string;
12
+ readonly starknet_chain_id: string;
13
+ readonly block_explorer_url: string;
14
+ readonly paraclear_address: string;
15
+ readonly paraclear_decimals: number;
16
+ readonly paraclear_account_proxy_hash: string;
17
+ readonly paraclear_account_hash: string;
18
+ readonly bridged_tokens: ReadonlyArray<RawBridgedTokenConfig>;
19
+ readonly l1_core_contract_address: `0x${string}`;
20
+ readonly l1_operator_address: `0x${string}`;
21
+ readonly l1_chain_id: string;
22
+ }
23
+ export interface ParadexConfig {
24
+ readonly l1ChainId: string;
25
+ readonly paraclearAccountHash: string;
26
+ readonly paraclearAccountProxyHash: string;
27
+ }
28
+ type ParadexEnvironment = 'testnet' | 'prod';
29
+ /**
30
+ * Fetches the Paradex config for the given environment.
31
+ * This is required for account derivation.
32
+ */
33
+ export declare function fetchConfig(environment: ParadexEnvironment): Promise<ParadexConfig>;
34
+ export {};
package/dist/config.js ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Fetches the Paradex config for the given environment.
3
+ * This is required for account derivation.
4
+ */
5
+ export async function fetchConfig(environment) {
6
+ assertParadexEnvironment(environment);
7
+ const apiUrl = getParadexApiUrl(environment);
8
+ const resp = await fetch(`${apiUrl}/system/config`);
9
+ if (!resp.ok) {
10
+ throw new Error(`Failed to fetch Paradex config: ${resp.statusText} ${resp.status}`);
11
+ }
12
+ const jsonResp = await resp.json();
13
+ const config = jsonResp;
14
+ return {
15
+ l1ChainId: config.l1_chain_id,
16
+ paraclearAccountHash: config.paraclear_account_hash,
17
+ paraclearAccountProxyHash: config.paraclear_account_proxy_hash,
18
+ };
19
+ }
20
+ function assertParadexEnvironment(environment) {
21
+ if (environment !== 'testnet' && environment !== 'prod') {
22
+ throw new Error(`Invalid Paradex environment: ${environment}`);
23
+ }
24
+ }
25
+ function getParadexApiUrl(environment) {
26
+ return `https://api.${environment}.paradex.trade/v1`;
27
+ }
@@ -0,0 +1,18 @@
1
+ import type { ethers } from 'ethers';
2
+ interface TypedData {
3
+ readonly domain: {
4
+ readonly name: string;
5
+ readonly version: string;
6
+ readonly chainId: string;
7
+ };
8
+ readonly types: Record<string, Array<{
9
+ readonly name: string;
10
+ readonly type: string;
11
+ }>>;
12
+ readonly message: Record<string, unknown>;
13
+ }
14
+ export interface EthereumSigner {
15
+ signTypedData(typedData: TypedData): Promise<string>;
16
+ }
17
+ export declare function ethersSignerAdapter(ethersSigner: ethers.Signer): EthereumSigner;
18
+ export {};
@@ -0,0 +1,7 @@
1
+ export function ethersSignerAdapter(ethersSigner) {
2
+ return {
3
+ async signTypedData(typedData) {
4
+ return ethersSigner.signTypedData(typedData.domain, typedData.types, typedData.message);
5
+ },
6
+ };
7
+ }
@@ -0,0 +1,12 @@
1
+ import * as _Config from './config';
2
+ import * as _Account from './account';
3
+ import * as _Signer from './ethereum-signer';
4
+ export declare const Config: {
5
+ fetchConfig: typeof _Config.fetchConfig;
6
+ };
7
+ export declare const Account: {
8
+ fromEthSigner: typeof _Account.fromEthSigner;
9
+ };
10
+ export declare const Signer: {
11
+ ethersSignerAdapter: typeof _Signer.ethersSignerAdapter;
12
+ };
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import * as _Config from './config';
2
+ import * as _Account from './account';
3
+ import * as _Signer from './ethereum-signer';
4
+ export const Config = { fetchConfig: _Config.fetchConfig };
5
+ export const Account = { fromEthSigner: _Account.fromEthSigner };
6
+ export const Signer = { ethersSignerAdapter: _Signer.ethersSignerAdapter };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@paradex/sdk",
3
+ "version": "0.1.1",
4
+ "description": "JavaScript SDK for Paradex",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "scripts": {
10
+ "prepare": "husky && yarn build",
11
+ "build": "yarn tsc",
12
+ "lint": "prettier --check .",
13
+ "format": "prettier --write ."
14
+ },
15
+ "lint-staged": {
16
+ "**/*": "prettier --write --ignore-unknown"
17
+ },
18
+ "license": "MIT",
19
+ "private": false,
20
+ "keywords": [
21
+ "paradex"
22
+ ],
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/tradeparadex/paradex.js.git"
26
+ },
27
+ "dependencies": {
28
+ "@starkware-industries/starkware-crypto-utils": "^0.2.1",
29
+ "starknet": "^6.6.6"
30
+ },
31
+ "devDependencies": {
32
+ "@types/elliptic": "^6.4.18",
33
+ "ethers": "^6.11.1",
34
+ "husky": "^9.0.11",
35
+ "lint-staged": "^15.2.2",
36
+ "prettier": "3.2.5",
37
+ "typescript": "^5.4.3"
38
+ }
39
+ }