@cityofzion/bs-neo-legacy 1.9.1 → 1.10.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.
@@ -18,4 +18,7 @@ export declare class BSNeoLegacyConstants {
18
18
  static ALL_NETWORKS: Network<BSNeoLegacyNetworkId>[];
19
19
  static DEFAULT_BIP44_DERIVATION_PATH: string;
20
20
  static DEFAULT_NETWORK: Network<BSNeoLegacyNetworkId>;
21
+ static readonly MIGRATION_NEO3_COZ_ADDRESS = "AaT27thuyPaqERPwERhk7QhfKrbj4xoyAV";
22
+ static readonly MIGRATION_NEO3_COZ_FEE = 0.01;
23
+ static readonly MIGRATION_NEO3_NEP_17_TRANSFER_FEE = 0.0112143;
21
24
  }
@@ -58,3 +58,6 @@ BSNeoLegacyConstants.TESTNET_NETWORKS = [
58
58
  BSNeoLegacyConstants.ALL_NETWORKS = [..._a.MAINNET_NETWORKS, ..._a.TESTNET_NETWORKS];
59
59
  BSNeoLegacyConstants.DEFAULT_BIP44_DERIVATION_PATH = "m/44'/888'/0'/0/?";
60
60
  BSNeoLegacyConstants.DEFAULT_NETWORK = _a.MAINNET_NETWORKS[0];
61
+ BSNeoLegacyConstants.MIGRATION_NEO3_COZ_ADDRESS = 'AaT27thuyPaqERPwERhk7QhfKrbj4xoyAV';
62
+ BSNeoLegacyConstants.MIGRATION_NEO3_COZ_FEE = 0.01; // 1%
63
+ BSNeoLegacyConstants.MIGRATION_NEO3_NEP_17_TRANSFER_FEE = 0.0112143;
@@ -1,8 +1,11 @@
1
- import { Account, BDSClaimable, BlockchainDataService, BlockchainService, BSClaimable, ExchangeDataService, Token, Network, TransferParam, BSWithExplorerService, ExplorerService, BSWithLedger, GetLedgerTransport } from '@cityofzion/blockchain-service';
1
+ import { Account, BDSClaimable, BlockchainDataService, BlockchainService, BSClaimable, ExchangeDataService, Token, Network, TransferParam, BSWithExplorerService, ExplorerService, BSWithLedger, GetLedgerTransport, BSMigrationNeo3, MigrateToNeo3Params, CalculateToMigrateToNeo3ValuesResponse, CalculateToMigrateToNeo3ValuesParams } from '@cityofzion/blockchain-service';
2
2
  import { BSNeoLegacyNetworkId } from '../constants/BSNeoLegacyConstants';
3
3
  import { NeonJsLedgerServiceNeoLegacy } from './ledger/NeonJsLedgerServiceNeoLegacy';
4
- export declare class BSNeoLegacy<BSName extends string = string> implements BlockchainService<BSName, BSNeoLegacyNetworkId>, BSClaimable<BSName>, BSWithExplorerService, BSWithLedger<BSName> {
4
+ export declare class BSNeoLegacy<BSName extends string = string> implements BlockchainService<BSName, BSNeoLegacyNetworkId>, BSClaimable<BSName>, BSWithExplorerService, BSWithLedger<BSName>, BSMigrationNeo3<BSName> {
5
5
  #private;
6
+ private readonly NATIVE_ASSETS;
7
+ private readonly GAS_ASSET;
8
+ private readonly NEO_ASSET;
6
9
  readonly name: BSName;
7
10
  readonly bip44DerivationPath: string;
8
11
  feeToken: Token;
@@ -28,4 +31,10 @@ export declare class BSNeoLegacy<BSName extends string = string> implements Bloc
28
31
  encrypt(key: string, password: string): Promise<string>;
29
32
  transfer({ intents, senderAccount, tipIntent, ...params }: TransferParam<BSName>): Promise<string[]>;
30
33
  claim(account: Account): Promise<string>;
34
+ /**
35
+ * Reference: https://github.com/CityOfZion/legacy-n3-swap-service/blob/master/policy/policy.go
36
+ */
37
+ calculateToMigrateToNeo3Values(params: CalculateToMigrateToNeo3ValuesParams<BSName>): Promise<CalculateToMigrateToNeo3ValuesResponse>;
38
+ migrateToNeo3({ account, address }: MigrateToNeo3Params<BSName>): Promise<string[]>;
39
+ private getMigrationNeo3ValidatedData;
31
40
  }
@@ -27,6 +27,7 @@ var __rest = (this && this.__rest) || function (s, e) {
27
27
  var _BSNeoLegacy_instances, _BSNeoLegacy_generateSigningCallback, _BSNeoLegacy_setTokens;
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.BSNeoLegacy = void 0;
30
+ const blockchain_service_1 = require("@cityofzion/blockchain-service");
30
31
  const neon_js_1 = require("@cityofzion/neon-js");
31
32
  const bs_asteroid_sdk_1 = require("@cityofzion/bs-asteroid-sdk");
32
33
  const BSNeoLegacyConstants_1 = require("../constants/BSNeoLegacyConstants");
@@ -38,6 +39,9 @@ const NeonJsLedgerServiceNeoLegacy_1 = require("./ledger/NeonJsLedgerServiceNeoL
38
39
  class BSNeoLegacy {
39
40
  constructor(name, network, getLedgerTransport) {
40
41
  _BSNeoLegacy_instances.add(this);
42
+ this.NATIVE_ASSETS = BSNeoLegacyConstants_1.BSNeoLegacyConstants.NATIVE_ASSETS.map(asset => (Object.assign(Object.assign({}, asset), { hash: (0, blockchain_service_1.normalizeHash)(asset.hash) })));
43
+ this.GAS_ASSET = this.NATIVE_ASSETS.find(({ symbol }) => symbol === 'GAS');
44
+ this.NEO_ASSET = this.NATIVE_ASSETS.find(({ symbol }) => symbol === 'NEO');
41
45
  network = network !== null && network !== void 0 ? network : BSNeoLegacyConstants_1.BSNeoLegacyConstants.DEFAULT_NETWORK;
42
46
  this.name = name;
43
47
  this.legacyNetwork = BSNeoLegacyConstants_1.BSNeoLegacyConstants.LEGACY_NETWORK_BY_NETWORK_ID[network.id];
@@ -116,7 +120,7 @@ class BSNeoLegacy {
116
120
  const concatIntents = [...intents, ...(tipIntent ? [tipIntent] : [])];
117
121
  for (const intent of concatIntents) {
118
122
  const tokenHashFixed = BSNeoLegacyHelper_1.BSNeoLegacyHelper.normalizeHash(intent.tokenHash);
119
- const nativeAsset = BSNeoLegacyConstants_1.BSNeoLegacyConstants.NATIVE_ASSETS.find(asset => BSNeoLegacyHelper_1.BSNeoLegacyHelper.normalizeHash(asset.hash) === tokenHashFixed);
123
+ const nativeAsset = this.NATIVE_ASSETS.find(({ hash }) => hash === tokenHashFixed);
120
124
  if (nativeAsset) {
121
125
  nativeIntents.push(...neon_js_1.api.makeIntent({ [nativeAsset.symbol]: Number(intent.amount) }, intent.receiverAddress));
122
126
  continue;
@@ -180,6 +184,86 @@ class BSNeoLegacy {
180
184
  return response.tx.hash;
181
185
  });
182
186
  }
187
+ /**
188
+ * Reference: https://github.com/CityOfZion/legacy-n3-swap-service/blob/master/policy/policy.go
189
+ */
190
+ calculateToMigrateToNeo3Values(params) {
191
+ return __awaiter(this, void 0, void 0, function* () {
192
+ const { gasAmountNumber, neoAmountNumber } = yield this.getMigrationNeo3ValidatedData(params);
193
+ const response = {};
194
+ if (gasAmountNumber) {
195
+ // Two transfers fee and one transfer fee left over
196
+ const allNep17TransfersFee = BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEO3_NEP_17_TRANSFER_FEE * 3;
197
+ // Necessary to calculate the COZ fee
198
+ const gasAmountNumberLessAllNep17TransfersFee = gasAmountNumber - allNep17TransfersFee;
199
+ // Example: ~0.06635710 * 0.01 = ~0.00066357
200
+ const cozFee = gasAmountNumberLessAllNep17TransfersFee * BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEO3_COZ_FEE;
201
+ // Example: ~0.06635710 - ~0.00066357 = ~0.06569352
202
+ const gasAmountNumberLessCozFee = gasAmountNumberLessAllNep17TransfersFee - cozFee;
203
+ const allGasFeeNumberThatUserWillPay = cozFee + BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEO3_NEP_17_TRANSFER_FEE * 2;
204
+ const allGasAmountNumberThatUserWillReceive = gasAmountNumberLessCozFee + BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEO3_NEP_17_TRANSFER_FEE;
205
+ response.gasMigrationTotalFees = (0, blockchain_service_1.formatNumber)(allGasFeeNumberThatUserWillPay, this.GAS_ASSET.decimals);
206
+ response.gasMigrationAmount = (0, blockchain_service_1.formatNumber)(allGasAmountNumberThatUserWillReceive, this.GAS_ASSET.decimals);
207
+ }
208
+ if (neoAmountNumber) {
209
+ response.neoMigrationTotalFees = (0, blockchain_service_1.formatNumber)(Math.ceil(neoAmountNumber * BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEO3_COZ_FEE), this.NEO_ASSET.decimals);
210
+ response.neoMigrationAmount = (0, blockchain_service_1.formatNumber)(neoAmountNumber - Number(response.neoMigrationTotalFees), this.NEO_ASSET.decimals);
211
+ }
212
+ return response;
213
+ });
214
+ }
215
+ migrateToNeo3({ account, address }) {
216
+ return __awaiter(this, void 0, void 0, function* () {
217
+ if (!address)
218
+ throw new Error('Must have address');
219
+ const { gasAmountNumber, neoAmountNumber } = yield this.getMigrationNeo3ValidatedData({ account });
220
+ const { neonJsAccount, signingCallback } = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_generateSigningCallback).call(this, account);
221
+ const provider = new neon_js_1.api.neoCli.instance(this.network.url);
222
+ const intents = [];
223
+ if (gasAmountNumber)
224
+ intents.push(...neon_js_1.api.makeIntent({ [this.GAS_ASSET.symbol]: gasAmountNumber }, BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEO3_COZ_ADDRESS));
225
+ if (neoAmountNumber)
226
+ intents.push(...neon_js_1.api.makeIntent({ [this.NEO_ASSET.symbol]: neoAmountNumber }, BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEO3_COZ_ADDRESS));
227
+ const response = yield neon_js_1.api.sendAsset({
228
+ url: this.network.url,
229
+ api: provider,
230
+ account: neonJsAccount,
231
+ intents,
232
+ fees: 0,
233
+ signingFunction: signingCallback,
234
+ override: {
235
+ attributes: [
236
+ new neon_js_1.tx.TransactionAttribute({
237
+ usage: neon_js_1.tx.TxAttrUsage.Remark14,
238
+ data: neon_js_1.u.str2hexstring(address),
239
+ }),
240
+ new neon_js_1.tx.TransactionAttribute({
241
+ usage: neon_js_1.tx.TxAttrUsage.Remark15,
242
+ data: neon_js_1.u.str2hexstring('Neon Desktop Migration'),
243
+ }),
244
+ ],
245
+ },
246
+ });
247
+ if (!response.tx)
248
+ throw new Error('Migration failed on send');
249
+ return intents.map(() => response.tx.hash);
250
+ });
251
+ }
252
+ getMigrationNeo3ValidatedData({ account }) {
253
+ var _a, _b;
254
+ return __awaiter(this, void 0, void 0, function* () {
255
+ if (!BSNeoLegacyHelper_1.BSNeoLegacyHelper.isMainnet(this.network))
256
+ throw new Error('Must use Mainnet network');
257
+ const balances = yield this.blockchainDataService.getBalance(account.address);
258
+ const gasAmount = (_a = balances.find(({ token }) => (0, blockchain_service_1.normalizeHash)(token.hash) === this.GAS_ASSET.hash)) === null || _a === void 0 ? void 0 : _a.amount;
259
+ const neoAmount = (_b = balances.find(({ token }) => (0, blockchain_service_1.normalizeHash)(token.hash) === this.NEO_ASSET.hash)) === null || _b === void 0 ? void 0 : _b.amount;
260
+ const gasAmountNumber = Number(gasAmount) || 0;
261
+ const neoAmountNumber = Number(neoAmount) || 0;
262
+ if (gasAmountNumber < 0.1 && neoAmountNumber < 2)
263
+ throw new Error('Must have at least 0.1 GAS or 2 NEO');
264
+ return { gasAmountNumber, neoAmountNumber };
265
+ });
266
+ }
183
267
  }
184
268
  exports.BSNeoLegacy = BSNeoLegacy;
185
269
  _BSNeoLegacy_instances = new WeakSet(), _BSNeoLegacy_generateSigningCallback = function _BSNeoLegacy_generateSigningCallback(account) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cityofzion/bs-neo-legacy",
3
- "version": "1.9.1",
3
+ "version": "1.10.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "Coz",
@@ -12,7 +12,7 @@
12
12
  "@cityofzion/dora-ts": "0.0.11",
13
13
  "@cityofzion/neon-js": "4.8.3",
14
14
  "@ledgerhq/hw-transport": "~6.30.5",
15
- "@cityofzion/blockchain-service": "1.15.1",
15
+ "@cityofzion/blockchain-service": "1.15.2",
16
16
  "@cityofzion/bs-asteroid-sdk": "0.9.0"
17
17
  },
18
18
  "devDependencies": {