@pioneer-platform/osmosis-client 0.0.15 → 0.1.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # @pioneer-platform/osmosis-client
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Release: minor version bump for all packages
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @pioneer-platform/cosmos-network@8.4.0
13
+ - @pioneer-platform/osmosis-network@8.4.0
14
+ - @pioneer-platform/loggerdog@8.4.0
15
+ - @pioneer-platform/pioneer-coins@9.4.0
16
+ - @pioneer-platform/pioneer-caip@9.3.0
package/lib/index.js CHANGED
@@ -1,56 +1,19 @@
1
- "use strict";
2
1
  /*
3
2
  Osmosis Swap Intergration
4
3
  -Highlander
5
4
 
6
5
  */
7
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
- return new (P || (P = Promise))(function (resolve, reject) {
10
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
- step((generator = generator.apply(thisArg, _arguments || [])).next());
14
- });
15
- };
16
- var __generator = (this && this.__generator) || function (thisArg, body) {
17
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
18
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
19
- function verb(n) { return function (v) { return step([n, v]); }; }
20
- function step(op) {
21
- if (f) throw new TypeError("Generator is already executing.");
22
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
23
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
24
- if (y = 0, t) op = [op[0] & 2, t.value];
25
- switch (op[0]) {
26
- case 0: case 1: t = op; break;
27
- case 4: _.label++; return { value: op[1], done: false };
28
- case 5: _.label++; y = op[1]; op = [0]; continue;
29
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
30
- default:
31
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
32
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
33
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
34
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
35
- if (t[2]) _.ops.pop();
36
- _.trys.pop(); continue;
37
- }
38
- op = body.call(thisArg, _);
39
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
40
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
41
- }
42
- };
43
- var TAG = " | osmosis | ";
44
- var log = require('@pioneer-platform/loggerdog')();
45
- var _a = require("@pioneer-platform/pioneer-caip"), caipToNetworkId = _a.caipToNetworkId, shortListSymbolToCaip = _a.shortListSymbolToCaip, ChainToNetworkId = _a.ChainToNetworkId;
46
- var networkOsmo = require("@pioneer-platform/osmosis-network");
47
- var networkAtom = require("@pioneer-platform/cosmos-network");
48
- var uuid = require('uuidv4').uuid;
49
- var networkSupport = [
6
+ const TAG = " | osmosis | ";
7
+ const log = require('@pioneer-platform/loggerdog')();
8
+ let { caipToNetworkId, shortListSymbolToCaip, ChainToNetworkId } = require("@pioneer-platform/pioneer-caip");
9
+ let networkOsmo = require("@pioneer-platform/osmosis-network");
10
+ let networkAtom = require("@pioneer-platform/cosmos-network");
11
+ const { uuid } = require('uuidv4');
12
+ let networkSupport = [
50
13
  ChainToNetworkId["OSMO"],
51
14
  ChainToNetworkId["GAIA"],
52
15
  ];
53
- var assetSupport = [
16
+ let assetSupport = [
54
17
  shortListSymbolToCaip["OSMO"],
55
18
  shortListSymbolToCaip["GAIA"],
56
19
  ];
@@ -70,145 +33,143 @@ module.exports = {
70
33
  };
71
34
  function quoteFromPool(amountAtomSwap, amountAtomPool, amountOsmoPool, maxSlippage) {
72
35
  // Convert string inputs to numbers and scale the swap amount
73
- var swapAmount = parseFloat(amountAtomSwap) * 1e6;
74
- var atomPoolAmount = parseFloat(amountAtomPool);
75
- var osmoPoolAmount = parseFloat(amountOsmoPool);
36
+ const swapAmount = parseFloat(amountAtomSwap) * 1e6;
37
+ const atomPoolAmount = parseFloat(amountAtomPool);
38
+ const osmoPoolAmount = parseFloat(amountOsmoPool);
76
39
  // Calculate the constant product
77
- var k = atomPoolAmount * osmoPoolAmount;
40
+ const k = atomPoolAmount * osmoPoolAmount;
78
41
  // New amount of ATOM in the pool after the swap
79
- var newAtomPoolAmount = atomPoolAmount + swapAmount;
42
+ const newAtomPoolAmount = atomPoolAmount + swapAmount;
80
43
  // Calculate the amount of OSMO received
81
- var newOsmoPoolAmount = k / newAtomPoolAmount;
82
- var osmoReceived = osmoPoolAmount - newOsmoPoolAmount;
44
+ const newOsmoPoolAmount = k / newAtomPoolAmount;
45
+ const osmoReceived = osmoPoolAmount - newOsmoPoolAmount;
83
46
  // Scale back down the amount of OSMO received
84
- var scaledOsmoReceived = osmoReceived / 1e6;
47
+ const scaledOsmoReceived = osmoReceived / 1e6;
85
48
  // Calculate the actual rate of the swap
86
- var actualRate = scaledOsmoReceived / (swapAmount / 1e6);
49
+ const actualRate = scaledOsmoReceived / (swapAmount / 1e6);
87
50
  // Calculate the ideal rate
88
- var idealRate = osmoPoolAmount / atomPoolAmount;
51
+ const idealRate = osmoPoolAmount / atomPoolAmount;
89
52
  // Calculate the slippage
90
- var slippage = ((idealRate - actualRate) / idealRate) * 100;
53
+ const slippage = ((idealRate - actualRate) / idealRate) * 100;
91
54
  // Calculate amountOutMin considering the maximum slippage
92
- var amountOutMin = scaledOsmoReceived * (1 - maxSlippage / 100);
55
+ const amountOutMin = scaledOsmoReceived * (1 - maxSlippage / 100);
93
56
  return {
94
57
  amountOutMin: amountOutMin.toFixed(6).toString(),
95
58
  amountOut: scaledOsmoReceived.toFixed(6),
96
59
  slippage: Math.max(slippage, 0).toFixed(6)
97
60
  };
98
61
  }
99
- var get_quote = function (quote) {
100
- return __awaiter(this, void 0, void 0, function () {
101
- var tag, output, pools, amountAtom, amountOsmo, rate, invocationId, result, tx1, tx2, tx1, tx2, e_1;
102
- return __generator(this, function (_a) {
103
- switch (_a.label) {
104
- case 0:
105
- tag = TAG + " | get_quote | ";
106
- _a.label = 1;
107
- case 1:
108
- _a.trys.push([1, 3, , 4]);
109
- output = {};
110
- if (!quote.sellAsset)
111
- throw new Error("missing sellAsset");
112
- if (!quote.buyAsset)
113
- throw new Error("missing buyAsset");
114
- if (!quote.sellAmount)
115
- throw new Error("missing sellAmount");
116
- if (!quote.senderAddress)
117
- throw new Error("missing senderAddress");
118
- if (!quote.recipientAddress)
119
- throw new Error("missing recipientAddress");
120
- if (!quote.slippage)
121
- throw new Error("missing slippage");
122
- return [4 /*yield*/, networkOsmo.getPools()];
123
- case 2:
124
- pools = _a.sent();
125
- if (!pools.pools[0])
126
- throw Error("Unable to get pools from network!");
127
- log.info(tag, "resp: ", pools.pools[0].pool_assets);
128
- amountAtom = pools.pools[0].pool_assets[0].token.amount;
129
- amountOsmo = pools.pools[0].pool_assets[1].token.amount;
130
- rate = amountOsmo / amountAtom;
131
- output.rate = rate;
132
- log.info(tag, "rate: ", rate);
133
- invocationId = uuid();
134
- output.invocationId = invocationId;
135
- output.meta = {
136
- quoteMode: "OSMOSIS-IBC"
137
- };
138
- // get amountOut
139
- //2 steps
140
- output.steps = 2;
141
- output.complete = true;
142
- result = void 0;
143
- if (quote.sellAsset === shortListSymbolToCaip["OSMO"]) {
144
- result = quoteFromPool(quote.sellAmount, amountAtom, amountOsmo, quote.slippage);
145
- if (!result || !result.amountOut)
146
- throw new Error("Unable to get result from quoteFromPool");
147
- output.amountOut = result.amountOut;
148
- output.result = result;
149
- tx1 = {
150
- type: "sendSwapTx",
151
- chain: caipToNetworkId(shortListSymbolToCaip["OSMO"]),
152
- txParams: {
153
- senderAddress: quote.senderAddress,
154
- tokenIn: 'uosmo',
155
- tokenOut: 'ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2',
156
- amountIn: quote.sellAmount,
157
- amountOutMin: result.amountOutMin,
158
- }
159
- };
160
- log.info(tag, "tx1: ", tx1);
161
- tx2 = {
162
- type: "ibcTransfer",
163
- chain: caipToNetworkId(shortListSymbolToCaip["ATOM"]), // Assuming ATOM is the target chain for the IBC withdrawal
164
- txParams: {
165
- senderAddress: quote.senderAddress, // Address initiating the IBC withdrawal
166
- recipientAddress: quote.recipientAddress, // Destination address for the ATOM tokens
167
- amount: result.amountOut, // Amount of ATOM tokens to withdraw
168
- token: 'uatom' // Other parameters required for the IBC withdrawal can be added here
169
- }
170
- };
171
- log.info(tag, "tx2: ", tx2);
172
- output.txs = [tx1, tx2];
173
- }
174
- else if (quote.sellAsset === shortListSymbolToCaip["ATOM"]) {
175
- result = quoteFromPool(quote.sellAmount, amountAtom, amountOsmo, quote.slippage);
176
- output.result = result;
177
- tx1 = {
178
- type: "ibcTransfer",
179
- chain: shortListSymbolToCaip["ATOM"],
180
- txParams: {
181
- senderAddress: quote.senderAddress, // Address initiating the IBC deposit
182
- recipientAddress: quote.recipientAddress, // Osmosis address to receive ATOM
183
- amount: quote.sellAmount, // Amount of ATOM tokens to deposit
184
- // Other parameters for the IBC deposit can be added here
185
- }
186
- };
187
- log.info(tag, "tx1: ", tx1);
188
- tx2 = {
189
- type: "sendSwapTx",
190
- chain: shortListSymbolToCaip["OSMO"],
191
- txParams: {
192
- senderAddress: quote.senderAddress,
193
- sellAsset: shortListSymbolToCaip["ATOM"],
194
- buyAsset: quote.buyAsset,
195
- sellAmount: result.amountOut, // Amount of ATOM obtained from the IBC deposit
196
- buyAmountMin: quote.sellAmount // Minimum amount of OSMO to buy
197
- }
198
- };
199
- log.info(tag, "tx2: ", tx2);
200
- output.txs = [tx1, tx2];
201
- }
202
- else {
203
- throw Error("Asset not supported! asset:" + quote.sellAsset);
204
- }
205
- return [2 /*return*/, output];
206
- case 3:
207
- e_1 = _a.sent();
208
- console.error(tag, "e: ", e_1);
209
- return [3 /*break*/, 4];
210
- case 4: return [2 /*return*/];
211
- }
212
- });
213
- });
62
+ const get_quote = async function (quote) {
63
+ let tag = TAG + " | get_quote | ";
64
+ try {
65
+ let output = {};
66
+ if (!quote.sellAsset)
67
+ throw new Error("missing sellAsset");
68
+ if (!quote.buyAsset)
69
+ throw new Error("missing buyAsset");
70
+ if (!quote.sellAmount)
71
+ throw new Error("missing sellAmount");
72
+ if (!quote.senderAddress)
73
+ throw new Error("missing senderAddress");
74
+ if (!quote.recipientAddress)
75
+ throw new Error("missing recipientAddress");
76
+ if (!quote.slippage)
77
+ throw new Error("missing slippage");
78
+ //get pools
79
+ let pools = await networkOsmo.getPools();
80
+ if (!pools.pools[0])
81
+ throw Error("Unable to get pools from network!");
82
+ log.info(tag, "resp: ", pools.pools[0].pool_assets);
83
+ let amountAtom = pools.pools[0].pool_assets[0].token.amount; // ATOM amount
84
+ let amountOsmo = pools.pools[0].pool_assets[1].token.amount; // OSMO amount
85
+ let rate = amountOsmo / amountAtom;
86
+ output.rate = rate;
87
+ log.info(tag, "rate: ", rate);
88
+ //issue invocationId
89
+ let invocationId = uuid();
90
+ output.invocationId = invocationId;
91
+ output.source = 'osmosis';
92
+ output.meta = {
93
+ quoteMode: "OSMOSIS-IBC"
94
+ };
95
+ // get amountOut
96
+ //2 steps
97
+ output.steps = 2;
98
+ output.complete = true;
99
+ let result;
100
+ if (quote.sellAsset === shortListSymbolToCaip["OSMO"]) {
101
+ result = quoteFromPool(quote.sellAmount, amountAtom, amountOsmo, quote.slippage);
102
+ if (!result || !result.amountOut)
103
+ throw new Error("Unable to get result from quoteFromPool");
104
+ output.amountOut = result.amountOut;
105
+ output.result = result;
106
+ //build first TX
107
+ //swap osmo for Atom IBC
108
+ let tx1 = {
109
+ type: "sendSwapTx",
110
+ chain: caipToNetworkId(shortListSymbolToCaip["OSMO"]),
111
+ txParams: {
112
+ senderAddress: quote.senderAddress,
113
+ tokenIn: 'uosmo',
114
+ tokenOut: 'ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2',
115
+ amountIn: quote.sellAmount,
116
+ amountOutMin: result.amountOutMin,
117
+ }
118
+ };
119
+ log.info(tag, "tx1: ", tx1);
120
+ //get balance ibc?
121
+ //IBC withdrawal from osmo:atom to atom
122
+ let tx2 = {
123
+ type: "ibcTransfer",
124
+ chain: caipToNetworkId(shortListSymbolToCaip["ATOM"]), // Assuming ATOM is the target chain for the IBC withdrawal
125
+ txParams: {
126
+ senderAddress: quote.senderAddress, // Address initiating the IBC withdrawal
127
+ recipientAddress: quote.recipientAddress, // Destination address for the ATOM tokens
128
+ amount: result.amountOut, // Amount of ATOM tokens to withdraw
129
+ token: 'uatom' // Other parameters required for the IBC withdrawal can be added here
130
+ }
131
+ };
132
+ log.info(tag, "tx2: ", tx2);
133
+ output.txs = [tx1, tx2];
134
+ }
135
+ else if (quote.sellAsset === shortListSymbolToCaip["ATOM"]) {
136
+ result = quoteFromPool(quote.sellAmount, amountAtom, amountOsmo, quote.slippage);
137
+ output.result = result;
138
+ //TODO audit this
139
+ //deposit atom to osmo:atom IBC
140
+ // Deposit ATOM to osmo:atom IBC
141
+ let tx1 = {
142
+ type: "ibcTransfer",
143
+ chain: shortListSymbolToCaip["ATOM"],
144
+ txParams: {
145
+ senderAddress: quote.senderAddress, // Address initiating the IBC deposit
146
+ recipientAddress: quote.recipientAddress, // Osmosis address to receive ATOM
147
+ amount: quote.sellAmount, // Amount of ATOM tokens to deposit
148
+ // Other parameters for the IBC deposit can be added here
149
+ }
150
+ };
151
+ log.info(tag, "tx1: ", tx1);
152
+ // Swap ATOM for OSMO
153
+ let tx2 = {
154
+ type: "sendSwapTx",
155
+ chain: shortListSymbolToCaip["OSMO"],
156
+ txParams: {
157
+ senderAddress: quote.senderAddress,
158
+ sellAsset: shortListSymbolToCaip["ATOM"],
159
+ buyAsset: quote.buyAsset,
160
+ sellAmount: result.amountOut, // Amount of ATOM obtained from the IBC deposit
161
+ buyAmountMin: quote.sellAmount // Minimum amount of OSMO to buy
162
+ }
163
+ };
164
+ log.info(tag, "tx2: ", tx2);
165
+ output.txs = [tx1, tx2];
166
+ }
167
+ else {
168
+ throw Error("Asset not supported! asset:" + quote.sellAsset);
169
+ }
170
+ return output;
171
+ }
172
+ catch (e) {
173
+ console.error(tag, "e: ", e);
174
+ }
214
175
  };
package/package.json CHANGED
@@ -1,35 +1,35 @@
1
1
  {
2
2
  "name": "@pioneer-platform/osmosis-client",
3
- "version": "0.0.15",
3
+ "version": "0.1.0",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "dependencies": {
7
- "@pioneer-platform/cosmos-network": "^8.3.5",
8
- "@pioneer-platform/loggerdog": "^8.3.1",
9
- "@pioneer-platform/osmosis-network": "^8.3.11",
10
- "@pioneer-platform/pioneer-caip": "^9.2.23",
11
- "@pioneer-platform/pioneer-coins": "^9.2.13",
12
- "axios": "^1.3.4",
7
+ "@pioneer-platform/cosmos-network": "^8.4.0",
8
+ "@pioneer-platform/loggerdog": "^8.4.0",
9
+ "@pioneer-platform/osmosis-network": "^8.4.0",
10
+ "@pioneer-platform/pioneer-caip": "^9.3.0",
11
+ "@pioneer-platform/pioneer-coins": "^9.4.0",
12
+ "axios": "^1.6.0",
13
13
  "dotenv": "^8.2.0",
14
14
  "rango-sdk": "^0.1.45",
15
15
  "uuidv4": "^6.2.13"
16
16
  },
17
- "scripts": {
18
- "npm": "npm i",
19
- "test": "npm run build && node __tests__/test-module.js",
20
- "build": "tsc -p .",
21
- "prepublish": "npm run build",
22
- "refresh": "rm -rf ./node_modules ./package-lock.json && npm install"
23
- },
24
17
  "devDependencies": {
25
18
  "@types/jest": "^25.2.3",
26
- "@types/node": "^13.13.21",
19
+ "@types/node": "^18.16.0",
27
20
  "@types/source-map-support": "^0.5.3",
28
21
  "jest": "^26.4.2",
29
22
  "onchange": "^7.0.2",
30
23
  "serve": "^11.3.2",
31
24
  "ts-jest": "^29.0.5",
32
- "typescript": "^5.0.2"
25
+ "typescript": "^5.0.4"
33
26
  },
34
- "gitHead": "a76012f6693a12181c4744e53e977a9eaeef0ed3"
35
- }
27
+ "gitHead": "a76012f6693a12181c4744e53e977a9eaeef0ed3",
28
+ "scripts": {
29
+ "npm": "pnpm i",
30
+ "test": "pnpm run build && node __tests__/test-module.js",
31
+ "build": "tsc -p .",
32
+ "prepublish": "pnpm run build",
33
+ "refresh": "rm -rf ./node_modules ./package-lock.json && pnpm install"
34
+ }
35
+ }
package/tsconfig.json CHANGED
@@ -1,13 +1,24 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es5",
3
+ "target": "es2020",
4
4
  "module": "commonjs",
5
- "lib": ["es6", "es2015", "dom"],
5
+ "lib": ["es2020", "dom"],
6
6
  "declaration": true,
7
- "outDir": "lib",
8
- "rootDir": "src",
9
- "strict": true,
10
- "types": ["node"],
11
- "esModuleInterop": true
12
- }
7
+ "outDir": "./lib",
8
+ "rootDir": "./src",
9
+ "strict": false,
10
+ "noImplicitAny": false,
11
+ "strictNullChecks": false,
12
+ "strictFunctionTypes": false,
13
+ "strictBindCallApply": false,
14
+ "strictPropertyInitialization": false,
15
+ "noImplicitThis": false,
16
+ "alwaysStrict": false,
17
+ "esModuleInterop": true,
18
+ "resolveJsonModule": true,
19
+ "skipLibCheck": true,
20
+ "forceConsistentCasingInFileNames": true
21
+ },
22
+ "include": ["src"],
23
+ "exclude": ["node_modules", "**/__tests__/*"]
13
24
  }