@pioneer-platform/mayachain-client 0.2.0 → 0.4.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/.turbo/turbo-build.log +1 -0
- package/CHANGELOG.md +28 -0
- package/build.sh +22 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +324 -0
- package/lib/index.js.map +850 -0
- package/package.json +5 -5
@@ -0,0 +1 @@
|
|
1
|
+
$ tsc -p .
|
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,33 @@
|
|
1
1
|
# @pioneer-platform/mayachain-client
|
2
2
|
|
3
|
+
## 0.4.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- Automated minor version bump for all packages
|
8
|
+
|
9
|
+
### Patch Changes
|
10
|
+
|
11
|
+
- Updated dependencies
|
12
|
+
- @pioneer-platform/maya-network@8.7.0
|
13
|
+
- @pioneer-platform/loggerdog@8.7.0
|
14
|
+
- @pioneer-platform/pioneer-coins@9.7.0
|
15
|
+
- @pioneer-platform/pioneer-caip@9.6.0
|
16
|
+
|
17
|
+
## 0.3.0
|
18
|
+
|
19
|
+
### Minor Changes
|
20
|
+
|
21
|
+
- Automated minor version bump for all packages
|
22
|
+
|
23
|
+
### Patch Changes
|
24
|
+
|
25
|
+
- Updated dependencies
|
26
|
+
- @pioneer-platform/maya-network@8.6.0
|
27
|
+
- @pioneer-platform/loggerdog@8.6.0
|
28
|
+
- @pioneer-platform/pioneer-coins@9.6.0
|
29
|
+
- @pioneer-platform/pioneer-caip@9.5.0
|
30
|
+
|
3
31
|
## 0.2.0
|
4
32
|
|
5
33
|
### Minor Changes
|
package/build.sh
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
# Fast Bun build (replaces slow tsc)
|
3
|
+
|
4
|
+
OUTDIR="${1:-lib}"
|
5
|
+
ENTRY="${2:-src/index.ts}"
|
6
|
+
|
7
|
+
# Clean output
|
8
|
+
rm -rf "$OUTDIR"
|
9
|
+
mkdir -p "$OUTDIR"
|
10
|
+
|
11
|
+
# Build with Bun (10x faster than tsc)
|
12
|
+
bun build "$ENTRY" \
|
13
|
+
--outdir "$OUTDIR" \
|
14
|
+
--target node \
|
15
|
+
--format cjs \
|
16
|
+
--sourcemap \
|
17
|
+
--minify
|
18
|
+
|
19
|
+
# Generate TypeScript declarations
|
20
|
+
bunx tsc --emitDeclarationOnly --outDir "$OUTDIR" --project .
|
21
|
+
|
22
|
+
echo "✅ Built $OUTDIR"
|
package/lib/index.d.ts
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
declare const TAG = " | maya | ";
|
2
|
+
type BaseDecimal = string | number;
|
3
|
+
declare const uuid: any;
|
4
|
+
declare const log: any;
|
5
|
+
declare let caipToNetworkId: any, shortListSymbolToCaip: any, ChainToNetworkId: any;
|
6
|
+
declare let network: any;
|
7
|
+
declare const createMemo: any, parseMemo: any;
|
8
|
+
declare const axios: any;
|
9
|
+
declare let Web3: any;
|
10
|
+
declare let service: string | undefined;
|
11
|
+
declare let web3: any;
|
12
|
+
declare let networkSupport: any[];
|
13
|
+
declare let assetSupport: any[];
|
14
|
+
declare function nodeRequest(path: any): Promise<any>;
|
15
|
+
declare const get_quote: (quote: any) => Promise<any>;
|
package/lib/index.js
ADDED
@@ -0,0 +1,324 @@
|
|
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
17
|
+
return g.next = verb(0), g["throw"] = verb(1), g["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 axios = require('axios');
|
49
|
+
var Web3 = require('web3');
|
50
|
+
var service = process.env['PARITY_ARCHIVE_NODE'];
|
51
|
+
if (!service)
|
52
|
+
throw Error("Missing PARITY_ARCHIVE_NODE in .env");
|
53
|
+
var web3 = new Web3(service);
|
54
|
+
var networkSupport = [
|
55
|
+
ChainToNetworkId["BTC"],
|
56
|
+
ChainToNetworkId["MAYA"],
|
57
|
+
ChainToNetworkId["ETH"],
|
58
|
+
// ChainToNetworkId["THOR"],
|
59
|
+
ChainToNetworkId["DASH"],
|
60
|
+
ChainToNetworkId["ARB"],
|
61
|
+
];
|
62
|
+
var assetSupport = [
|
63
|
+
shortListSymbolToCaip["BTC"],
|
64
|
+
shortListSymbolToCaip["ETH"],
|
65
|
+
shortListSymbolToCaip["MAYA"],
|
66
|
+
// shortListSymbolToCaip["THOR"], Eats money, need to handle msgDeposit?
|
67
|
+
shortListSymbolToCaip["DASH"],
|
68
|
+
shortListSymbolToCaip["ARB"],
|
69
|
+
];
|
70
|
+
// Function to make a request to the node
|
71
|
+
function nodeRequest(path) {
|
72
|
+
return __awaiter(this, void 0, void 0, function () {
|
73
|
+
var response, data, error_1;
|
74
|
+
return __generator(this, function (_a) {
|
75
|
+
switch (_a.label) {
|
76
|
+
case 0:
|
77
|
+
_a.trys.push([0, 3, , 4]);
|
78
|
+
return [4 /*yield*/, fetch("https://mayanode.mayachain.info/mayachain".concat(path))];
|
79
|
+
case 1:
|
80
|
+
response = _a.sent();
|
81
|
+
return [4 /*yield*/, response.json()];
|
82
|
+
case 2:
|
83
|
+
data = _a.sent();
|
84
|
+
return [2 /*return*/, data];
|
85
|
+
case 3:
|
86
|
+
error_1 = _a.sent();
|
87
|
+
console.error('Error fetching from node:', error_1);
|
88
|
+
throw error_1;
|
89
|
+
case 4: return [2 /*return*/];
|
90
|
+
}
|
91
|
+
});
|
92
|
+
});
|
93
|
+
}
|
94
|
+
module.exports = {
|
95
|
+
init: function (settings) {
|
96
|
+
return true;
|
97
|
+
},
|
98
|
+
networkSupport: function () {
|
99
|
+
return networkSupport;
|
100
|
+
},
|
101
|
+
assetSupport: function () {
|
102
|
+
return assetSupport;
|
103
|
+
},
|
104
|
+
getQuote: function (quote) {
|
105
|
+
return get_quote(quote);
|
106
|
+
},
|
107
|
+
};
|
108
|
+
var get_quote = function (quote) {
|
109
|
+
return __awaiter(this, void 0, void 0, function () {
|
110
|
+
var tag, output, pools, BaseDecimal, asset, DECIMALS, BASE_UNIT, sellAmountInBaseUnits, quoteFromNode, URL_1, amountOutEstimated, memoInput, memo, type, chain, tx, inbounds, inbound, tokenAddress, depositAbi, routerContract, quoteSellAmountInEther, numberBN, numberHex, data, gasLimit, block, baseFeePerGas, gasPrice, maxPriorityFeePerGas, maxFeePerGas, feeData, e_1;
|
111
|
+
return __generator(this, function (_a) {
|
112
|
+
switch (_a.label) {
|
113
|
+
case 0:
|
114
|
+
tag = TAG + " | get_quote | ";
|
115
|
+
_a.label = 1;
|
116
|
+
case 1:
|
117
|
+
_a.trys.push([1, 10, , 11]);
|
118
|
+
output = {};
|
119
|
+
if (!quote.sellAsset)
|
120
|
+
throw new Error("missing sellAsset");
|
121
|
+
if (!quote.buyAsset)
|
122
|
+
throw new Error("missing buyAsset");
|
123
|
+
if (!quote.sellAmount)
|
124
|
+
throw new Error("missing sellAmount");
|
125
|
+
if (!quote.senderAddress)
|
126
|
+
throw new Error("missing senderAddress");
|
127
|
+
if (!quote.recipientAddress)
|
128
|
+
throw new Error("missing recipientAddress");
|
129
|
+
if (!quote.slippage)
|
130
|
+
throw new Error("missing slippage");
|
131
|
+
return [4 /*yield*/, network.getPools()];
|
132
|
+
case 2:
|
133
|
+
pools = _a.sent();
|
134
|
+
if (!pools)
|
135
|
+
throw Error("Unable to get pools from network!");
|
136
|
+
// log.info(tag, "pools: ", pools)
|
137
|
+
output.meta = {
|
138
|
+
quoteMode: "MAYA_SUPPORTED_TO_MAYA_SUPPORTED"
|
139
|
+
};
|
140
|
+
output.source = 'mayachain';
|
141
|
+
output.steps = 1;
|
142
|
+
output.complete = true;
|
143
|
+
output.id = uuid();
|
144
|
+
BaseDecimal = {
|
145
|
+
ARB: 18,
|
146
|
+
AVAX: 18,
|
147
|
+
BCH: 8,
|
148
|
+
BNB: 8,
|
149
|
+
BSC: 18,
|
150
|
+
BTC: 8,
|
151
|
+
DASH: 8,
|
152
|
+
DGB: 8,
|
153
|
+
DOGE: 8,
|
154
|
+
ETH: 18,
|
155
|
+
BASE: 18,
|
156
|
+
EOS: 6,
|
157
|
+
GAIA: 6,
|
158
|
+
KUJI: 6,
|
159
|
+
LTC: 8,
|
160
|
+
MATIC: 18,
|
161
|
+
MAYA: 10,
|
162
|
+
OP: 18,
|
163
|
+
OSMO: 6,
|
164
|
+
XRP: 6,
|
165
|
+
THOR: 8,
|
166
|
+
ZEC: 8
|
167
|
+
};
|
168
|
+
asset = quote.sellAsset.split(".")[0];
|
169
|
+
if (!asset)
|
170
|
+
throw Error("unable to pasre asset from quote.sellAsset");
|
171
|
+
DECIMALS = BaseDecimal[asset];
|
172
|
+
if (!DECIMALS)
|
173
|
+
throw Error("unable to get DECIMALS for asset: " + asset);
|
174
|
+
BASE_UNIT = Math.pow(10, DECIMALS);
|
175
|
+
sellAmountInBaseUnits = parseFloat(quote.sellAmount) * BASE_UNIT;
|
176
|
+
quoteFromNode = void 0;
|
177
|
+
URL_1 = "/quote/swap?from_asset=".concat(quote.sellAsset, "&to_asset=").concat(quote.buyAsset, "&amount=").concat(sellAmountInBaseUnits, "&destination=").concat(quote.recipientAddress);
|
178
|
+
log.info("URL: ", URL_1);
|
179
|
+
return [4 /*yield*/, nodeRequest(URL_1)];
|
180
|
+
case 3:
|
181
|
+
quoteFromNode = _a.sent();
|
182
|
+
log.info("quoteFromNode: ", quoteFromNode);
|
183
|
+
if (quoteFromNode.error)
|
184
|
+
throw Error(quoteFromNode.error);
|
185
|
+
amountOutEstimated = (parseInt(quoteFromNode.expected_amount_out) / BASE_UNIT).toFixed(DECIMALS);
|
186
|
+
output.amountOut = amountOutEstimated;
|
187
|
+
memoInput = {
|
188
|
+
type: 'SWAP',
|
189
|
+
asset: quote.buyAsset,
|
190
|
+
destAddr: quote.recipientAddress,
|
191
|
+
lim: null,
|
192
|
+
interval: null,
|
193
|
+
quantity: null,
|
194
|
+
affiliate: null,
|
195
|
+
fee: null,
|
196
|
+
dexAggregatorAddr: null,
|
197
|
+
finalAssetAddr: null,
|
198
|
+
minAmountOut: null
|
199
|
+
};
|
200
|
+
memo = createMemo(memoInput);
|
201
|
+
log.info(tag, "memo: ", memo);
|
202
|
+
type = void 0;
|
203
|
+
chain = quote.sellAsset.split(".")[0];
|
204
|
+
if (chain == "MAYA") {
|
205
|
+
type = 'deposit';
|
206
|
+
}
|
207
|
+
else {
|
208
|
+
type = 'transfer';
|
209
|
+
}
|
210
|
+
tx = void 0;
|
211
|
+
if (!(chain === "ETH")) return [3 /*break*/, 8];
|
212
|
+
type = 'EVM';
|
213
|
+
return [4 /*yield*/, nodeRequest('/inbound_addresses')];
|
214
|
+
case 4:
|
215
|
+
inbounds = _a.sent();
|
216
|
+
console.log("inbounds: ", inbounds);
|
217
|
+
inbound = inbounds.find(function (i) { return i.chain === 'ETH'; });
|
218
|
+
console.log("inbound: ", inbound);
|
219
|
+
tokenAddress = (quote.sellAsset.split('-')[1] || '').toLowerCase();
|
220
|
+
log.info("tokenAddress: ", tokenAddress);
|
221
|
+
depositAbi = [
|
222
|
+
{
|
223
|
+
"constant": false,
|
224
|
+
"inputs": [
|
225
|
+
{ "name": "tokenAddress", "type": "address" },
|
226
|
+
{ "name": "to", "type": "address" },
|
227
|
+
{ "name": "amount", "type": "uint256" },
|
228
|
+
{ "name": "data", "type": "string" }
|
229
|
+
],
|
230
|
+
"name": "deposit",
|
231
|
+
"outputs": [],
|
232
|
+
"payable": false,
|
233
|
+
"stateMutability": "nonpayable",
|
234
|
+
"type": "function"
|
235
|
+
}
|
236
|
+
];
|
237
|
+
routerContract = new web3.eth.Contract(depositAbi, inbound.router);
|
238
|
+
log.info("routerContract: ", routerContract);
|
239
|
+
quoteSellAmountInEther = web3.utils.toWei(quote.sellAmount, 'ether');
|
240
|
+
numberBN = web3.utils.toBN(quoteSellAmountInEther);
|
241
|
+
numberHex = '0x' + numberBN.toString(16);
|
242
|
+
console.log("numberHex: ", numberHex);
|
243
|
+
data = routerContract.methods
|
244
|
+
.deposit(inbound.address, '0x0000000000000000000000000000000000000000', // 0 = ETH
|
245
|
+
numberHex, memo)
|
246
|
+
.encodeABI();
|
247
|
+
return [4 /*yield*/, web3.eth.estimateGas({
|
248
|
+
from: quote.senderAddress,
|
249
|
+
to: inbound.router,
|
250
|
+
value: 0,
|
251
|
+
data: data,
|
252
|
+
})];
|
253
|
+
case 5:
|
254
|
+
gasLimit = _a.sent();
|
255
|
+
gasLimit = gasLimit + gasLimit * 0.2;
|
256
|
+
gasLimit = parseInt(gasLimit);
|
257
|
+
log.info(tag, 'gasLimit: ', gasLimit);
|
258
|
+
return [4 /*yield*/, web3.eth.getBlock("latest")];
|
259
|
+
case 6:
|
260
|
+
block = _a.sent();
|
261
|
+
baseFeePerGas = block.baseFeePerGas;
|
262
|
+
return [4 /*yield*/, web3.eth.getGasPrice()];
|
263
|
+
case 7:
|
264
|
+
gasPrice = _a.sent();
|
265
|
+
maxPriorityFeePerGas = baseFeePerGas ? web3.utils.toBN(baseFeePerGas).div(web3.utils.toBN(2)).toString() : '0';
|
266
|
+
maxFeePerGas = baseFeePerGas ? web3.utils.toBN(baseFeePerGas).mul(web3.utils.toBN(2)).toString() : gasPrice;
|
267
|
+
feeData = {
|
268
|
+
gasPrice: gasPrice, // Legacy networks
|
269
|
+
baseFeePerGas: baseFeePerGas, // EIP-1559 networks
|
270
|
+
maxPriorityFeePerGas: maxPriorityFeePerGas, // EIP-1559 calculation
|
271
|
+
maxFeePerGas: maxFeePerGas // EIP-1559 calculation
|
272
|
+
};
|
273
|
+
console.log("feeData: ", feeData);
|
274
|
+
// Calculate a 20% buffer and add it to the gasLimit
|
275
|
+
// const buffer = gasLimit.mul(20).div(100); // 20% buffer
|
276
|
+
// gasLimit = gasLimit.add(buffer);
|
277
|
+
// //if gas limit is < 21000 then set to 21000
|
278
|
+
// if (gasLimit.lt(ethers.BigNumber.from('36000'))) {
|
279
|
+
// gasLimit = ethers.BigNumber.from('36000');
|
280
|
+
// }
|
281
|
+
// const nonce = await web3.eth.getTransactionCount(quote.senderAddress, 'latest'); // 'latest' can be replaced with the desired block number
|
282
|
+
// console.log("nonce: ",nonce)
|
283
|
+
tx = {
|
284
|
+
type: type,
|
285
|
+
chain: ChainToNetworkId[quote.sellAsset.split(".")[0]],
|
286
|
+
txParams: {
|
287
|
+
to: inbound.router,
|
288
|
+
from: quote.senderAddress,
|
289
|
+
data: data,
|
290
|
+
value: numberHex,
|
291
|
+
"gasLimit": Web3.utils.toHex(gasLimit),
|
292
|
+
"maxPriorityFeePerGas": maxPriorityFeePerGas,
|
293
|
+
"maxFeePerGas": maxFeePerGas,
|
294
|
+
"nonce": null
|
295
|
+
}
|
296
|
+
};
|
297
|
+
return [3 /*break*/, 9];
|
298
|
+
case 8:
|
299
|
+
tx = {
|
300
|
+
type: type,
|
301
|
+
chain: ChainToNetworkId[quote.sellAsset.split(".")[0]],
|
302
|
+
txParams: {
|
303
|
+
senderAddress: quote.senderAddress,
|
304
|
+
recipientAddress: quoteFromNode.inbound_address,
|
305
|
+
amount: quote.sellAmount,
|
306
|
+
token: quote.sellAsset.split(".")[1],
|
307
|
+
memo: quoteFromNode.memo || memo
|
308
|
+
}
|
309
|
+
};
|
310
|
+
_a.label = 9;
|
311
|
+
case 9:
|
312
|
+
output.txs = [
|
313
|
+
tx
|
314
|
+
];
|
315
|
+
return [2 /*return*/, output];
|
316
|
+
case 10:
|
317
|
+
e_1 = _a.sent();
|
318
|
+
console.error(tag, "e: ", e_1);
|
319
|
+
throw e_1;
|
320
|
+
case 11: return [2 /*return*/];
|
321
|
+
}
|
322
|
+
});
|
323
|
+
});
|
324
|
+
};
|