@pioneer-platform/mayachain-client 0.0.10
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/lib/index.d.ts +15 -0
- package/lib/index.js +190 -0
- package/package.json +35 -0
- package/tsconfig.json +13 -0
package/lib/index.d.ts
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
declare const TAG = " | maya | ";
|
2
|
+
declare const uuid: any;
|
3
|
+
declare const log: any;
|
4
|
+
declare let caipToNetworkId: any, shortListSymbolToCaip: any, ChainToNetworkId: any;
|
5
|
+
declare let network: any;
|
6
|
+
declare const createMemo: any, parseMemo: any;
|
7
|
+
declare let networkSupport: any[];
|
8
|
+
declare function nodeRequest(path: any): Promise<any>;
|
9
|
+
interface QuoteResult {
|
10
|
+
amountOutMin: string;
|
11
|
+
amountOut: string;
|
12
|
+
slippage: string;
|
13
|
+
}
|
14
|
+
declare function quoteFromPool(sellAmount: string, assetPoolAmount: string, runePoolAmount: string, maxSlippage: number): QuoteResult;
|
15
|
+
declare const get_quote: (quote: any) => Promise<any>;
|
package/lib/index.js
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
"use strict";
|
2
|
+
/*
|
3
|
+
Maya Swap Integration
|
4
|
+
- Highlander
|
5
|
+
*/
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
13
|
+
});
|
14
|
+
};
|
15
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
16
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
17
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
18
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
19
|
+
function step(op) {
|
20
|
+
if (f) throw new TypeError("Generator is already executing.");
|
21
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
22
|
+
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;
|
23
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
24
|
+
switch (op[0]) {
|
25
|
+
case 0: case 1: t = op; break;
|
26
|
+
case 4: _.label++; return { value: op[1], done: false };
|
27
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
28
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
29
|
+
default:
|
30
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
31
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
32
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
33
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
34
|
+
if (t[2]) _.ops.pop();
|
35
|
+
_.trys.pop(); continue;
|
36
|
+
}
|
37
|
+
op = body.call(thisArg, _);
|
38
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
39
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
40
|
+
}
|
41
|
+
};
|
42
|
+
var TAG = " | maya | ";
|
43
|
+
var uuid = require('uuidv4').uuid;
|
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 network = require("@pioneer-platform/maya-network");
|
47
|
+
var _b = require('@pioneer-platform/pioneer-coins'), createMemo = _b.createMemo, parseMemo = _b.parseMemo;
|
48
|
+
var networkSupport = [
|
49
|
+
ChainToNetworkId["BTC"],
|
50
|
+
ChainToNetworkId["CACAO"],
|
51
|
+
ChainToNetworkId["ETH"],
|
52
|
+
ChainToNetworkId["THOR"],
|
53
|
+
ChainToNetworkId["DASH"],
|
54
|
+
];
|
55
|
+
// Function to make a request to the node
|
56
|
+
function nodeRequest(path) {
|
57
|
+
return __awaiter(this, void 0, void 0, function () {
|
58
|
+
var response, data, error_1;
|
59
|
+
return __generator(this, function (_a) {
|
60
|
+
switch (_a.label) {
|
61
|
+
case 0:
|
62
|
+
_a.trys.push([0, 3, , 4]);
|
63
|
+
return [4 /*yield*/, fetch("https://mayanode.mayachain.info/mayachain".concat(path))];
|
64
|
+
case 1:
|
65
|
+
response = _a.sent();
|
66
|
+
return [4 /*yield*/, response.json()];
|
67
|
+
case 2:
|
68
|
+
data = _a.sent();
|
69
|
+
return [2 /*return*/, data];
|
70
|
+
case 3:
|
71
|
+
error_1 = _a.sent();
|
72
|
+
console.error('Error fetching from node:', error_1);
|
73
|
+
throw error_1;
|
74
|
+
case 4: return [2 /*return*/];
|
75
|
+
}
|
76
|
+
});
|
77
|
+
});
|
78
|
+
}
|
79
|
+
module.exports = {
|
80
|
+
init: function (settings) {
|
81
|
+
return true;
|
82
|
+
},
|
83
|
+
networkSupport: function () {
|
84
|
+
return networkSupport;
|
85
|
+
},
|
86
|
+
getQuote: function (quote) {
|
87
|
+
return get_quote(quote);
|
88
|
+
},
|
89
|
+
};
|
90
|
+
function quoteFromPool(sellAmount, assetPoolAmount, runePoolAmount, maxSlippage) {
|
91
|
+
// Convert string inputs to numbers and scale the sell amount
|
92
|
+
var swapAmount = parseFloat(sellAmount) * 1e8; // Assuming 1e6 is the scaling factor for Maya
|
93
|
+
var assetDepth = parseFloat(assetPoolAmount);
|
94
|
+
var runeDepth = parseFloat(runePoolAmount);
|
95
|
+
// Calculate the constant product
|
96
|
+
var k = assetDepth * runeDepth;
|
97
|
+
// New amount of the asset in the pool after the swap
|
98
|
+
var newAssetDepth = assetDepth + swapAmount;
|
99
|
+
// Calculate the amount of Rune received (or the other asset in the pool)
|
100
|
+
var newRuneDepth = k / newAssetDepth;
|
101
|
+
var runeReceived = runeDepth - newRuneDepth;
|
102
|
+
// Scale back down the amount of Rune received
|
103
|
+
var scaledRuneReceived = runeReceived / 1e6; // Adjust as per Rune's scaling factor
|
104
|
+
// Calculate the actual rate of the swap
|
105
|
+
var actualRate = scaledRuneReceived / (swapAmount / 1e6);
|
106
|
+
// Calculate the ideal rate
|
107
|
+
var idealRate = runeDepth / assetDepth;
|
108
|
+
// Calculate the slippage
|
109
|
+
var slippage = ((idealRate - actualRate) / idealRate) * 100;
|
110
|
+
// Calculate amountOutMin considering the maximum slippage
|
111
|
+
var amountOutMin = scaledRuneReceived * (1 - maxSlippage / 100);
|
112
|
+
return {
|
113
|
+
amountOutMin: amountOutMin.toFixed(6).toString(),
|
114
|
+
amountOut: scaledRuneReceived.toFixed(6),
|
115
|
+
slippage: Math.max(slippage, 0).toFixed(6)
|
116
|
+
};
|
117
|
+
}
|
118
|
+
var get_quote = function (quote) {
|
119
|
+
return __awaiter(this, void 0, void 0, function () {
|
120
|
+
var tag, output, pools, memoInput, memo, tx, e_1;
|
121
|
+
return __generator(this, function (_a) {
|
122
|
+
switch (_a.label) {
|
123
|
+
case 0:
|
124
|
+
tag = TAG + " | get_quote | ";
|
125
|
+
_a.label = 1;
|
126
|
+
case 1:
|
127
|
+
_a.trys.push([1, 3, , 4]);
|
128
|
+
output = {};
|
129
|
+
if (!quote.sellAsset)
|
130
|
+
throw new Error("missing sellAsset");
|
131
|
+
if (!quote.buyAsset)
|
132
|
+
throw new Error("missing buyAsset");
|
133
|
+
if (!quote.sellAmount)
|
134
|
+
throw new Error("missing sellAmount");
|
135
|
+
if (!quote.senderAddress)
|
136
|
+
throw new Error("missing senderAddress");
|
137
|
+
if (!quote.recipientAddress)
|
138
|
+
throw new Error("missing recipientAddress");
|
139
|
+
if (!quote.slippage)
|
140
|
+
throw new Error("missing slippage");
|
141
|
+
return [4 /*yield*/, network.getPools()];
|
142
|
+
case 2:
|
143
|
+
pools = _a.sent();
|
144
|
+
if (!pools)
|
145
|
+
throw Error("Unable to get pools from network!");
|
146
|
+
log.info(tag, "pools: ", pools);
|
147
|
+
output.meta = {
|
148
|
+
quoteMode: "MAYA-OUT"
|
149
|
+
};
|
150
|
+
output.steps = 1;
|
151
|
+
output.complete = true;
|
152
|
+
memoInput = {
|
153
|
+
type: 'SWAP',
|
154
|
+
asset: quote.buyAsset,
|
155
|
+
destAddr: quote.recipientAddress,
|
156
|
+
lim: null,
|
157
|
+
interval: null,
|
158
|
+
quantity: null,
|
159
|
+
affiliate: null,
|
160
|
+
fee: null,
|
161
|
+
dexAggregatorAddr: null,
|
162
|
+
finalAssetAddr: null,
|
163
|
+
minAmountOut: null
|
164
|
+
};
|
165
|
+
memo = createMemo(memoInput);
|
166
|
+
log.info(tag, "memo: ", memo);
|
167
|
+
tx = {
|
168
|
+
type: "transfer",
|
169
|
+
chain: quote.sellAsset.split(".")[0],
|
170
|
+
txParams: {
|
171
|
+
senderAddress: quote.senderAddress,
|
172
|
+
recipientAddress: quote.recipientAddress,
|
173
|
+
amount: quote.sellAmount,
|
174
|
+
token: quote.sellAsset.split(".")[1],
|
175
|
+
memo: memo
|
176
|
+
}
|
177
|
+
};
|
178
|
+
output.txs = [
|
179
|
+
tx
|
180
|
+
];
|
181
|
+
return [2 /*return*/, output];
|
182
|
+
case 3:
|
183
|
+
e_1 = _a.sent();
|
184
|
+
console.error(tag, "e: ", e_1);
|
185
|
+
throw e_1;
|
186
|
+
case 4: return [2 /*return*/];
|
187
|
+
}
|
188
|
+
});
|
189
|
+
});
|
190
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
{
|
2
|
+
"name": "@pioneer-platform/mayachain-client",
|
3
|
+
"version": "0.0.10",
|
4
|
+
"main": "./lib/index.js",
|
5
|
+
"types": "./lib/index.d.ts",
|
6
|
+
"dependencies": {
|
7
|
+
"@pioneer-platform/cosmos-network": "^8.3.2",
|
8
|
+
"@pioneer-platform/loggerdog": "^8.3.1",
|
9
|
+
"@pioneer-platform/osmosis-network": "^8.3.8",
|
10
|
+
"@pioneer-platform/pioneer-caip": "^9.2.13",
|
11
|
+
"@pioneer-platform/pioneer-coins": "^9.2.4",
|
12
|
+
"axios": "^1.3.4",
|
13
|
+
"dotenv": "^8.2.0",
|
14
|
+
"rango-sdk": "^0.1.45",
|
15
|
+
"uuidv4": "^6.2.13"
|
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
|
+
"devDependencies": {
|
25
|
+
"@types/jest": "^25.2.3",
|
26
|
+
"@types/node": "^13.13.21",
|
27
|
+
"@types/source-map-support": "^0.5.3",
|
28
|
+
"jest": "^26.4.2",
|
29
|
+
"onchange": "^7.0.2",
|
30
|
+
"serve": "^11.3.2",
|
31
|
+
"ts-jest": "^29.0.5",
|
32
|
+
"typescript": "^5.0.2"
|
33
|
+
},
|
34
|
+
"gitHead": "a76012f6693a12181c4744e53e977a9eaeef0ed3"
|
35
|
+
}
|
package/tsconfig.json
ADDED