@cityofzion/bs-multichain 0.0.1 → 1.0.2
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/LICENSE +674 -0
- package/dist/BSAggregator.d.ts +14 -0
- package/dist/BSAggregator.js +65 -0
- package/dist/features/bridge/Neo3NeoXBridgeOrchestrator.d.ts +21 -0
- package/dist/features/bridge/Neo3NeoXBridgeOrchestrator.js +360 -0
- package/dist/features/bridge/index.d.ts +2 -0
- package/dist/features/bridge/index.js +18 -0
- package/dist/features/bridge/types.d.ts +6 -0
- package/dist/features/bridge/types.js +2 -0
- package/dist/features/swap/SimpleSwapApi.d.ts +23 -0
- package/dist/features/swap/SimpleSwapApi.js +258 -0
- package/dist/features/swap/SimpleSwapOrchestrator.d.ts +17 -0
- package/dist/features/swap/SimpleSwapOrchestrator.js +441 -0
- package/dist/features/swap/SimpleSwapService.d.ts +6 -0
- package/dist/features/swap/SimpleSwapService.js +56 -0
- package/dist/features/swap/index.d.ts +4 -0
- package/dist/features/swap/index.js +20 -0
- package/dist/features/swap/types.d.ts +64 -0
- package/dist/features/swap/types.js +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/package.json +38 -11
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
var _SimpleSwapOrchestrator_instances, _SimpleSwapOrchestrator_api, _SimpleSwapOrchestrator_blockchainServicesByName, _SimpleSwapOrchestrator_chainsByServiceName, _SimpleSwapOrchestrator_amountToUseTimeout, _SimpleSwapOrchestrator_internalAvailableTokensToUse, _SimpleSwapOrchestrator_internalTokenToUse, _SimpleSwapOrchestrator_internalAccountToUse, _SimpleSwapOrchestrator_internalAmountToUse, _SimpleSwapOrchestrator_internalAmountToUseMinMax, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, _SimpleSwapOrchestrator_internalTokenToReceive, _SimpleSwapOrchestrator_internalAddressToReceive, _SimpleSwapOrchestrator_internalExtraIdToReceive, _SimpleSwapOrchestrator_internalAmountToReceive, _SimpleSwapOrchestrator_createSwapToken, _SimpleSwapOrchestrator_availableTokensToUse_get, _SimpleSwapOrchestrator_availableTokensToUse_set, _SimpleSwapOrchestrator_tokenToUse_get, _SimpleSwapOrchestrator_tokenToUse_set, _SimpleSwapOrchestrator_accountToUse_get, _SimpleSwapOrchestrator_accountToUse_set, _SimpleSwapOrchestrator_amountToUse_get, _SimpleSwapOrchestrator_amountToUse_set, _SimpleSwapOrchestrator_amountToUseMinMax_get, _SimpleSwapOrchestrator_amountToUseMinMax_set, _SimpleSwapOrchestrator_availableTokensToReceive_get, _SimpleSwapOrchestrator_availableTokensToReceive_set, _SimpleSwapOrchestrator_tokenToReceive_get, _SimpleSwapOrchestrator_tokenToReceive_set, _SimpleSwapOrchestrator_addressToReceive_get, _SimpleSwapOrchestrator_addressToReceive_set, _SimpleSwapOrchestrator_extraIdToReceive_get, _SimpleSwapOrchestrator_extraIdToReceive_set, _SimpleSwapOrchestrator_amountToReceive_get, _SimpleSwapOrchestrator_amountToReceive_set, _SimpleSwapOrchestrator_recalculateValues;
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.SimpleSwapOrchestrator = void 0;
|
|
28
|
+
const blockchain_service_1 = require("@cityofzion/blockchain-service");
|
|
29
|
+
const events_1 = __importDefault(require("events"));
|
|
30
|
+
const SimpleSwapApi_1 = require("./SimpleSwapApi");
|
|
31
|
+
class SimpleSwapOrchestrator {
|
|
32
|
+
constructor(params) {
|
|
33
|
+
_SimpleSwapOrchestrator_instances.add(this);
|
|
34
|
+
_SimpleSwapOrchestrator_api.set(this, void 0);
|
|
35
|
+
_SimpleSwapOrchestrator_blockchainServicesByName.set(this, void 0);
|
|
36
|
+
_SimpleSwapOrchestrator_chainsByServiceName.set(this, void 0);
|
|
37
|
+
_SimpleSwapOrchestrator_amountToUseTimeout.set(this, null);
|
|
38
|
+
_SimpleSwapOrchestrator_internalAvailableTokensToUse.set(this, {
|
|
39
|
+
loading: false,
|
|
40
|
+
value: null,
|
|
41
|
+
});
|
|
42
|
+
_SimpleSwapOrchestrator_internalTokenToUse.set(this, { loading: false, value: null });
|
|
43
|
+
_SimpleSwapOrchestrator_internalAccountToUse.set(this, { loading: false, value: null, valid: null });
|
|
44
|
+
_SimpleSwapOrchestrator_internalAmountToUse.set(this, { loading: false, value: null });
|
|
45
|
+
_SimpleSwapOrchestrator_internalAmountToUseMinMax.set(this, { loading: false, value: null });
|
|
46
|
+
_SimpleSwapOrchestrator_internalAvailableTokensToReceive.set(this, {
|
|
47
|
+
loading: false,
|
|
48
|
+
value: null,
|
|
49
|
+
});
|
|
50
|
+
_SimpleSwapOrchestrator_internalTokenToReceive.set(this, { loading: false, value: null });
|
|
51
|
+
_SimpleSwapOrchestrator_internalAddressToReceive.set(this, { loading: false, value: null, valid: null });
|
|
52
|
+
_SimpleSwapOrchestrator_internalExtraIdToReceive.set(this, { loading: false, value: null, valid: null });
|
|
53
|
+
_SimpleSwapOrchestrator_internalAmountToReceive.set(this, { loading: false, value: null });
|
|
54
|
+
this.eventEmitter = new events_1.default();
|
|
55
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_api, new SimpleSwapApi_1.SimpleSwapApi(), "f");
|
|
56
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_blockchainServicesByName, params.blockchainServicesByName, "f");
|
|
57
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_chainsByServiceName, params.chainsByServiceName, "f");
|
|
58
|
+
}
|
|
59
|
+
init() {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
try {
|
|
62
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: true }, "a", _SimpleSwapOrchestrator_availableTokensToUse_set);
|
|
63
|
+
const tokens = yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_api, "f").getCurrencies({
|
|
64
|
+
blockchainServicesByName: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_blockchainServicesByName, "f"),
|
|
65
|
+
chainsByServiceName: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_chainsByServiceName, "f"),
|
|
66
|
+
});
|
|
67
|
+
const filteredTokens = tokens.filter(token => token.blockchain && token.hash);
|
|
68
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: filteredTokens }, "a", _SimpleSwapOrchestrator_availableTokensToUse_set);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: [] }, "a", _SimpleSwapOrchestrator_availableTokensToUse_set);
|
|
72
|
+
this.eventEmitter.emit('error', error.message);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
setTokenToUse(token) {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: null }, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
81
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: null }, "a", _SimpleSwapOrchestrator_amountToUseMinMax_set);
|
|
82
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: true }, "a", _SimpleSwapOrchestrator_tokenToUse_set);
|
|
83
|
+
if (!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_availableTokensToUse_get).value)
|
|
84
|
+
throw new Error('Available tokens to use is not set');
|
|
85
|
+
let simpleSwapCurrency = null;
|
|
86
|
+
if (token) {
|
|
87
|
+
simpleSwapCurrency = (_a = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_availableTokensToUse_get).value.find(item => item.id === token.id)) !== null && _a !== void 0 ? _a : null;
|
|
88
|
+
if (!simpleSwapCurrency)
|
|
89
|
+
throw new Error('You are trying to use a token that is not available');
|
|
90
|
+
}
|
|
91
|
+
if (simpleSwapCurrency && simpleSwapCurrency.decimals === undefined) {
|
|
92
|
+
if (!(simpleSwapCurrency === null || simpleSwapCurrency === void 0 ? void 0 : simpleSwapCurrency.blockchain) || !simpleSwapCurrency.hash)
|
|
93
|
+
throw new Error('Token is not valid');
|
|
94
|
+
let decimals = 6;
|
|
95
|
+
try {
|
|
96
|
+
const service = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_blockchainServicesByName, "f")[simpleSwapCurrency.blockchain];
|
|
97
|
+
const tokenInfo = yield service.blockchainDataService.getTokenInfo(simpleSwapCurrency.hash);
|
|
98
|
+
decimals = tokenInfo.decimals;
|
|
99
|
+
}
|
|
100
|
+
catch (_c) {
|
|
101
|
+
/* empty */
|
|
102
|
+
}
|
|
103
|
+
simpleSwapCurrency.decimals = decimals;
|
|
104
|
+
}
|
|
105
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: simpleSwapCurrency }, "a", _SimpleSwapOrchestrator_tokenToUse_set);
|
|
106
|
+
if (((_b = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value) === null || _b === void 0 ? void 0 : _b.blockchain) !== (simpleSwapCurrency === null || simpleSwapCurrency === void 0 ? void 0 : simpleSwapCurrency.blockchain)) {
|
|
107
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: null }, "a", _SimpleSwapOrchestrator_accountToUse_set);
|
|
108
|
+
}
|
|
109
|
+
yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_recalculateValues).call(this, ['amountToReceive', 'availableTokensToReceive', 'amountToUseMinMax', 'amountToUse']);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
setAccountToUse(account) {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: account }, "a", _SimpleSwapOrchestrator_accountToUse_set);
|
|
115
|
+
yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_recalculateValues).call(this, []);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
setAmountToUse(amount) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
121
|
+
value: amount,
|
|
122
|
+
}, "a", _SimpleSwapOrchestrator_amountToUse_set);
|
|
123
|
+
if (__classPrivateFieldGet(this, _SimpleSwapOrchestrator_amountToUseTimeout, "f") !== null)
|
|
124
|
+
clearTimeout(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_amountToUseTimeout, "f"));
|
|
125
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_amountToUseTimeout, setTimeout(() => {
|
|
126
|
+
var _a;
|
|
127
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
128
|
+
value: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value
|
|
129
|
+
? blockchain_service_1.BSBigNumberHelper.format(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value, {
|
|
130
|
+
decimals: (_a = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value) === null || _a === void 0 ? void 0 : _a.decimals,
|
|
131
|
+
})
|
|
132
|
+
: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value,
|
|
133
|
+
}, "a", _SimpleSwapOrchestrator_amountToUse_set);
|
|
134
|
+
__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_recalculateValues).call(this, ['amountToReceive']);
|
|
135
|
+
}, 1500), "f");
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
setTokenToReceive(token) {
|
|
139
|
+
var _a;
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null, valid: null }, "a", _SimpleSwapOrchestrator_extraIdToReceive_set);
|
|
142
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: null }, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
143
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false, value: null }, "a", _SimpleSwapOrchestrator_amountToUseMinMax_set);
|
|
144
|
+
if (!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_availableTokensToReceive_get).value)
|
|
145
|
+
throw new Error('Available tokens to receive is not set');
|
|
146
|
+
let simpleSwapCurrency = null;
|
|
147
|
+
if (token) {
|
|
148
|
+
simpleSwapCurrency = (_a = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_availableTokensToReceive_get).value.find(item => item.id === token.id)) !== null && _a !== void 0 ? _a : null;
|
|
149
|
+
if (!simpleSwapCurrency)
|
|
150
|
+
throw new Error('You are trying to use a token that is not available');
|
|
151
|
+
}
|
|
152
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
153
|
+
value: simpleSwapCurrency ? Object.assign(Object.assign({}, token), simpleSwapCurrency) : null,
|
|
154
|
+
}, "a", _SimpleSwapOrchestrator_tokenToReceive_set);
|
|
155
|
+
yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_recalculateValues).call(this, ['amountToReceive', 'amountToUseMinMax', 'amountToUse']);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
setAddressToReceive(address) {
|
|
159
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
161
|
+
loading: false,
|
|
162
|
+
value: address,
|
|
163
|
+
}, "a", _SimpleSwapOrchestrator_addressToReceive_set);
|
|
164
|
+
yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_recalculateValues).call(this, []);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
setExtraIdToReceive(extraIdToReceive) {
|
|
168
|
+
var _a;
|
|
169
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
if (!((_a = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value) === null || _a === void 0 ? void 0 : _a.hasExtraId))
|
|
171
|
+
return;
|
|
172
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: extraIdToReceive, valid: null }, "a", _SimpleSwapOrchestrator_extraIdToReceive_set);
|
|
173
|
+
yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_recalculateValues).call(this, []);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
swap() {
|
|
177
|
+
var _a;
|
|
178
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
+
if (!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value ||
|
|
180
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value ||
|
|
181
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value ||
|
|
182
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).value ||
|
|
183
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).valid ||
|
|
184
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value ||
|
|
185
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToReceive_get).value ||
|
|
186
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.hash ||
|
|
187
|
+
(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value.hasExtraId &&
|
|
188
|
+
(!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_extraIdToReceive_get).valid || !((_a = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_extraIdToReceive_get).value) === null || _a === void 0 ? void 0 : _a.trim())))) {
|
|
189
|
+
throw new Error('Not all required fields are set');
|
|
190
|
+
}
|
|
191
|
+
const result = {
|
|
192
|
+
id: '',
|
|
193
|
+
txFrom: undefined,
|
|
194
|
+
log: undefined,
|
|
195
|
+
};
|
|
196
|
+
try {
|
|
197
|
+
const { depositAddress, id, log } = yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_api, "f").createExchange({
|
|
198
|
+
currencyFrom: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value,
|
|
199
|
+
currencyTo: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value,
|
|
200
|
+
amount: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value,
|
|
201
|
+
refundAddress: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value.address,
|
|
202
|
+
address: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).value,
|
|
203
|
+
extraIdToReceive: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_extraIdToReceive_get).value,
|
|
204
|
+
});
|
|
205
|
+
result.id = id;
|
|
206
|
+
result.log = log;
|
|
207
|
+
const service = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_blockchainServicesByName, "f")[__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value.blockchain];
|
|
208
|
+
const [transactionHash] = yield service.transfer({
|
|
209
|
+
senderAccount: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value,
|
|
210
|
+
intents: [
|
|
211
|
+
{
|
|
212
|
+
amount: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value,
|
|
213
|
+
receiverAddress: depositAddress,
|
|
214
|
+
tokenHash: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.hash,
|
|
215
|
+
tokenDecimals: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.decimals,
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
});
|
|
219
|
+
result.txFrom = transactionHash;
|
|
220
|
+
}
|
|
221
|
+
catch (_b) {
|
|
222
|
+
// empty
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
calculateFee() {
|
|
228
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
+
if (!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value ||
|
|
230
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value ||
|
|
231
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value ||
|
|
232
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).value ||
|
|
233
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).valid ||
|
|
234
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value ||
|
|
235
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToReceive_get).value ||
|
|
236
|
+
!__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.hash) {
|
|
237
|
+
throw new Error('Not all required fields are set');
|
|
238
|
+
}
|
|
239
|
+
const { blockchain } = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value;
|
|
240
|
+
const service = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_blockchainServicesByName, "f")[blockchain];
|
|
241
|
+
if (!(0, blockchain_service_1.isCalculableFee)(service))
|
|
242
|
+
return '0';
|
|
243
|
+
return yield service.calculateTransferFee({
|
|
244
|
+
senderAccount: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value,
|
|
245
|
+
intents: [
|
|
246
|
+
{
|
|
247
|
+
amount: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value,
|
|
248
|
+
receiverAddress: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value.blockchain === blockchain
|
|
249
|
+
? __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).value
|
|
250
|
+
: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value.address,
|
|
251
|
+
tokenHash: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.hash,
|
|
252
|
+
tokenDecimals: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.decimals,
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
exports.SimpleSwapOrchestrator = SimpleSwapOrchestrator;
|
|
260
|
+
_SimpleSwapOrchestrator_api = new WeakMap(), _SimpleSwapOrchestrator_blockchainServicesByName = new WeakMap(), _SimpleSwapOrchestrator_chainsByServiceName = new WeakMap(), _SimpleSwapOrchestrator_amountToUseTimeout = new WeakMap(), _SimpleSwapOrchestrator_internalAvailableTokensToUse = new WeakMap(), _SimpleSwapOrchestrator_internalTokenToUse = new WeakMap(), _SimpleSwapOrchestrator_internalAccountToUse = new WeakMap(), _SimpleSwapOrchestrator_internalAmountToUse = new WeakMap(), _SimpleSwapOrchestrator_internalAmountToUseMinMax = new WeakMap(), _SimpleSwapOrchestrator_internalAvailableTokensToReceive = new WeakMap(), _SimpleSwapOrchestrator_internalTokenToReceive = new WeakMap(), _SimpleSwapOrchestrator_internalAddressToReceive = new WeakMap(), _SimpleSwapOrchestrator_internalExtraIdToReceive = new WeakMap(), _SimpleSwapOrchestrator_internalAmountToReceive = new WeakMap(), _SimpleSwapOrchestrator_instances = new WeakSet(), _SimpleSwapOrchestrator_createSwapToken = function _SimpleSwapOrchestrator_createSwapToken(token) {
|
|
261
|
+
return {
|
|
262
|
+
id: token.id,
|
|
263
|
+
blockchain: token.blockchain,
|
|
264
|
+
imageUrl: token.imageUrl,
|
|
265
|
+
symbol: token.symbol,
|
|
266
|
+
name: token.name,
|
|
267
|
+
hash: token.hash,
|
|
268
|
+
decimals: token.decimals,
|
|
269
|
+
addressTemplateUrl: token.addressTemplateUrl,
|
|
270
|
+
txTemplateUrl: token.txTemplateUrl,
|
|
271
|
+
network: token.network,
|
|
272
|
+
hasExtraId: token.hasExtraId,
|
|
273
|
+
};
|
|
274
|
+
}, _SimpleSwapOrchestrator_availableTokensToUse_get = function _SimpleSwapOrchestrator_availableTokensToUse_get() {
|
|
275
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToUse, "f");
|
|
276
|
+
}, _SimpleSwapOrchestrator_availableTokensToUse_set = function _SimpleSwapOrchestrator_availableTokensToUse_set(availableTokens) {
|
|
277
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalAvailableTokensToUse, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToUse, "f")), availableTokens), "f");
|
|
278
|
+
this.eventEmitter.emit('availableTokensToUse', Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToUse, "f")), { value: !__classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToUse, "f").value
|
|
279
|
+
? __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToUse, "f").value
|
|
280
|
+
: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToUse, "f").value.map(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_createSwapToken)) }));
|
|
281
|
+
}, _SimpleSwapOrchestrator_tokenToUse_get = function _SimpleSwapOrchestrator_tokenToUse_get() {
|
|
282
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToUse, "f");
|
|
283
|
+
}, _SimpleSwapOrchestrator_tokenToUse_set = function _SimpleSwapOrchestrator_tokenToUse_set(tokenToUse) {
|
|
284
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalTokenToUse, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToUse, "f")), tokenToUse), "f");
|
|
285
|
+
this.eventEmitter.emit('tokenToUse', Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToUse, "f")), { value: !__classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToUse, "f").value
|
|
286
|
+
? __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToUse, "f").value
|
|
287
|
+
: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_createSwapToken).call(this, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToUse, "f").value) }));
|
|
288
|
+
}, _SimpleSwapOrchestrator_accountToUse_get = function _SimpleSwapOrchestrator_accountToUse_get() {
|
|
289
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAccountToUse, "f");
|
|
290
|
+
}, _SimpleSwapOrchestrator_accountToUse_set = function _SimpleSwapOrchestrator_accountToUse_set(accountToUse) {
|
|
291
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalAccountToUse, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAccountToUse, "f")), accountToUse), "f");
|
|
292
|
+
this.eventEmitter.emit('accountToUse', __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAccountToUse, "f"));
|
|
293
|
+
}, _SimpleSwapOrchestrator_amountToUse_get = function _SimpleSwapOrchestrator_amountToUse_get() {
|
|
294
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToUse, "f");
|
|
295
|
+
}, _SimpleSwapOrchestrator_amountToUse_set = function _SimpleSwapOrchestrator_amountToUse_set(amountToUse) {
|
|
296
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalAmountToUse, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToUse, "f")), amountToUse), "f");
|
|
297
|
+
this.eventEmitter.emit('amountToUse', __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToUse, "f"));
|
|
298
|
+
}, _SimpleSwapOrchestrator_amountToUseMinMax_get = function _SimpleSwapOrchestrator_amountToUseMinMax_get() {
|
|
299
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToUseMinMax, "f");
|
|
300
|
+
}, _SimpleSwapOrchestrator_amountToUseMinMax_set = function _SimpleSwapOrchestrator_amountToUseMinMax_set(amountToUseMinMax) {
|
|
301
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalAmountToUseMinMax, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToUseMinMax, "f")), amountToUseMinMax), "f");
|
|
302
|
+
this.eventEmitter.emit('amountToUseMinMax', __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToUseMinMax, "f"));
|
|
303
|
+
}, _SimpleSwapOrchestrator_availableTokensToReceive_get = function _SimpleSwapOrchestrator_availableTokensToReceive_get() {
|
|
304
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, "f");
|
|
305
|
+
}, _SimpleSwapOrchestrator_availableTokensToReceive_set = function _SimpleSwapOrchestrator_availableTokensToReceive_set(availableTokens) {
|
|
306
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, "f")), availableTokens), "f");
|
|
307
|
+
this.eventEmitter.emit('availableTokensToReceive', Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, "f")), { value: !__classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, "f").value
|
|
308
|
+
? __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, "f").value
|
|
309
|
+
: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAvailableTokensToReceive, "f").value.map(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_createSwapToken)) }));
|
|
310
|
+
}, _SimpleSwapOrchestrator_tokenToReceive_get = function _SimpleSwapOrchestrator_tokenToReceive_get() {
|
|
311
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToReceive, "f");
|
|
312
|
+
}, _SimpleSwapOrchestrator_tokenToReceive_set = function _SimpleSwapOrchestrator_tokenToReceive_set(tokenToReceive) {
|
|
313
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalTokenToReceive, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToReceive, "f")), tokenToReceive), "f");
|
|
314
|
+
this.eventEmitter.emit('tokenToReceive', Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToReceive, "f")), { value: !__classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToReceive, "f").value
|
|
315
|
+
? __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToReceive, "f").value
|
|
316
|
+
: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "m", _SimpleSwapOrchestrator_createSwapToken).call(this, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalTokenToReceive, "f").value) }));
|
|
317
|
+
}, _SimpleSwapOrchestrator_addressToReceive_get = function _SimpleSwapOrchestrator_addressToReceive_get() {
|
|
318
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAddressToReceive, "f");
|
|
319
|
+
}, _SimpleSwapOrchestrator_addressToReceive_set = function _SimpleSwapOrchestrator_addressToReceive_set(addressToReceive) {
|
|
320
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalAddressToReceive, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAddressToReceive, "f")), addressToReceive), "f");
|
|
321
|
+
this.eventEmitter.emit('addressToReceive', __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAddressToReceive, "f"));
|
|
322
|
+
}, _SimpleSwapOrchestrator_extraIdToReceive_get = function _SimpleSwapOrchestrator_extraIdToReceive_get() {
|
|
323
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalExtraIdToReceive, "f");
|
|
324
|
+
}, _SimpleSwapOrchestrator_extraIdToReceive_set = function _SimpleSwapOrchestrator_extraIdToReceive_set(extraIdToReceive) {
|
|
325
|
+
if (extraIdToReceive.value === '')
|
|
326
|
+
extraIdToReceive.value = null;
|
|
327
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalExtraIdToReceive, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalExtraIdToReceive, "f")), extraIdToReceive), "f");
|
|
328
|
+
this.eventEmitter.emit('extraIdToReceive', __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalExtraIdToReceive, "f"));
|
|
329
|
+
}, _SimpleSwapOrchestrator_amountToReceive_get = function _SimpleSwapOrchestrator_amountToReceive_get() {
|
|
330
|
+
return __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToReceive, "f");
|
|
331
|
+
}, _SimpleSwapOrchestrator_amountToReceive_set = function _SimpleSwapOrchestrator_amountToReceive_set(amountToReceive) {
|
|
332
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_internalAmountToReceive, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToReceive, "f")), amountToReceive), "f");
|
|
333
|
+
this.eventEmitter.emit('amountToReceive', __classPrivateFieldGet(this, _SimpleSwapOrchestrator_internalAmountToReceive, "f"));
|
|
334
|
+
}, _SimpleSwapOrchestrator_recalculateValues = function _SimpleSwapOrchestrator_recalculateValues(fieldsToRecalculate) {
|
|
335
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
336
|
+
try {
|
|
337
|
+
if (__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value === null) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).value && __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value) {
|
|
341
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
342
|
+
valid: RegExp(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value.validationAddress).test(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_addressToReceive_get).value),
|
|
343
|
+
}, "a", _SimpleSwapOrchestrator_addressToReceive_set);
|
|
344
|
+
}
|
|
345
|
+
if (__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_extraIdToReceive_get).value && __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value) {
|
|
346
|
+
const extraIdToReceive = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_extraIdToReceive_get).value.trim();
|
|
347
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
348
|
+
valid: !extraIdToReceive || !__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value.validationExtra
|
|
349
|
+
? true
|
|
350
|
+
: RegExp(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value.validationExtra).test(extraIdToReceive),
|
|
351
|
+
}, "a", _SimpleSwapOrchestrator_extraIdToReceive_set);
|
|
352
|
+
}
|
|
353
|
+
if (__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value) {
|
|
354
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { valid: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.blockchain === __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_accountToUse_get).value.blockchain }, "a", _SimpleSwapOrchestrator_accountToUse_set);
|
|
355
|
+
}
|
|
356
|
+
const shouldRecalculateAvailableTokensToReceive = fieldsToRecalculate.includes('availableTokensToReceive');
|
|
357
|
+
const shouldRecalculateAmountToUse = fieldsToRecalculate.includes('amountToUse') &&
|
|
358
|
+
__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value === null &&
|
|
359
|
+
__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value !== null;
|
|
360
|
+
const shouldRecalculateAmountToReceive = fieldsToRecalculate.includes('amountToReceive') && __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value !== null;
|
|
361
|
+
const shouldRecalculateAmountToUseMinMax = fieldsToRecalculate.includes('amountToUseMinMax') && __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value !== null;
|
|
362
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: shouldRecalculateAvailableTokensToReceive }, "a", _SimpleSwapOrchestrator_availableTokensToReceive_set);
|
|
363
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: shouldRecalculateAmountToUseMinMax }, "a", _SimpleSwapOrchestrator_amountToUseMinMax_set);
|
|
364
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: shouldRecalculateAmountToUse }, "a", _SimpleSwapOrchestrator_amountToUse_set);
|
|
365
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: shouldRecalculateAmountToReceive }, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
366
|
+
if (shouldRecalculateAvailableTokensToReceive) {
|
|
367
|
+
try {
|
|
368
|
+
const pairs = yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_api, "f").getPairs(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.ticker, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.network);
|
|
369
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: pairs }, "a", _SimpleSwapOrchestrator_availableTokensToReceive_set);
|
|
370
|
+
if (__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value && !pairs.some(pair => pair.ticker === __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.ticker)) {
|
|
371
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_tokenToReceive_set);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
this.eventEmitter.emit('error', error.message);
|
|
376
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_availableTokensToReceive_set);
|
|
377
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_tokenToReceive_set);
|
|
378
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_amountToUseMinMax_set);
|
|
379
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
380
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null, valid: null }, "a", _SimpleSwapOrchestrator_addressToReceive_set);
|
|
381
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null, valid: null }, "a", _SimpleSwapOrchestrator_extraIdToReceive_set);
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if (shouldRecalculateAmountToUseMinMax || shouldRecalculateAmountToUse || shouldRecalculateAmountToReceive) {
|
|
386
|
+
let range = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUseMinMax_get).value;
|
|
387
|
+
try {
|
|
388
|
+
if ((shouldRecalculateAmountToUseMinMax || range === null) && __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value) {
|
|
389
|
+
const { decimals } = __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value;
|
|
390
|
+
const rangeResponse = yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_api, "f").getRange(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value);
|
|
391
|
+
// Add 1% because the SimpleSwap sends us a smaller minimum than the required
|
|
392
|
+
const minWithOnePercent = blockchain_service_1.BSBigNumberHelper.format((Number(rangeResponse.min) * 1.01).toString(), {
|
|
393
|
+
decimals,
|
|
394
|
+
});
|
|
395
|
+
// Add the smallest number to round up correctly because the SimpleSwap doesn't have the decimals, and we need to apply the decimals here
|
|
396
|
+
const smallestNumberToRoundUp = decimals ? `0.${'2'.padStart(decimals, '0')}` : '1';
|
|
397
|
+
range = {
|
|
398
|
+
min: blockchain_service_1.BSBigNumberHelper.format(Number(minWithOnePercent) + Number(smallestNumberToRoundUp), {
|
|
399
|
+
decimals,
|
|
400
|
+
}),
|
|
401
|
+
max: rangeResponse.max ? blockchain_service_1.BSBigNumberHelper.format(rangeResponse.max, { decimals }) : rangeResponse.max,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: range }, "a", _SimpleSwapOrchestrator_amountToUseMinMax_set);
|
|
405
|
+
if (shouldRecalculateAmountToUse && range) {
|
|
406
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
407
|
+
value: range.min
|
|
408
|
+
? blockchain_service_1.BSBigNumberHelper.format(range.min, { decimals: __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value.decimals })
|
|
409
|
+
: range.min,
|
|
410
|
+
}, "a", _SimpleSwapOrchestrator_amountToUse_set);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
catch (error) {
|
|
414
|
+
this.eventEmitter.emit('error', error.message);
|
|
415
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_amountToUseMinMax_set);
|
|
416
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
417
|
+
throw error;
|
|
418
|
+
}
|
|
419
|
+
if (shouldRecalculateAmountToReceive && __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value && __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value) {
|
|
420
|
+
try {
|
|
421
|
+
const estimate = yield __classPrivateFieldGet(this, _SimpleSwapOrchestrator_api, "f").getEstimate(__classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToUse_get).value, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_tokenToReceive_get).value, __classPrivateFieldGet(this, _SimpleSwapOrchestrator_instances, "a", _SimpleSwapOrchestrator_amountToUse_get).value);
|
|
422
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, {
|
|
423
|
+
value: estimate,
|
|
424
|
+
}, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
425
|
+
}
|
|
426
|
+
catch (error) {
|
|
427
|
+
this.eventEmitter.emit('error', error.message);
|
|
428
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { value: null }, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
429
|
+
throw error;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
finally {
|
|
435
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false }, "a", _SimpleSwapOrchestrator_availableTokensToReceive_set);
|
|
436
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false }, "a", _SimpleSwapOrchestrator_amountToUseMinMax_set);
|
|
437
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false }, "a", _SimpleSwapOrchestrator_amountToUse_set);
|
|
438
|
+
__classPrivateFieldSet(this, _SimpleSwapOrchestrator_instances, { loading: false }, "a", _SimpleSwapOrchestrator_amountToReceive_set);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ISwapService, TSwapServiceStatusResponse } from '@cityofzion/blockchain-service';
|
|
2
|
+
export declare class SimpleSwapService<BSName extends string = string> implements ISwapService {
|
|
3
|
+
#private;
|
|
4
|
+
constructor();
|
|
5
|
+
getStatus(id: string): Promise<TSwapServiceStatusResponse>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var _SimpleSwapService_api;
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.SimpleSwapService = void 0;
|
|
25
|
+
const SimpleSwapApi_1 = require("./SimpleSwapApi");
|
|
26
|
+
class SimpleSwapService {
|
|
27
|
+
constructor() {
|
|
28
|
+
_SimpleSwapService_api.set(this, void 0);
|
|
29
|
+
__classPrivateFieldSet(this, _SimpleSwapService_api, new SimpleSwapApi_1.SimpleSwapApi(), "f");
|
|
30
|
+
}
|
|
31
|
+
getStatus(id) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
const response = yield __classPrivateFieldGet(this, _SimpleSwapService_api, "f").getExchange(id);
|
|
34
|
+
const statusBySimpleSwapStatus = {
|
|
35
|
+
waiting: 'confirming',
|
|
36
|
+
confirming: 'confirming',
|
|
37
|
+
exchanging: 'exchanging',
|
|
38
|
+
sending: 'exchanging',
|
|
39
|
+
verifying: 'exchanging',
|
|
40
|
+
finished: 'finished',
|
|
41
|
+
expired: 'failed',
|
|
42
|
+
failed: 'failed',
|
|
43
|
+
refunded: 'refunded',
|
|
44
|
+
};
|
|
45
|
+
const status = statusBySimpleSwapStatus[response.status];
|
|
46
|
+
return {
|
|
47
|
+
status,
|
|
48
|
+
txFrom: response.txFrom,
|
|
49
|
+
txTo: response.txTo,
|
|
50
|
+
log: response.log,
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.SimpleSwapService = SimpleSwapService;
|
|
56
|
+
_SimpleSwapService_api = new WeakMap();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./SimpleSwapApi"), exports);
|
|
19
|
+
__exportStar(require("./SimpleSwapService"), exports);
|
|
20
|
+
__exportStar(require("./SimpleSwapOrchestrator"), exports);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { BlockchainService, TSwapToken } from '@cityofzion/blockchain-service';
|
|
2
|
+
export type TSimpleSwapOrchestratorInitParams<BSName extends string = string> = {
|
|
3
|
+
blockchainServicesByName: Record<BSName, BlockchainService<BSName>>;
|
|
4
|
+
chainsByServiceName: Partial<Record<BSName, string[]>>;
|
|
5
|
+
};
|
|
6
|
+
export type TSimpleSwapApiCurrency<BSName extends string = string> = TSwapToken<BSName> & {
|
|
7
|
+
network: string;
|
|
8
|
+
ticker: string;
|
|
9
|
+
hasExtraId: boolean;
|
|
10
|
+
validationExtra: string | null;
|
|
11
|
+
validationAddress: string;
|
|
12
|
+
};
|
|
13
|
+
export type TSimpleSwapApiCreateExchangeParams = {
|
|
14
|
+
currencyFrom: TSimpleSwapApiCurrency;
|
|
15
|
+
currencyTo: TSimpleSwapApiCurrency;
|
|
16
|
+
amount: string;
|
|
17
|
+
refundAddress: string;
|
|
18
|
+
address: string;
|
|
19
|
+
extraIdToReceive: string | null;
|
|
20
|
+
};
|
|
21
|
+
export type TSimpleSwapApiCurrencyResponse = {
|
|
22
|
+
name: string | null;
|
|
23
|
+
ticker: string | null;
|
|
24
|
+
network: string | null;
|
|
25
|
+
hasExtraId: boolean;
|
|
26
|
+
validationExtra: string | null;
|
|
27
|
+
validationAddress: string | null;
|
|
28
|
+
image: string | null;
|
|
29
|
+
contractAddress: string | null;
|
|
30
|
+
addressExplorer: string | null;
|
|
31
|
+
txExplorer: string | null;
|
|
32
|
+
};
|
|
33
|
+
export type TSimpleSwapApiGetCurrenciesResponse = {
|
|
34
|
+
result: TSimpleSwapApiCurrencyResponse[];
|
|
35
|
+
};
|
|
36
|
+
export type TSimpleSwapApiGetPairsResponse = {
|
|
37
|
+
result: Record<string, string[]>;
|
|
38
|
+
};
|
|
39
|
+
export type TSimpleSwapApiGetRangeResponse = {
|
|
40
|
+
result: {
|
|
41
|
+
min: string;
|
|
42
|
+
max: string | null;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
export type TSimpleSwapApiGetEstimateResponse = {
|
|
46
|
+
result: {
|
|
47
|
+
estimatedAmount: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
export type TSimpleSwapApiCreateExchangeResponse = {
|
|
51
|
+
result: {
|
|
52
|
+
id: string;
|
|
53
|
+
addressFrom: string;
|
|
54
|
+
log?: string;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
export type TSimpleSwapApiGetExchangeResponse = {
|
|
58
|
+
result: {
|
|
59
|
+
status: string;
|
|
60
|
+
txFrom?: string;
|
|
61
|
+
txTo?: string;
|
|
62
|
+
log?: string;
|
|
63
|
+
};
|
|
64
|
+
};
|
package/dist/index.d.ts
ADDED