jumbo-exchange-sdk 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +67 -0
  2. package/index.js +194 -0
  3. package/package.json +29 -0
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # jumbo-exchange-sdk
2
+
3
+ SDK for interacting with the Jumbo Exchange on NEAR Protocol.
4
+
5
+ ## Installation
6
+
7
+ npm install jumbo-exchange-sdk
8
+
9
+ ## Usage
10
+
11
+ ### Get Configuration
12
+
13
+ import { getConfig } from 'jumbo-exchange-sdk';
14
+
15
+ const config = getConfig({ networkId: 'mainnet' });
16
+
17
+ ### Get Pool Info
18
+
19
+ import { getPool } from 'jumbo-exchange-sdk';
20
+
21
+ const pool = await getPool(42);
22
+ // with config override
23
+ const pool = await getPool(42, { networkId: 'testnet' });
24
+
25
+ ### Get Token Balance
26
+
27
+ import { getTokenBalance } from 'jumbo-exchange-sdk';
28
+
29
+ const balance = await getTokenBalance(
30
+ 'token.near',
31
+ 'myaccount.near'
32
+ );
33
+
34
+ ### Swap Tokens
35
+
36
+ import { swap } from 'jumbo-exchange-sdk';
37
+
38
+ const result = await swap({
39
+ tokenIn: 'token-a.near',
40
+ tokenOut: 'token-b.near',
41
+ amountIn: '1000000',
42
+ minAmountOut: '990000',
43
+ });
44
+
45
+ ### Add Liquidity
46
+
47
+ import { addLiquidity } from 'jumbo-exchange-sdk';
48
+
49
+ const result = await addLiquidity(
50
+ 42,
51
+ ['1000000', '2000000'],
52
+ account
53
+ );
54
+
55
+ ## API
56
+
57
+ | Function | Description |
58
+ |---|---|
59
+ | `getConfig(overrides?)` | Returns full resolved config |
60
+ | `getPool(poolId, config?)` | Fetches pool info by ID |
61
+ | `getTokenBalance(tokenId, accountId, config?)` | Fetches token balance for an account |
62
+ | `swap(params, config?)` | Executes a token swap |
63
+ | `addLiquidity(poolId, amounts, account, config?)` | Adds liquidity to a pool |
64
+
65
+ ## License
66
+
67
+ MIT
package/index.js ADDED
@@ -0,0 +1,194 @@
1
+ import * as nearApiJs from 'near-api-js';
2
+
3
+ /** Configuration for Jumbo Exchange SDK */
4
+ export interface JumboConfig {
5
+ networkId?: string;
6
+ nodeUrl?: string;
7
+ contractId?: string;
8
+ }
9
+
10
+ /** Swap parameters */
11
+ export interface SwapParams {
12
+ tokenIn: string;
13
+ tokenOut: string;
14
+ amountIn: string;
15
+ minAmountOut: string;
16
+ account: nearApiJs.Account;
17
+ }
18
+
19
+ /** Pool info returned from Jumbo */
20
+ export interface PoolInfo {
21
+ id: number;
22
+ tokenIds: string[];
23
+ amounts: string[];
24
+ totalFee: number;
25
+ sharesTotalSupply: string;
26
+ }
27
+
28
+ /** Token balance result */
29
+ export interface TokenBalance {
30
+ token: string;
31
+ balance: string;
32
+ decimals: number;
33
+ }
34
+
35
+ /** Transaction result */
36
+ export interface TxResult {
37
+ transactionHash: string;
38
+ status: string;
39
+ }
40
+
41
+ const DEFAULT_CONTRACT = 'v1.jumbo_exchange.near';
42
+ const DEFAULT_RPC = 'https://rpc.mainnet.near.org';
43
+
44
+ /** Returns default Jumbo config merged with provided overrides */
45
+ export function getConfig(overrides: JumboConfig = {}): Required<JumboConfig> {
46
+ return {
47
+ networkId: overrides.networkId ?? 'mainnet',
48
+ nodeUrl: overrides.nodeUrl ?? DEFAULT_RPC,
49
+ contractId: overrides.contractId ?? DEFAULT_CONTRACT,
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Fetches pool information by pool ID from Jumbo Exchange.
55
+ */
56
+ export async function getPool(
57
+ poolId: number,
58
+ config: JumboConfig = {}
59
+ ): Promise<PoolInfo> {
60
+ const { nodeUrl, contractId } = getConfig(config);
61
+ const args = Buffer.from(JSON.stringify({ pool_id: poolId })).toString('base64');
62
+ const response = await fetch(nodeUrl, {
63
+ method: 'POST',
64
+ headers: { 'Content-Type': 'application/json' },
65
+ body: JSON.stringify({
66
+ jsonrpc: '2.0',
67
+ id: 'jumbo-sdk',
68
+ method: 'query',
69
+ params: {
70
+ request_type: 'call_function',
71
+ finality: 'final',
72
+ account_id: contractId,
73
+ method_name: 'get_pool',
74
+ args_base64: args,
75
+ },
76
+ }),
77
+ });
78
+ const json = await response.json();
79
+ if (json.error) throw new Error(json.error.message ?? 'RPC error');
80
+ const result: PoolInfo = JSON.parse(
81
+ Buffer.from(json.result.result).toString()
82
+ );
83
+ return result;
84
+ }
85
+
86
+ /**
87
+ * Returns the token balance of an account for a given NEP-141 token.
88
+ */
89
+ export async function getTokenBalance(
90
+ tokenId: string,
91
+ accountId: string,
92
+ config: JumboConfig = {}
93
+ ): Promise<TokenBalance> {
94
+ const { nodeUrl } = getConfig(config);
95
+ const args = Buffer.from(JSON.stringify({ account_id: accountId })).toString('base64');
96
+ const response = await fetch(nodeUrl, {
97
+ method: 'POST',
98
+ headers: { 'Content-Type': 'application/json' },
99
+ body: JSON.stringify({
100
+ jsonrpc: '2.0',
101
+ id: 'jumbo-sdk',
102
+ method: 'query',
103
+ params: {
104
+ request_type: 'call_function',
105
+ finality: 'final',
106
+ account_id: tokenId,
107
+ method_name: 'ft_balance_of',
108
+ args_base64: args,
109
+ },
110
+ }),
111
+ });
112
+ const json = await response.json();
113
+ if (json.error) throw new Error(json.error.message ?? 'RPC error');
114
+ const balance: string = JSON.parse(Buffer.from(json.result.result).toString());
115
+
116
+ const metaArgs = Buffer.from('{}').toString('base64');
117
+ const metaResponse = await fetch(nodeUrl, {
118
+ method: 'POST',
119
+ headers: { 'Content-Type': 'application/json' },
120
+ body: JSON.stringify({
121
+ jsonrpc: '2.0',
122
+ id: 'jumbo-sdk-meta',
123
+ method: 'query',
124
+ params: {
125
+ request_type: 'call_function',
126
+ finality: 'final',
127
+ account_id: tokenId,
128
+ method_name: 'ft_metadata',
129
+ args_base64: metaArgs,
130
+ },
131
+ }),
132
+ });
133
+ const metaJson = await metaResponse.json();
134
+ const meta = JSON.parse(Buffer.from(metaJson.result.result).toString());
135
+ return { token: tokenId, balance, decimals: meta.decimals };
136
+ }
137
+
138
+ /**
139
+ * Executes a token swap on Jumbo Exchange using near-api-js Account.
140
+ */
141
+ export async function swap(params: SwapParams, config: JumboConfig = {}): Promise<TxResult> {
142
+ const { contractId } = getConfig(config);
143
+ const { tokenIn, tokenOut, amountIn, minAmountOut, account } = params;
144
+
145
+ const actions = [
146
+ {
147
+ pool_id: 0,
148
+ token_in: tokenIn,
149
+ token_out: tokenOut,
150
+ amount_in: amountIn,
151
+ min_amount_out: minAmountOut,
152
+ },
153
+ ];
154
+
155
+ const outcome = await account.functionCall({
156
+ contractId: tokenIn,
157
+ methodName: 'ft_transfer_call',
158
+ args: {
159
+ receiver_id: contractId,
160
+ amount: amountIn,
161
+ msg: JSON.stringify({ force: 0, actions }),
162
+ },
163
+ gas: '100000000000000',
164
+ attachedDeposit: '1',
165
+ });
166
+
167
+ return {
168
+ transactionHash: outcome.transaction.hash,
169
+ status: Object.keys(outcome.status)[0],
170
+ };
171
+ }
172
+
173
+ /**
174
+ * Adds liquidity to a Jumbo pool using near-api-js Account.
175
+ */
176
+ export async function addLiquidity(
177
+ poolId: number,
178
+ amounts: string[],
179
+ account: nearApiJs.Account,
180
+ config: JumboConfig = {}
181
+ ): Promise<TxResult> {
182
+ const { contractId } = getConfig(config);
183
+ const outcome = await account.functionCall({
184
+ contractId,
185
+ methodName: 'add_liquidity',
186
+ args: { pool_id: poolId, amounts },
187
+ gas: '100000000000000',
188
+ attachedDeposit: '970000000000000000000',
189
+ });
190
+ return {
191
+ transactionHash: outcome.transaction.hash,
192
+ status: Object.keys(outcome.status)[0],
193
+ };
194
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "jumbo-exchange-sdk",
3
+ "version": "1.0.0",
4
+ "description": "npm Package - jumbo-exchange-sdk",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "jest --passWithNoTests"
8
+ },
9
+ "keywords": [
10
+ "near",
11
+ "blockchain",
12
+ "web3"
13
+ ],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "near-api-js": "^4.0.0"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^5.0.0",
20
+ "@types/node": "^20.0.0",
21
+ "jest": "^29.0.0",
22
+ "@types/jest": "^29.0.0",
23
+ "ts-jest": "^29.0.0"
24
+ },
25
+ "files": [
26
+ "dist/**/*",
27
+ "README.md"
28
+ ]
29
+ }