@cityofzion/bs-neo3 0.13.1 → 0.14.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/dist/constants.d.ts +12 -0
- package/dist/constants.js +23 -1
- package/dist/flamingo-swap/FlamingoSwapControllerService.d.ts +55 -0
- package/dist/flamingo-swap/FlamingoSwapControllerService.js +332 -0
- package/dist/flamingo-swap/FlamingoSwapError.d.ts +12 -0
- package/dist/flamingo-swap/FlamingoSwapError.js +28 -0
- package/dist/flamingo-swap/FlamingoSwapHelper.d.ts +34 -0
- package/dist/flamingo-swap/FlamingoSwapHelper.js +120 -0
- package/dist/flamingo-swap/FlamingoSwapNeonDappKitInvocationBuilder.d.ts +18 -0
- package/dist/flamingo-swap/FlamingoSwapNeonDappKitInvocationBuilder.js +210 -0
- package/dist/index.d.ts +8 -3
- package/dist/index.js +8 -3
- package/package.json +6 -3
package/dist/constants.d.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { NetworkType, Token } from '@cityofzion/blockchain-service';
|
|
2
2
|
export declare const TOKENS: Record<NetworkType, Token[]>;
|
|
3
|
+
export declare const BLOCKCHAIN_WSS_URL = "wss://rpc10.n3.nspcc.ru:10331/ws";
|
|
3
4
|
export declare const NEO_NS_HASH = "0x50ac1c37690cc2cfc594472833cf57505d5f46de";
|
|
5
|
+
export declare const GAS_PER_NEO = 0.001;
|
|
4
6
|
export declare const GHOSTMARKET_URL_BY_NETWORK_TYPE: Partial<Record<NetworkType, string>>;
|
|
5
7
|
export declare const GHOSTMARKET_CHAIN_BY_NETWORK_TYPE: Partial<Record<NetworkType, string>>;
|
|
6
8
|
export declare const DERIVATION_PATH = "m/44'/888'/0'/0/?";
|
|
7
9
|
export declare const RPC_LIST_BY_NETWORK_TYPE: Record<NetworkType, string[]>;
|
|
8
10
|
export declare const DEFAULT_URL_BY_NETWORK_TYPE: Record<NetworkType, string>;
|
|
11
|
+
export type SwapScriptHashes = {
|
|
12
|
+
flamingoSwapRouter: string;
|
|
13
|
+
flamingoPairWhiteList: string;
|
|
14
|
+
flamingoFactory: string;
|
|
15
|
+
neo: string;
|
|
16
|
+
gas: string;
|
|
17
|
+
bneo: string;
|
|
18
|
+
flpBneoGas: string;
|
|
19
|
+
};
|
|
20
|
+
export declare const SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE: Partial<Record<NetworkType, SwapScriptHashes>>;
|
package/dist/constants.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DEFAULT_URL_BY_NETWORK_TYPE = exports.RPC_LIST_BY_NETWORK_TYPE = exports.DERIVATION_PATH = exports.GHOSTMARKET_CHAIN_BY_NETWORK_TYPE = exports.GHOSTMARKET_URL_BY_NETWORK_TYPE = exports.NEO_NS_HASH = exports.TOKENS = void 0;
|
|
6
|
+
exports.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE = exports.DEFAULT_URL_BY_NETWORK_TYPE = exports.RPC_LIST_BY_NETWORK_TYPE = exports.DERIVATION_PATH = exports.GHOSTMARKET_CHAIN_BY_NETWORK_TYPE = exports.GHOSTMARKET_URL_BY_NETWORK_TYPE = exports.GAS_PER_NEO = exports.NEO_NS_HASH = exports.BLOCKCHAIN_WSS_URL = exports.TOKENS = void 0;
|
|
7
7
|
const common_json_1 = __importDefault(require("./assets/tokens/common.json"));
|
|
8
8
|
const mainnet_json_1 = __importDefault(require("./assets/tokens/mainnet.json"));
|
|
9
9
|
exports.TOKENS = {
|
|
@@ -11,7 +11,9 @@ exports.TOKENS = {
|
|
|
11
11
|
testnet: common_json_1.default,
|
|
12
12
|
custom: common_json_1.default,
|
|
13
13
|
};
|
|
14
|
+
exports.BLOCKCHAIN_WSS_URL = 'wss://rpc10.n3.nspcc.ru:10331/ws';
|
|
14
15
|
exports.NEO_NS_HASH = '0x50ac1c37690cc2cfc594472833cf57505d5f46de';
|
|
16
|
+
exports.GAS_PER_NEO = 0.001;
|
|
15
17
|
exports.GHOSTMARKET_URL_BY_NETWORK_TYPE = {
|
|
16
18
|
mainnet: 'https://api.ghostmarket.io/api/v2',
|
|
17
19
|
testnet: 'https://api-testnet.ghostmarket.io/api/v2',
|
|
@@ -50,3 +52,23 @@ exports.DEFAULT_URL_BY_NETWORK_TYPE = {
|
|
|
50
52
|
testnet: exports.RPC_LIST_BY_NETWORK_TYPE.testnet[0],
|
|
51
53
|
custom: exports.RPC_LIST_BY_NETWORK_TYPE.custom[0],
|
|
52
54
|
};
|
|
55
|
+
exports.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE = {
|
|
56
|
+
mainnet: {
|
|
57
|
+
flamingoSwapRouter: '0xf970f4ccecd765b63732b821775dc38c25d74f23',
|
|
58
|
+
flamingoPairWhiteList: '0xfb75a5314069b56e136713d38477f647a13991b4',
|
|
59
|
+
flamingoFactory: '0xca2d20610d7982ebe0bed124ee7e9b2d580a6efc',
|
|
60
|
+
gas: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
|
|
61
|
+
neo: '0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5',
|
|
62
|
+
bneo: '0x48c40d4666f93408be1bef038b6722404d9a4c2a',
|
|
63
|
+
flpBneoGas: '0x3244fcadcccff190c329f7b3083e4da2af60fbce',
|
|
64
|
+
},
|
|
65
|
+
testnet: {
|
|
66
|
+
flamingoSwapRouter: '0x6f0910fa26290f4a423930c8f833395790c71705',
|
|
67
|
+
flamingoPairWhiteList: '0xfb75a5314069b56e136713d38477f647a13991b4',
|
|
68
|
+
flamingoFactory: '0xca2d20610d7982ebe0bed124ee7e9b2d580a6efc',
|
|
69
|
+
gas: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
|
|
70
|
+
neo: '0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5',
|
|
71
|
+
bneo: '0x85deac50febfd93988d3f391dea54e8289e43e9e',
|
|
72
|
+
flpBneoGas: '0x3244fcadcccff190c329f7b3083e4da2af60fbce',
|
|
73
|
+
},
|
|
74
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Account, Network, SwapControllerService, SwapControllerServiceEvents, SwapControllerServiceSwapToReceiveArgs, SwapControllerServiceSwapToUseArgs, Token } from '@cityofzion/blockchain-service';
|
|
2
|
+
import TypedEmitter from 'typed-emitter';
|
|
3
|
+
import WebSocket from 'isomorphic-ws';
|
|
4
|
+
export declare class FlamingoSwapControllerService implements SwapControllerService {
|
|
5
|
+
#private;
|
|
6
|
+
eventEmitter: TypedEmitter<SwapControllerServiceEvents>;
|
|
7
|
+
ws: WebSocket;
|
|
8
|
+
constructor(network: Network);
|
|
9
|
+
buildSwapArgs(): SwapControllerServiceSwapToReceiveArgs | SwapControllerServiceSwapToUseArgs;
|
|
10
|
+
setAccountToUse(account: Account | null): void;
|
|
11
|
+
setAmountToUse(val: string | null): void;
|
|
12
|
+
setAmountToReceive(val: string | null): void;
|
|
13
|
+
setDeadline(deadline: string): void;
|
|
14
|
+
setSlippage(slippage: number): void;
|
|
15
|
+
setTokenToUse(val: Token | null): Promise<void>;
|
|
16
|
+
setTokenToReceive(val: Token | null): Promise<void>;
|
|
17
|
+
swap(isLedger?: boolean): Promise<void>;
|
|
18
|
+
setReserves(): Promise<void>;
|
|
19
|
+
startListeningBlockGeneration(): void;
|
|
20
|
+
stopListeningBlockGeneration(): void;
|
|
21
|
+
private recalculateSwapArguments;
|
|
22
|
+
private clearFields;
|
|
23
|
+
private get lastAmountChange();
|
|
24
|
+
private set lastAmountChange(value);
|
|
25
|
+
private get accountToUse();
|
|
26
|
+
private set accountToUse(value);
|
|
27
|
+
private get amountToUse();
|
|
28
|
+
private set amountToUse(value);
|
|
29
|
+
private get minimumReceived();
|
|
30
|
+
private set minimumReceived(value);
|
|
31
|
+
private get maximumSelling();
|
|
32
|
+
private set maximumSelling(value);
|
|
33
|
+
private get amountToReceive();
|
|
34
|
+
private set amountToReceive(value);
|
|
35
|
+
private get deadline();
|
|
36
|
+
private set deadline(value);
|
|
37
|
+
private get liquidityProviderFee();
|
|
38
|
+
private set liquidityProviderFee(value);
|
|
39
|
+
private get priceImpact();
|
|
40
|
+
private set priceImpact(value);
|
|
41
|
+
private get priceInverse();
|
|
42
|
+
private set priceInverse(value);
|
|
43
|
+
private get routes();
|
|
44
|
+
private set routes(value);
|
|
45
|
+
private get reservesToUse();
|
|
46
|
+
private set reservesToUse(value);
|
|
47
|
+
private get reservesToReceive();
|
|
48
|
+
private set reservesToReceive(value);
|
|
49
|
+
private get slippage();
|
|
50
|
+
private set slippage(value);
|
|
51
|
+
private get tokenToReceive();
|
|
52
|
+
private set tokenToReceive(value);
|
|
53
|
+
private get tokenToUse();
|
|
54
|
+
private set tokenToUse(value);
|
|
55
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
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 _FlamingoSwapControllerService_network, _FlamingoSwapControllerService_accountToUse, _FlamingoSwapControllerService_tokenToReceive, _FlamingoSwapControllerService_tokenToUse, _FlamingoSwapControllerService_amountToReceive, _FlamingoSwapControllerService_amountToUse, _FlamingoSwapControllerService_minimumReceived, _FlamingoSwapControllerService_maximumSelling, _FlamingoSwapControllerService_reservesToReceive, _FlamingoSwapControllerService_reservesToUse, _FlamingoSwapControllerService_slippage, _FlamingoSwapControllerService_deadline, _FlamingoSwapControllerService_priceInverse, _FlamingoSwapControllerService_priceImpact, _FlamingoSwapControllerService_liquidityProviderFee, _FlamingoSwapControllerService_routes, _FlamingoSwapControllerService_lastAmountChange;
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.FlamingoSwapControllerService = void 0;
|
|
28
|
+
const events_1 = __importDefault(require("events"));
|
|
29
|
+
const FlamingoSwapNeonDappKitInvocationBuilder_1 = require("./FlamingoSwapNeonDappKitInvocationBuilder");
|
|
30
|
+
const neon_dappkit_1 = require("@cityofzion/neon-dappkit");
|
|
31
|
+
const neon_core_1 = require("@cityofzion/neon-core");
|
|
32
|
+
const FlamingoSwapError_1 = require("./FlamingoSwapError");
|
|
33
|
+
const FlamingoSwapHelper_1 = require("./FlamingoSwapHelper");
|
|
34
|
+
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
35
|
+
const constants_1 = require("../constants");
|
|
36
|
+
class FlamingoSwapControllerService {
|
|
37
|
+
constructor(network) {
|
|
38
|
+
_FlamingoSwapControllerService_network.set(this, void 0);
|
|
39
|
+
_FlamingoSwapControllerService_accountToUse.set(this, null);
|
|
40
|
+
_FlamingoSwapControllerService_tokenToReceive.set(this, null);
|
|
41
|
+
_FlamingoSwapControllerService_tokenToUse.set(this, null);
|
|
42
|
+
_FlamingoSwapControllerService_amountToReceive.set(this, null);
|
|
43
|
+
_FlamingoSwapControllerService_amountToUse.set(this, null);
|
|
44
|
+
_FlamingoSwapControllerService_minimumReceived.set(this, null);
|
|
45
|
+
_FlamingoSwapControllerService_maximumSelling.set(this, null);
|
|
46
|
+
_FlamingoSwapControllerService_reservesToReceive.set(this, null);
|
|
47
|
+
_FlamingoSwapControllerService_reservesToUse.set(this, null);
|
|
48
|
+
_FlamingoSwapControllerService_slippage.set(this, 0.5);
|
|
49
|
+
_FlamingoSwapControllerService_deadline.set(this, '10');
|
|
50
|
+
_FlamingoSwapControllerService_priceInverse.set(this, null);
|
|
51
|
+
_FlamingoSwapControllerService_priceImpact.set(this, null);
|
|
52
|
+
_FlamingoSwapControllerService_liquidityProviderFee.set(this, null);
|
|
53
|
+
_FlamingoSwapControllerService_routes.set(this, null);
|
|
54
|
+
_FlamingoSwapControllerService_lastAmountChange.set(this, null);
|
|
55
|
+
if (network.type === 'custom')
|
|
56
|
+
throw new FlamingoSwapError_1.CustomNetworkNotSupportedError();
|
|
57
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_network, network, "f");
|
|
58
|
+
this.eventEmitter = new events_1.default();
|
|
59
|
+
}
|
|
60
|
+
buildSwapArgs() {
|
|
61
|
+
if (!this.accountToUse)
|
|
62
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('accountToUse');
|
|
63
|
+
if (!this.amountToReceive)
|
|
64
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('amountToReceive');
|
|
65
|
+
if (!this.amountToUse)
|
|
66
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('amountToUse');
|
|
67
|
+
if (!this.tokenToReceive)
|
|
68
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('tokenToReceive');
|
|
69
|
+
if (!this.tokenToUse)
|
|
70
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('tokenToUse');
|
|
71
|
+
const baseSwapArgs = {
|
|
72
|
+
address: this.accountToUse.address,
|
|
73
|
+
amountToReceive: this.amountToReceive,
|
|
74
|
+
amountToUse: this.amountToUse,
|
|
75
|
+
tokenToReceive: this.tokenToReceive,
|
|
76
|
+
tokenToUse: this.tokenToUse,
|
|
77
|
+
deadline: this.deadline,
|
|
78
|
+
network: __classPrivateFieldGet(this, _FlamingoSwapControllerService_network, "f"),
|
|
79
|
+
};
|
|
80
|
+
if (this.lastAmountChange === 'amountToReceive') {
|
|
81
|
+
if (!this.maximumSelling)
|
|
82
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('maximumSelling');
|
|
83
|
+
return Object.assign(Object.assign({}, baseSwapArgs), { maximumSelling: this.maximumSelling, type: 'swapTokenToReceive' });
|
|
84
|
+
}
|
|
85
|
+
if (!this.minimumReceived)
|
|
86
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('minimumReceived');
|
|
87
|
+
return Object.assign(Object.assign({}, baseSwapArgs), { minimumReceived: this.minimumReceived, type: 'swapTokenToUse' });
|
|
88
|
+
}
|
|
89
|
+
setAccountToUse(account) {
|
|
90
|
+
this.accountToUse = account;
|
|
91
|
+
}
|
|
92
|
+
setAmountToUse(val) {
|
|
93
|
+
this.amountToUse = val;
|
|
94
|
+
this.lastAmountChange = 'amountToUse';
|
|
95
|
+
this.recalculateSwapArguments();
|
|
96
|
+
}
|
|
97
|
+
setAmountToReceive(val) {
|
|
98
|
+
this.amountToReceive = val;
|
|
99
|
+
this.lastAmountChange = 'amountToReceive';
|
|
100
|
+
this.recalculateSwapArguments();
|
|
101
|
+
}
|
|
102
|
+
setDeadline(deadline) {
|
|
103
|
+
this.deadline = deadline;
|
|
104
|
+
}
|
|
105
|
+
setSlippage(slippage) {
|
|
106
|
+
this.slippage = slippage;
|
|
107
|
+
this.recalculateSwapArguments();
|
|
108
|
+
}
|
|
109
|
+
setTokenToUse(val) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
this.tokenToUse = val;
|
|
112
|
+
yield this.setReserves();
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
setTokenToReceive(val) {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
this.tokenToReceive = val;
|
|
118
|
+
yield this.setReserves();
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
swap(isLedger) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
if (isLedger)
|
|
124
|
+
throw new Error('Method not implemented.');
|
|
125
|
+
if (!this.accountToUse)
|
|
126
|
+
throw new FlamingoSwapError_1.FlamingoSwapMissingParametersError('accountToUse');
|
|
127
|
+
const swapArguments = this.buildSwapArgs();
|
|
128
|
+
const neonInvokerAccount = new neon_core_1.wallet.Account(this.accountToUse);
|
|
129
|
+
const invoker = yield neon_dappkit_1.NeonInvoker.init({
|
|
130
|
+
rpcAddress: __classPrivateFieldGet(this, _FlamingoSwapControllerService_network, "f").url,
|
|
131
|
+
account: neonInvokerAccount,
|
|
132
|
+
});
|
|
133
|
+
yield invoker.invokeFunction(FlamingoSwapNeonDappKitInvocationBuilder_1.FlamingoSwapNeonDappKitInvocationBuilder.swapInvocation(swapArguments));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
setReserves() {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
if (!this.tokenToReceive || !this.tokenToUse)
|
|
139
|
+
return;
|
|
140
|
+
const invoker = yield neon_dappkit_1.NeonInvoker.init({
|
|
141
|
+
rpcAddress: __classPrivateFieldGet(this, _FlamingoSwapControllerService_network, "f").url,
|
|
142
|
+
});
|
|
143
|
+
const invocation = FlamingoSwapNeonDappKitInvocationBuilder_1.FlamingoSwapNeonDappKitInvocationBuilder.getReservesInvocation({
|
|
144
|
+
network: __classPrivateFieldGet(this, _FlamingoSwapControllerService_network, "f"),
|
|
145
|
+
tokenToReceiveScriptHash: this.tokenToReceive.hash,
|
|
146
|
+
tokenToUseScriptHash: this.tokenToUse.hash,
|
|
147
|
+
});
|
|
148
|
+
const { stack } = yield invoker.testInvoke(invocation);
|
|
149
|
+
if (!neon_dappkit_1.TypeChecker.isStackTypeArray(stack[0]) ||
|
|
150
|
+
!neon_dappkit_1.TypeChecker.isStackTypeInteger(stack[0].value[0]) ||
|
|
151
|
+
!neon_dappkit_1.TypeChecker.isStackTypeInteger(stack[0].value[1]))
|
|
152
|
+
throw new FlamingoSwapError_1.FlamingoInvalidReservesResponseError();
|
|
153
|
+
this.reservesToReceive = stack[0].value[0].value;
|
|
154
|
+
this.reservesToUse = stack[0].value[1].value;
|
|
155
|
+
this.recalculateSwapArguments();
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
startListeningBlockGeneration() {
|
|
159
|
+
this.ws = new isomorphic_ws_1.default(constants_1.BLOCKCHAIN_WSS_URL);
|
|
160
|
+
this.ws.onopen = () => {
|
|
161
|
+
const block_added = {
|
|
162
|
+
jsonrpc: '2.0',
|
|
163
|
+
method: 'subscribe',
|
|
164
|
+
params: ['block_added'],
|
|
165
|
+
id: 1,
|
|
166
|
+
};
|
|
167
|
+
this.ws.send(JSON.stringify(block_added));
|
|
168
|
+
};
|
|
169
|
+
this.ws.onmessage = () => __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
this.setReserves();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
stopListeningBlockGeneration() {
|
|
174
|
+
if (this.ws) {
|
|
175
|
+
this.ws.close();
|
|
176
|
+
this.ws = null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
recalculateSwapArguments() {
|
|
180
|
+
if (!this.tokenToReceive || !this.tokenToUse || !this.reservesToReceive || !this.reservesToUse)
|
|
181
|
+
return;
|
|
182
|
+
if ((this.lastAmountChange === 'amountToReceive' && this.amountToReceive) ||
|
|
183
|
+
(this.lastAmountChange === 'amountToUse' && this.amountToUse)) {
|
|
184
|
+
const amountToReceive = this.lastAmountChange === 'amountToReceive' ? this.amountToReceive : null;
|
|
185
|
+
const amountToUse = this.lastAmountChange === 'amountToUse' ? this.amountToUse : null;
|
|
186
|
+
const { amountToUseToDisplay, amountToReceiveToDisplay, maximumSelling, minimumReceived, liquidityProviderFee, priceImpact, priceInverse, } = FlamingoSwapHelper_1.FlamingoSwapHelper.getSwapFields({
|
|
187
|
+
network: __classPrivateFieldGet(this, _FlamingoSwapControllerService_network, "f"),
|
|
188
|
+
amountToReceive,
|
|
189
|
+
amountToUse,
|
|
190
|
+
tokenToUse: this.tokenToUse,
|
|
191
|
+
tokenToReceive: this.tokenToReceive,
|
|
192
|
+
reservesToUse: this.reservesToUse,
|
|
193
|
+
reservesToReceive: this.reservesToReceive,
|
|
194
|
+
slippage: this.slippage,
|
|
195
|
+
});
|
|
196
|
+
this.amountToUse = amountToUseToDisplay;
|
|
197
|
+
this.amountToReceive = amountToReceiveToDisplay;
|
|
198
|
+
this.maximumSelling = maximumSelling;
|
|
199
|
+
this.minimumReceived = minimumReceived;
|
|
200
|
+
this.liquidityProviderFee = liquidityProviderFee;
|
|
201
|
+
this.priceImpact = priceImpact;
|
|
202
|
+
this.priceInverse = priceInverse;
|
|
203
|
+
this.routes = []; // TODO: It will be implemented in Swap Multi Invoke issue
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
this.clearFields();
|
|
207
|
+
}
|
|
208
|
+
clearFields() {
|
|
209
|
+
this.amountToUse = null;
|
|
210
|
+
this.amountToReceive = null;
|
|
211
|
+
this.minimumReceived = null;
|
|
212
|
+
this.maximumSelling = null;
|
|
213
|
+
this.liquidityProviderFee = null;
|
|
214
|
+
this.priceImpact = null;
|
|
215
|
+
this.priceInverse = null;
|
|
216
|
+
}
|
|
217
|
+
// Getters and setters
|
|
218
|
+
get lastAmountChange() {
|
|
219
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_lastAmountChange, "f");
|
|
220
|
+
}
|
|
221
|
+
set lastAmountChange(val) {
|
|
222
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_lastAmountChange, val, "f");
|
|
223
|
+
this.eventEmitter.emit('lastAmountChanged', val);
|
|
224
|
+
}
|
|
225
|
+
get accountToUse() {
|
|
226
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_accountToUse, "f");
|
|
227
|
+
}
|
|
228
|
+
set accountToUse(val) {
|
|
229
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_accountToUse, val, "f");
|
|
230
|
+
this.eventEmitter.emit('accountToUse', val);
|
|
231
|
+
}
|
|
232
|
+
get amountToUse() {
|
|
233
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_amountToUse, "f");
|
|
234
|
+
}
|
|
235
|
+
set amountToUse(val) {
|
|
236
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_amountToUse, val, "f");
|
|
237
|
+
this.eventEmitter.emit('amountToUse', val);
|
|
238
|
+
}
|
|
239
|
+
get minimumReceived() {
|
|
240
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_minimumReceived, "f");
|
|
241
|
+
}
|
|
242
|
+
set minimumReceived(val) {
|
|
243
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_minimumReceived, val, "f");
|
|
244
|
+
this.eventEmitter.emit('minimumReceived', val);
|
|
245
|
+
}
|
|
246
|
+
get maximumSelling() {
|
|
247
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_maximumSelling, "f");
|
|
248
|
+
}
|
|
249
|
+
set maximumSelling(val) {
|
|
250
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_maximumSelling, val, "f");
|
|
251
|
+
this.eventEmitter.emit('maximumSelling', val);
|
|
252
|
+
}
|
|
253
|
+
get amountToReceive() {
|
|
254
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_amountToReceive, "f");
|
|
255
|
+
}
|
|
256
|
+
set amountToReceive(val) {
|
|
257
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_amountToReceive, val, "f");
|
|
258
|
+
this.eventEmitter.emit('amountToReceive', val);
|
|
259
|
+
}
|
|
260
|
+
get deadline() {
|
|
261
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_deadline, "f");
|
|
262
|
+
}
|
|
263
|
+
set deadline(val) {
|
|
264
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_deadline, val, "f");
|
|
265
|
+
this.eventEmitter.emit('deadline', val);
|
|
266
|
+
}
|
|
267
|
+
get liquidityProviderFee() {
|
|
268
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_liquidityProviderFee, "f");
|
|
269
|
+
}
|
|
270
|
+
set liquidityProviderFee(val) {
|
|
271
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_liquidityProviderFee, val, "f");
|
|
272
|
+
this.eventEmitter.emit('liquidityProviderFee', val);
|
|
273
|
+
}
|
|
274
|
+
get priceImpact() {
|
|
275
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_priceImpact, "f");
|
|
276
|
+
}
|
|
277
|
+
set priceImpact(val) {
|
|
278
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_priceImpact, val, "f");
|
|
279
|
+
this.eventEmitter.emit('priceImpact', val);
|
|
280
|
+
}
|
|
281
|
+
get priceInverse() {
|
|
282
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_priceInverse, "f");
|
|
283
|
+
}
|
|
284
|
+
set priceInverse(val) {
|
|
285
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_priceInverse, val, "f");
|
|
286
|
+
this.eventEmitter.emit('priceInverse', val);
|
|
287
|
+
}
|
|
288
|
+
get routes() {
|
|
289
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_routes, "f");
|
|
290
|
+
}
|
|
291
|
+
set routes(val) {
|
|
292
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_routes, val, "f");
|
|
293
|
+
this.eventEmitter.emit('routes', val);
|
|
294
|
+
}
|
|
295
|
+
get reservesToUse() {
|
|
296
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_reservesToUse, "f");
|
|
297
|
+
}
|
|
298
|
+
set reservesToUse(val) {
|
|
299
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_reservesToUse, val, "f");
|
|
300
|
+
this.eventEmitter.emit('reservesToUse', val);
|
|
301
|
+
}
|
|
302
|
+
get reservesToReceive() {
|
|
303
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_reservesToReceive, "f");
|
|
304
|
+
}
|
|
305
|
+
set reservesToReceive(val) {
|
|
306
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_reservesToReceive, val, "f");
|
|
307
|
+
this.eventEmitter.emit('reservesToReceive', val);
|
|
308
|
+
}
|
|
309
|
+
get slippage() {
|
|
310
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_slippage, "f");
|
|
311
|
+
}
|
|
312
|
+
set slippage(val) {
|
|
313
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_slippage, val, "f");
|
|
314
|
+
this.eventEmitter.emit('slippage', val);
|
|
315
|
+
}
|
|
316
|
+
get tokenToReceive() {
|
|
317
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_tokenToReceive, "f");
|
|
318
|
+
}
|
|
319
|
+
set tokenToReceive(val) {
|
|
320
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_tokenToReceive, val, "f");
|
|
321
|
+
this.eventEmitter.emit('tokenToReceive', val);
|
|
322
|
+
}
|
|
323
|
+
get tokenToUse() {
|
|
324
|
+
return __classPrivateFieldGet(this, _FlamingoSwapControllerService_tokenToUse, "f");
|
|
325
|
+
}
|
|
326
|
+
set tokenToUse(val) {
|
|
327
|
+
__classPrivateFieldSet(this, _FlamingoSwapControllerService_tokenToUse, val, "f");
|
|
328
|
+
this.eventEmitter.emit('tokenToUse', val);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
exports.FlamingoSwapControllerService = FlamingoSwapControllerService;
|
|
332
|
+
_FlamingoSwapControllerService_network = new WeakMap(), _FlamingoSwapControllerService_accountToUse = new WeakMap(), _FlamingoSwapControllerService_tokenToReceive = new WeakMap(), _FlamingoSwapControllerService_tokenToUse = new WeakMap(), _FlamingoSwapControllerService_amountToReceive = new WeakMap(), _FlamingoSwapControllerService_amountToUse = new WeakMap(), _FlamingoSwapControllerService_minimumReceived = new WeakMap(), _FlamingoSwapControllerService_maximumSelling = new WeakMap(), _FlamingoSwapControllerService_reservesToReceive = new WeakMap(), _FlamingoSwapControllerService_reservesToUse = new WeakMap(), _FlamingoSwapControllerService_slippage = new WeakMap(), _FlamingoSwapControllerService_deadline = new WeakMap(), _FlamingoSwapControllerService_priceInverse = new WeakMap(), _FlamingoSwapControllerService_priceImpact = new WeakMap(), _FlamingoSwapControllerService_liquidityProviderFee = new WeakMap(), _FlamingoSwapControllerService_routes = new WeakMap(), _FlamingoSwapControllerService_lastAmountChange = new WeakMap();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class FlamingoSwapError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class FlamingoSwapMissingParametersError extends FlamingoSwapError {
|
|
5
|
+
constructor(parameter: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class CustomNetworkNotSupportedError extends FlamingoSwapError {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
export declare class FlamingoInvalidReservesResponseError extends FlamingoSwapError {
|
|
11
|
+
constructor();
|
|
12
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlamingoInvalidReservesResponseError = exports.CustomNetworkNotSupportedError = exports.FlamingoSwapMissingParametersError = exports.FlamingoSwapError = void 0;
|
|
4
|
+
class FlamingoSwapError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'FlamingoSwapError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.FlamingoSwapError = FlamingoSwapError;
|
|
11
|
+
class FlamingoSwapMissingParametersError extends FlamingoSwapError {
|
|
12
|
+
constructor(parameter) {
|
|
13
|
+
super(`Missing parameter: ${parameter} for swap invocation`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.FlamingoSwapMissingParametersError = FlamingoSwapMissingParametersError;
|
|
17
|
+
class CustomNetworkNotSupportedError extends FlamingoSwapError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super('Custom network is not supported');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.CustomNetworkNotSupportedError = CustomNetworkNotSupportedError;
|
|
23
|
+
class FlamingoInvalidReservesResponseError extends FlamingoSwapError {
|
|
24
|
+
constructor() {
|
|
25
|
+
super('Invalid reserves response');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.FlamingoInvalidReservesResponseError = FlamingoInvalidReservesResponseError;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { Network, Token } from '@cityofzion/blockchain-service';
|
|
3
|
+
type TGetSwapArgs = {
|
|
4
|
+
amountToUse: string | null;
|
|
5
|
+
amountToReceive: string | null;
|
|
6
|
+
tokenToUse: Token;
|
|
7
|
+
tokenToReceive: Token;
|
|
8
|
+
reservesToUse: string;
|
|
9
|
+
reservesToReceive: string;
|
|
10
|
+
slippage: number;
|
|
11
|
+
network: Network;
|
|
12
|
+
};
|
|
13
|
+
export declare class FlamingoSwapHelper {
|
|
14
|
+
static readonly BN_0: BigNumber;
|
|
15
|
+
static readonly BN_1: BigNumber;
|
|
16
|
+
static readonly BN_997: BigNumber;
|
|
17
|
+
static readonly BN_1000: BigNumber;
|
|
18
|
+
static readonly FEE_RATE: BigNumber;
|
|
19
|
+
static getSwapFields(params: TGetSwapArgs): {
|
|
20
|
+
amountToReceiveToDisplay: string;
|
|
21
|
+
amountToUseToDisplay: string;
|
|
22
|
+
liquidityProviderFee: string;
|
|
23
|
+
priceImpact: string | null;
|
|
24
|
+
priceInverse: string | null;
|
|
25
|
+
minimumReceived: string | null;
|
|
26
|
+
maximumSelling: string | null;
|
|
27
|
+
};
|
|
28
|
+
static overrideToken(network: Network, token: Token): Token;
|
|
29
|
+
private static createTradeData;
|
|
30
|
+
private static overrideAmountInput;
|
|
31
|
+
private static overrideAmountToDisplay;
|
|
32
|
+
private static normalizeHash;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FlamingoSwapHelper = void 0;
|
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
+
const constants_1 = require("../constants");
|
|
9
|
+
class FlamingoSwapHelper {
|
|
10
|
+
static getSwapFields(params) {
|
|
11
|
+
const reservesToUseBn = new bignumber_js_1.default(params.reservesToUse);
|
|
12
|
+
const reservesToReceiveBn = new bignumber_js_1.default(params.reservesToReceive);
|
|
13
|
+
if (reservesToUseBn.lt(this.BN_0) || reservesToReceiveBn.lt(this.BN_0)) {
|
|
14
|
+
throw new Error('Reserve should be positive number');
|
|
15
|
+
}
|
|
16
|
+
let amountToUseBn;
|
|
17
|
+
let amountToReceiveBn;
|
|
18
|
+
let amountToUseToDisplay;
|
|
19
|
+
let amountToReceiveToDisplay;
|
|
20
|
+
let maximumSelling = null;
|
|
21
|
+
let minimumReceived = null;
|
|
22
|
+
const slippageFormatted = new bignumber_js_1.default(params.slippage * 10).shiftedBy(-3);
|
|
23
|
+
if (params.amountToReceive) {
|
|
24
|
+
amountToReceiveBn = this.overrideAmountInput(params.network, params.amountToReceive, params.tokenToReceive);
|
|
25
|
+
amountToUseBn = reservesToUseBn
|
|
26
|
+
.times(amountToReceiveBn)
|
|
27
|
+
.times(this.BN_1000)
|
|
28
|
+
.idiv(reservesToReceiveBn.minus(amountToReceiveBn).times(this.BN_997))
|
|
29
|
+
.plus(this.BN_1);
|
|
30
|
+
maximumSelling = amountToUseBn
|
|
31
|
+
.times(this.BN_1.plus(slippageFormatted))
|
|
32
|
+
.dp(0)
|
|
33
|
+
.shiftedBy(-params.tokenToUse.decimals)
|
|
34
|
+
.toFixed();
|
|
35
|
+
amountToUseToDisplay = this.overrideAmountToDisplay(params.network, amountToUseBn.toFixed(), params.tokenToUse);
|
|
36
|
+
amountToReceiveToDisplay = params.amountToReceive;
|
|
37
|
+
}
|
|
38
|
+
else if (params.amountToUse) {
|
|
39
|
+
amountToUseBn = this.overrideAmountInput(params.network, params.amountToUse, params.tokenToUse);
|
|
40
|
+
const amountToUseWithFee = amountToUseBn.times(this.BN_997);
|
|
41
|
+
amountToReceiveBn = amountToUseWithFee
|
|
42
|
+
.times(params.reservesToReceive)
|
|
43
|
+
.idiv(reservesToUseBn.times(this.BN_1000).plus(amountToUseWithFee));
|
|
44
|
+
minimumReceived = amountToReceiveBn
|
|
45
|
+
.times(this.BN_1.minus(slippageFormatted))
|
|
46
|
+
.dp(0)
|
|
47
|
+
.shiftedBy(-params.tokenToReceive.decimals)
|
|
48
|
+
.toFixed();
|
|
49
|
+
amountToReceiveToDisplay = this.overrideAmountToDisplay(params.network, amountToReceiveBn.toString(), params.tokenToReceive);
|
|
50
|
+
amountToUseToDisplay = params.amountToUse;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
throw new Error('AmountToReceive or amountToUse are required, but both aren`t provided');
|
|
54
|
+
}
|
|
55
|
+
const route = [
|
|
56
|
+
{
|
|
57
|
+
assetToUseSymbol: this.overrideToken(params.network, params.tokenToUse).symbol,
|
|
58
|
+
reservesToReceive: params.reservesToReceive,
|
|
59
|
+
assetToReceiveSymbol: this.overrideToken(params.network, params.tokenToReceive).symbol,
|
|
60
|
+
reservesToUse: params.reservesToUse,
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
const { fee, price, priceImpact } = this.createTradeData({
|
|
64
|
+
amountToReceive: amountToReceiveBn,
|
|
65
|
+
amountToUse: amountToUseBn,
|
|
66
|
+
route,
|
|
67
|
+
});
|
|
68
|
+
const priceInverse = this.BN_1.div(price);
|
|
69
|
+
const diffDecimals = params.tokenToUse.decimals - params.tokenToReceive.decimals;
|
|
70
|
+
return {
|
|
71
|
+
amountToReceiveToDisplay,
|
|
72
|
+
amountToUseToDisplay,
|
|
73
|
+
liquidityProviderFee: fee.shiftedBy(-8).plus(this.BN_0).toFixed(4),
|
|
74
|
+
priceImpact: isFinite(Number(priceImpact)) ? priceImpact.shiftedBy(2).toFixed(4) : null,
|
|
75
|
+
priceInverse: isFinite(Number(priceInverse)) ? priceInverse.multipliedBy(Math.pow(10, diffDecimals)).toFixed() : null,
|
|
76
|
+
minimumReceived,
|
|
77
|
+
maximumSelling,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
static overrideToken(network, token) {
|
|
81
|
+
const neoScriptHash = constants_1.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE[network.type].neo;
|
|
82
|
+
const isNeoToken = this.normalizeHash(token.hash) === this.normalizeHash(neoScriptHash);
|
|
83
|
+
if (!isNeoToken) {
|
|
84
|
+
return token;
|
|
85
|
+
}
|
|
86
|
+
const bneoToken = constants_1.TOKENS[network.type].find(token => token.hash === constants_1.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE[network.type].bneo);
|
|
87
|
+
if (!bneoToken)
|
|
88
|
+
throw new Error('Bneo token not found');
|
|
89
|
+
return bneoToken;
|
|
90
|
+
}
|
|
91
|
+
static createTradeData({ amountToUse, amountToReceive, route }) {
|
|
92
|
+
const fee = amountToUse.minus(route.reduce((acc) => acc.times(this.BN_1.minus(this.FEE_RATE)), amountToUse));
|
|
93
|
+
const price = amountToUse.div(amountToReceive);
|
|
94
|
+
const midPrice = route.reduce((acc, item) => acc.times(item.reservesToUse).div(item.reservesToReceive), this.BN_1);
|
|
95
|
+
const priceImpact = price.minus(midPrice).div(price).minus(fee.div(amountToUse));
|
|
96
|
+
return {
|
|
97
|
+
fee,
|
|
98
|
+
price,
|
|
99
|
+
priceImpact,
|
|
100
|
+
midPrice,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
static overrideAmountInput(network, amount, token) {
|
|
104
|
+
const tokenOverrode = this.overrideToken(network, token);
|
|
105
|
+
return new bignumber_js_1.default(amount).shiftedBy(tokenOverrode.decimals);
|
|
106
|
+
}
|
|
107
|
+
static overrideAmountToDisplay(network, amount, token) {
|
|
108
|
+
const tokenOverrode = this.overrideToken(network, token);
|
|
109
|
+
return new bignumber_js_1.default(amount).shiftedBy(-tokenOverrode.decimals).toFixed();
|
|
110
|
+
}
|
|
111
|
+
static normalizeHash(hash) {
|
|
112
|
+
return hash.startsWith('0x') ? hash.slice(2) : hash;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.FlamingoSwapHelper = FlamingoSwapHelper;
|
|
116
|
+
FlamingoSwapHelper.BN_0 = new bignumber_js_1.default(0);
|
|
117
|
+
FlamingoSwapHelper.BN_1 = new bignumber_js_1.default(1);
|
|
118
|
+
FlamingoSwapHelper.BN_997 = new bignumber_js_1.default(997);
|
|
119
|
+
FlamingoSwapHelper.BN_1000 = new bignumber_js_1.default(1000);
|
|
120
|
+
FlamingoSwapHelper.FEE_RATE = new bignumber_js_1.default(0.003);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ContractInvocationMulti } from '@cityofzion/neon-dappkit-types';
|
|
2
|
+
import { Network, SwapControllerServiceSwapToUseArgs, SwapControllerServiceSwapToReceiveArgs } from '@cityofzion/blockchain-service';
|
|
3
|
+
type GetReservesArgs = {
|
|
4
|
+
network: Network;
|
|
5
|
+
tokenToReceiveScriptHash: string;
|
|
6
|
+
tokenToUseScriptHash: string;
|
|
7
|
+
};
|
|
8
|
+
export declare class FlamingoSwapNeonDappKitInvocationBuilder {
|
|
9
|
+
static swapInvocation(data: SwapControllerServiceSwapToReceiveArgs | SwapControllerServiceSwapToUseArgs): ContractInvocationMulti;
|
|
10
|
+
static getReservesInvocation({ network, tokenToReceiveScriptHash, tokenToUseScriptHash, }: GetReservesArgs): ContractInvocationMulti;
|
|
11
|
+
private static swapTokenToReceiveForTokenToUseInvocation;
|
|
12
|
+
private static swapTokenToUseForTokenToReceiveInvocation;
|
|
13
|
+
private static transferContractInvocation;
|
|
14
|
+
private static allowedContractsSwap;
|
|
15
|
+
private static allowedContractsTransfer;
|
|
16
|
+
private static overrideScriptHash;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlamingoSwapNeonDappKitInvocationBuilder = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const neon_core_1 = require("@cityofzion/neon-core");
|
|
6
|
+
const FlamingoSwapHelper_1 = require("./FlamingoSwapHelper");
|
|
7
|
+
class FlamingoSwapNeonDappKitInvocationBuilder {
|
|
8
|
+
static swapInvocation(data) {
|
|
9
|
+
return data.type === 'swapTokenToReceive'
|
|
10
|
+
? this.swapTokenToReceiveForTokenToUseInvocation(data)
|
|
11
|
+
: this.swapTokenToUseForTokenToReceiveInvocation(data);
|
|
12
|
+
}
|
|
13
|
+
static getReservesInvocation({ network, tokenToReceiveScriptHash, tokenToUseScriptHash, }) {
|
|
14
|
+
const flamingoSwapRouter = constants_1.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE[network.type].flamingoSwapRouter;
|
|
15
|
+
return {
|
|
16
|
+
invocations: [
|
|
17
|
+
{
|
|
18
|
+
scriptHash: flamingoSwapRouter,
|
|
19
|
+
operation: 'getReserves',
|
|
20
|
+
args: [
|
|
21
|
+
{
|
|
22
|
+
type: 'Hash160',
|
|
23
|
+
value: this.overrideScriptHash(network, tokenToReceiveScriptHash),
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type: 'Hash160',
|
|
27
|
+
value: this.overrideScriptHash(network, tokenToUseScriptHash),
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
signers: [
|
|
33
|
+
{
|
|
34
|
+
scopes: 1,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
static swapTokenToReceiveForTokenToUseInvocation({ address, amountToReceive, maximumSelling, tokenToReceive, tokenToUse, deadline, network, }) {
|
|
40
|
+
const invocations = [];
|
|
41
|
+
const allowedContracts = [];
|
|
42
|
+
const scriptHashes = constants_1.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE[network.type];
|
|
43
|
+
const tokenToReceiveOverrode = FlamingoSwapHelper_1.FlamingoSwapHelper.overrideToken(network, tokenToReceive);
|
|
44
|
+
const amountToReceiveFormatted = neon_core_1.u.BigInteger.fromDecimal(Number(amountToReceive), tokenToReceiveOverrode.decimals).toString();
|
|
45
|
+
const maximumSellingFormatted = neon_core_1.u.BigInteger.fromDecimal(Number(maximumSelling), tokenToUse.decimals).toString();
|
|
46
|
+
invocations.push({
|
|
47
|
+
scriptHash: scriptHashes.flamingoSwapRouter,
|
|
48
|
+
operation: 'swapTokenOutForTokenIn',
|
|
49
|
+
args: [
|
|
50
|
+
{
|
|
51
|
+
type: 'Hash160',
|
|
52
|
+
value: address,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: 'Integer',
|
|
56
|
+
value: amountToReceiveFormatted,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: 'Integer',
|
|
60
|
+
value: maximumSellingFormatted,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
type: 'Array',
|
|
64
|
+
value: [
|
|
65
|
+
{
|
|
66
|
+
type: 'Hash160',
|
|
67
|
+
value: tokenToUse.hash,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
type: 'Hash160',
|
|
71
|
+
value: tokenToReceiveOverrode.hash,
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
type: 'Integer',
|
|
77
|
+
value: deadline,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
const isNeoSwapped = tokenToReceive.hash === scriptHashes.neo;
|
|
82
|
+
if (isNeoSwapped) {
|
|
83
|
+
const amountToUseInTransferFormatted = neon_core_1.u.BigInteger.fromNumber(Number(amountToReceiveFormatted) * constants_1.GAS_PER_NEO).toString();
|
|
84
|
+
const transferContractInvocation = this.transferContractInvocation({
|
|
85
|
+
address,
|
|
86
|
+
amountToUse: amountToUseInTransferFormatted,
|
|
87
|
+
tokenToUseScriptHash: scriptHashes.bneo,
|
|
88
|
+
contractScriptHash: scriptHashes.gas,
|
|
89
|
+
});
|
|
90
|
+
invocations.push(transferContractInvocation);
|
|
91
|
+
allowedContracts.push(...this.allowedContractsTransfer(scriptHashes));
|
|
92
|
+
}
|
|
93
|
+
allowedContracts.push(...this.allowedContractsSwap(scriptHashes, tokenToReceive.hash, tokenToUse.hash));
|
|
94
|
+
return {
|
|
95
|
+
invocations,
|
|
96
|
+
signers: [
|
|
97
|
+
{
|
|
98
|
+
scopes: neon_core_1.tx.WitnessScope.CustomContracts,
|
|
99
|
+
allowedContracts,
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
static swapTokenToUseForTokenToReceiveInvocation({ address, amountToUse, deadline, tokenToReceive, tokenToUse, minimumReceived, network, }) {
|
|
105
|
+
const invocations = [];
|
|
106
|
+
const allowedContracts = [];
|
|
107
|
+
const scriptHashes = constants_1.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE[network.type];
|
|
108
|
+
const isNeoSwapped = tokenToUse.hash === scriptHashes.neo;
|
|
109
|
+
if (isNeoSwapped) {
|
|
110
|
+
const transferContractInvocation = this.transferContractInvocation({
|
|
111
|
+
address,
|
|
112
|
+
amountToUse,
|
|
113
|
+
tokenToUseScriptHash: scriptHashes.bneo,
|
|
114
|
+
contractScriptHash: scriptHashes.neo,
|
|
115
|
+
});
|
|
116
|
+
invocations.push(transferContractInvocation);
|
|
117
|
+
allowedContracts.push(...this.allowedContractsTransfer(scriptHashes));
|
|
118
|
+
}
|
|
119
|
+
const tokenToUseOverrode = FlamingoSwapHelper_1.FlamingoSwapHelper.overrideToken(network, tokenToUse);
|
|
120
|
+
const amountToUseFormatted = neon_core_1.u.BigInteger.fromDecimal(Number(amountToUse), tokenToUseOverrode.decimals).toString();
|
|
121
|
+
const minimumReceivedFormatted = neon_core_1.u.BigInteger.fromDecimal(Number(minimumReceived), tokenToReceive.decimals).toString();
|
|
122
|
+
invocations.push({
|
|
123
|
+
scriptHash: scriptHashes.flamingoSwapRouter,
|
|
124
|
+
operation: 'swapTokenInForTokenOut',
|
|
125
|
+
args: [
|
|
126
|
+
{
|
|
127
|
+
type: 'Hash160',
|
|
128
|
+
value: address,
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
type: 'Integer',
|
|
132
|
+
value: amountToUseFormatted,
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
type: 'Integer',
|
|
136
|
+
value: minimumReceivedFormatted,
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
type: 'Array',
|
|
140
|
+
value: [
|
|
141
|
+
{
|
|
142
|
+
type: 'Hash160',
|
|
143
|
+
value: tokenToUseOverrode.hash,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
type: 'Hash160',
|
|
147
|
+
value: tokenToReceive.hash,
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
type: 'Integer',
|
|
153
|
+
value: deadline,
|
|
154
|
+
},
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
allowedContracts.push(...this.allowedContractsSwap(scriptHashes, tokenToReceive.hash, tokenToUse.hash));
|
|
158
|
+
return {
|
|
159
|
+
invocations,
|
|
160
|
+
signers: [
|
|
161
|
+
{
|
|
162
|
+
scopes: neon_core_1.tx.WitnessScope.CustomContracts,
|
|
163
|
+
allowedContracts,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
static transferContractInvocation({ address, amountToUse, contractScriptHash, tokenToUseScriptHash, }) {
|
|
169
|
+
return {
|
|
170
|
+
scriptHash: contractScriptHash,
|
|
171
|
+
operation: 'transfer',
|
|
172
|
+
args: [
|
|
173
|
+
{
|
|
174
|
+
type: 'Hash160',
|
|
175
|
+
value: address,
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
type: 'Hash160',
|
|
179
|
+
value: tokenToUseScriptHash,
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
type: 'Integer',
|
|
183
|
+
value: amountToUse,
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
type: 'Any',
|
|
187
|
+
value: null,
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
static allowedContractsSwap(scriptHashes, tokenInScriptHash, tokenOutScriptHash) {
|
|
193
|
+
return [
|
|
194
|
+
scriptHashes.flamingoSwapRouter,
|
|
195
|
+
scriptHashes.flamingoFactory,
|
|
196
|
+
scriptHashes.flamingoPairWhiteList,
|
|
197
|
+
tokenInScriptHash,
|
|
198
|
+
tokenOutScriptHash,
|
|
199
|
+
];
|
|
200
|
+
}
|
|
201
|
+
static allowedContractsTransfer(scriptHashes) {
|
|
202
|
+
return [scriptHashes.gas, scriptHashes.flpBneoGas, scriptHashes.bneo];
|
|
203
|
+
}
|
|
204
|
+
static overrideScriptHash(network, scriptHash) {
|
|
205
|
+
return scriptHash === constants_1.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE[network.type].neo
|
|
206
|
+
? constants_1.SWAP_SCRIPT_HASHES_BY_NETWORK_TYPE[network.type].bneo
|
|
207
|
+
: scriptHash;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.FlamingoSwapNeonDappKitInvocationBuilder = FlamingoSwapNeonDappKitInvocationBuilder;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
export * from './DoraBDSNeo3';
|
|
2
|
-
export * from './RpcBDSNeo3';
|
|
3
1
|
export * from './BSNeo3';
|
|
4
2
|
export * from './constants';
|
|
3
|
+
export * from './DoraBDSNeo3';
|
|
4
|
+
export * from './DoraESNeo3';
|
|
5
5
|
export * from './FlamingoEDSNeo3';
|
|
6
6
|
export * from './GhostMarketNDSNeo3';
|
|
7
|
-
export * from './RpcNDSNeo3';
|
|
8
7
|
export * from './LedgerServiceNeo3';
|
|
8
|
+
export * from './RpcBDSNeo3';
|
|
9
|
+
export * from './RpcNDSNeo3';
|
|
10
|
+
export * from './flamingo-swap/FlamingoSwapControllerService';
|
|
11
|
+
export * from './flamingo-swap/FlamingoSwapError';
|
|
12
|
+
export * from './flamingo-swap/FlamingoSwapHelper';
|
|
13
|
+
export * from './flamingo-swap/FlamingoSwapNeonDappKitInvocationBuilder';
|
package/dist/index.js
CHANGED
|
@@ -14,11 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./DoraBDSNeo3"), exports);
|
|
18
|
-
__exportStar(require("./RpcBDSNeo3"), exports);
|
|
19
17
|
__exportStar(require("./BSNeo3"), exports);
|
|
20
18
|
__exportStar(require("./constants"), exports);
|
|
19
|
+
__exportStar(require("./DoraBDSNeo3"), exports);
|
|
20
|
+
__exportStar(require("./DoraESNeo3"), exports);
|
|
21
21
|
__exportStar(require("./FlamingoEDSNeo3"), exports);
|
|
22
22
|
__exportStar(require("./GhostMarketNDSNeo3"), exports);
|
|
23
|
-
__exportStar(require("./RpcNDSNeo3"), exports);
|
|
24
23
|
__exportStar(require("./LedgerServiceNeo3"), exports);
|
|
24
|
+
__exportStar(require("./RpcBDSNeo3"), exports);
|
|
25
|
+
__exportStar(require("./RpcNDSNeo3"), exports);
|
|
26
|
+
__exportStar(require("./flamingo-swap/FlamingoSwapControllerService"), exports);
|
|
27
|
+
__exportStar(require("./flamingo-swap/FlamingoSwapError"), exports);
|
|
28
|
+
__exportStar(require("./flamingo-swap/FlamingoSwapHelper"), exports);
|
|
29
|
+
__exportStar(require("./flamingo-swap/FlamingoSwapNeonDappKitInvocationBuilder"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cityofzion/bs-neo3",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"repository": "https://github.com/CityOfZion/blockchain-services",
|
|
@@ -19,8 +19,11 @@
|
|
|
19
19
|
"@cityofzion/neon-dappkit": "0.4.1",
|
|
20
20
|
"@cityofzion/neon-dappkit-types": "~0.3.1",
|
|
21
21
|
"@ledgerhq/hw-transport-node-hid": "~6.28.5",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
22
|
+
"bignumber.js": "~9.1.2",
|
|
23
|
+
"typed-emitter": "~2.1.0",
|
|
24
|
+
"isomorphic-ws": "~5.0.0",
|
|
25
|
+
"@cityofzion/blockchain-service": "0.13.0",
|
|
26
|
+
"@cityofzion/bs-asteroid-sdk": "0.9.0"
|
|
24
27
|
},
|
|
25
28
|
"devDependencies": {
|
|
26
29
|
"@types/jest": "29.5.3",
|