@cityofzion/blockchain-service 1.6.0 → 1.8.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.
@@ -1,15 +1,14 @@
1
1
  import { Account, BlockchainService } from './interfaces';
2
- export declare class BSAggregator<BSCustomName extends string = string, BSCustom extends BlockchainService<BSCustomName, string> = BlockchainService<BSCustomName, string>> {
2
+ export declare class BSAggregator<BSName extends string = string> {
3
3
  #private;
4
- readonly blockchainServicesByName: Record<BSCustomName, BSCustom>;
5
- constructor(blockchainServices: Record<BSCustomName, BSCustom>);
6
- addBlockchain(name: BSCustomName, blockchain: BSCustom): void;
4
+ readonly blockchainServicesByName: Record<BSName, BlockchainService<BSName>>;
5
+ constructor(blockchainServices: BlockchainService<BSName>[]);
7
6
  validateAddressAllBlockchains(address: string): boolean;
8
7
  validateTextAllBlockchains(text: string): boolean;
9
8
  validateKeyAllBlockchains(wif: string): boolean;
10
9
  validateEncryptedAllBlockchains(keyOrJson: string): boolean;
11
- getBlockchainNameByAddress(address: string): BSCustomName[];
12
- getBlockchainNameByKey(wif: string): BSCustomName[];
13
- getBlockchainNameByEncrypted(keyOrJson: string): BSCustomName[];
14
- generateAccountsFromMnemonic(mnemonic: string): Promise<Map<BSCustomName, Account[]>>;
10
+ getBlockchainNameByAddress(address: string): BSName[];
11
+ getBlockchainNameByKey(wif: string): BSName[];
12
+ getBlockchainNameByEncrypted(keyOrJson: string): BSName[];
13
+ generateAccountsFromMnemonic(mnemonic: string): Promise<Map<BSName, Account<BSName>[]>>;
15
14
  }
@@ -26,14 +26,14 @@ const functions_1 = require("./functions");
26
26
  class BSAggregator {
27
27
  constructor(blockchainServices) {
28
28
  _BSAggregator_blockchainServices.set(this, void 0);
29
- this.blockchainServicesByName = blockchainServices;
30
- __classPrivateFieldSet(this, _BSAggregator_blockchainServices, Object.values(blockchainServices), "f");
31
- }
32
- addBlockchain(name, blockchain) {
33
- if (this.blockchainServicesByName[name])
34
- throw new Error(`The blockchain ${name} already exist`);
35
- this.blockchainServicesByName[name] = blockchain;
36
- __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").push(blockchain);
29
+ __classPrivateFieldSet(this, _BSAggregator_blockchainServices, blockchainServices, "f");
30
+ this.blockchainServicesByName = blockchainServices.reduce((acc, service) => {
31
+ if (acc[service.name]) {
32
+ throw new Error(`Duplicate blockchain service name: ${service.name}`);
33
+ }
34
+ acc[service.name] = service;
35
+ return acc;
36
+ }, {});
37
37
  }
38
38
  validateAddressAllBlockchains(address) {
39
39
  return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").some(bs => bs.validateAddress(address));
@@ -48,13 +48,13 @@ class BSAggregator {
48
48
  return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").some(bs => bs.validateEncrypted(keyOrJson));
49
49
  }
50
50
  getBlockchainNameByAddress(address) {
51
- return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").filter(bs => bs.validateAddress(address)).map(bs => bs.blockchainName);
51
+ return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").filter(bs => bs.validateAddress(address)).map(bs => bs.name);
52
52
  }
53
53
  getBlockchainNameByKey(wif) {
54
- return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").filter(bs => bs.validateKey(wif)).map(bs => bs.blockchainName);
54
+ return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").filter(bs => bs.validateKey(wif)).map(bs => bs.name);
55
55
  }
56
56
  getBlockchainNameByEncrypted(keyOrJson) {
57
- return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").filter(bs => bs.validateEncrypted(keyOrJson)).map(bs => bs.blockchainName);
57
+ return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").filter(bs => bs.validateEncrypted(keyOrJson)).map(bs => bs.name);
58
58
  }
59
59
  generateAccountsFromMnemonic(mnemonic) {
60
60
  return __awaiter(this, void 0, void 0, function* () {
@@ -72,7 +72,7 @@ class CryptoCompareEDS {
72
72
  }
73
73
  getCurrencyRatio(currency) {
74
74
  return __awaiter(this, void 0, void 0, function* () {
75
- const { data } = yield axios_1.default.get(`https://api.flamingo.finance/fiat/exchange-rate?pair=USD_${currency}`);
75
+ const { data } = yield axios_1.default.get(`https://neo-api.b-cdn.net/flamingo/live-data/fiat-exchange-rate/USD_${currency}`);
76
76
  return data;
77
77
  });
78
78
  }
@@ -1,10 +1,9 @@
1
- import { Account, BlockchainService, BSCalculableFee, BSClaimable, BSWithExplorerService, BSWithLedger, BSWithNameService, BSWithNft, BSWithSwap } from './interfaces';
2
- export declare function hasNameService(service: BlockchainService): service is BlockchainService & BSWithNameService;
3
- export declare function isClaimable(service: BlockchainService): service is BlockchainService & BSClaimable;
4
- export declare function isCalculableFee(service: BlockchainService): service is BlockchainService & BSCalculableFee;
5
- export declare function hasNft(service: BlockchainService): service is BlockchainService & BSWithNft;
6
- export declare function hasExplorerService(service: BlockchainService): service is BlockchainService & BSWithExplorerService;
7
- export declare function hasLedger(service: BlockchainService): service is BlockchainService & BSWithLedger;
8
- export declare function hasSwap(service: BlockchainService): service is BlockchainService & BSWithSwap;
9
- export declare function waitForTransaction(service: BlockchainService, txId: string): Promise<boolean>;
10
- export declare function fetchAccountsForBlockchainServices<BSCustomName extends string = string>(blockchainServices: BlockchainService<BSCustomName>[], getAccountCallback: (service: BlockchainService<BSCustomName>, index: number) => Promise<Account>): Promise<Map<BSCustomName, Account[]>>;
1
+ import { Account, BlockchainService, BSCalculableFee, BSClaimable, BSWithExplorerService, BSWithLedger, BSWithNameService, BSWithNft } from './interfaces';
2
+ export declare function hasNameService<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSWithNameService;
3
+ export declare function isClaimable<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSClaimable<BSName>;
4
+ export declare function isCalculableFee<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSCalculableFee<BSName>;
5
+ export declare function hasNft<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSWithNft;
6
+ export declare function hasExplorerService<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSWithExplorerService;
7
+ export declare function hasLedger<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSWithLedger<BSName>;
8
+ export declare function waitForTransaction<BSName extends string = string>(service: BlockchainService<BSName>, txId: string): Promise<boolean>;
9
+ export declare function fetchAccountsForBlockchainServices<BSName extends string = string>(blockchainServices: BlockchainService<BSName>[], getAccountCallback: (service: BlockchainService<BSName>, index: number) => Promise<Account<BSName>>): Promise<Map<BSName, Account<BSName>[]>>;
package/dist/functions.js CHANGED
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.fetchAccountsForBlockchainServices = exports.waitForTransaction = exports.hasSwap = exports.hasLedger = exports.hasExplorerService = exports.hasNft = exports.isCalculableFee = exports.isClaimable = exports.hasNameService = void 0;
12
+ exports.fetchAccountsForBlockchainServices = exports.waitForTransaction = exports.hasLedger = exports.hasExplorerService = exports.hasNft = exports.isCalculableFee = exports.isClaimable = exports.hasNameService = void 0;
13
13
  function hasNameService(service) {
14
14
  return 'resolveNameServiceDomain' in service && 'validateNameServiceDomainFormat' in service;
15
15
  }
@@ -34,10 +34,6 @@ function hasLedger(service) {
34
34
  return 'ledgerService' in service;
35
35
  }
36
36
  exports.hasLedger = hasLedger;
37
- function hasSwap(service) {
38
- return 'createSwapService' in service;
39
- }
40
- exports.hasSwap = hasSwap;
41
37
  function wait(ms) {
42
38
  return new Promise(resolve => setTimeout(resolve, ms));
43
39
  }
@@ -85,7 +81,7 @@ function fetchAccountsForBlockchainServices(blockchainServices, getAccountCallba
85
81
  accounts.push(generatedAccount);
86
82
  index++;
87
83
  }
88
- accountsByBlockchainService.set(service.blockchainName, accounts);
84
+ accountsByBlockchainService.set(service.name, accounts);
89
85
  }));
90
86
  yield Promise.all(promises);
91
87
  return accountsByBlockchainService;
@@ -1,17 +1,19 @@
1
1
  import Transport from '@ledgerhq/hw-transport';
2
2
  import TypedEmitter from 'typed-emitter';
3
- export type Account = {
3
+ export type Account<BSName extends string = string> = {
4
4
  key: string;
5
5
  type: 'wif' | 'privateKey' | 'publicKey';
6
6
  address: string;
7
7
  bip44Path?: string;
8
+ isHardware?: boolean;
9
+ blockchain: BSName;
8
10
  };
9
- export interface Token {
11
+ export type Token = {
10
12
  symbol: string;
11
13
  name: string;
12
14
  hash: string;
13
15
  decimals: number;
14
- }
16
+ };
15
17
  export type NetworkId<T extends string = string> = T | (string & {});
16
18
  export type Network<T extends string = string> = {
17
19
  id: NetworkId<T>;
@@ -24,15 +26,14 @@ export type IntentTransferParam = {
24
26
  amount: string;
25
27
  tokenDecimals?: number;
26
28
  };
27
- export type TransferParam = {
28
- senderAccount: Account;
29
+ export type TransferParam<BSName extends string = string> = {
30
+ senderAccount: Account<BSName>;
29
31
  intents: IntentTransferParam[];
30
32
  tipIntent?: IntentTransferParam;
31
33
  priorityFee?: string;
32
- isLedger?: boolean;
33
34
  };
34
- export interface BlockchainService<BSCustomName extends string = string, BSAvailableNetworks extends string = string> {
35
- readonly blockchainName: BSCustomName;
35
+ export interface BlockchainService<BSName extends string = string, BSAvailableNetworks extends string = string> {
36
+ readonly name: BSName;
36
37
  readonly bip44DerivationPath: string;
37
38
  readonly feeToken: Token;
38
39
  exchangeDataService: ExchangeDataService;
@@ -41,23 +42,23 @@ export interface BlockchainService<BSCustomName extends string = string, BSAvail
41
42
  network: Network<BSAvailableNetworks>;
42
43
  testNetwork: (network: Network<BSAvailableNetworks>) => Promise<void>;
43
44
  setNetwork: (partialNetwork: Network<BSAvailableNetworks>) => void;
44
- generateAccountFromMnemonic(mnemonic: string, index: number): Account;
45
- generateAccountFromKey(key: string): Account;
46
- decrypt(keyOrJson: string, password: string): Promise<Account>;
45
+ generateAccountFromMnemonic(mnemonic: string, index: number): Account<BSName>;
46
+ generateAccountFromKey(key: string): Account<BSName>;
47
+ decrypt(keyOrJson: string, password: string): Promise<Account<BSName>>;
47
48
  encrypt(key: string, password: string): Promise<string>;
48
49
  validateAddress(address: string): boolean;
49
50
  validateEncrypted(keyOrJson: string): boolean;
50
51
  validateKey(key: string): boolean;
51
- transfer(param: TransferParam): Promise<string[]>;
52
+ transfer(param: TransferParam<BSName>): Promise<string[]>;
52
53
  }
53
- export interface BSCalculableFee {
54
- calculateTransferFee(param: TransferParam, details?: boolean): Promise<string>;
54
+ export interface BSCalculableFee<BSName extends string = string> {
55
+ calculateTransferFee(param: TransferParam<BSName>, details?: boolean): Promise<string>;
55
56
  }
56
- export interface BSClaimable {
57
+ export interface BSClaimable<BSName extends string = string> {
57
58
  readonly claimToken: Token;
58
59
  readonly burnToken: Token;
59
60
  blockchainDataService: BlockchainDataService & BDSClaimable;
60
- claim(account: Account, isLedger?: boolean): Promise<string>;
61
+ claim(account: Account<BSName>): Promise<string>;
61
62
  }
62
63
  export interface BSWithNameService {
63
64
  resolveNameServiceDomain(domainName: string): Promise<string>;
@@ -69,12 +70,9 @@ export interface BSWithExplorerService {
69
70
  export interface BSWithNft {
70
71
  nftDataService: NftDataService;
71
72
  }
72
- export interface BSWithLedger {
73
- ledgerService: LedgerService;
74
- generateAccountFromPublicKey(publicKey: string): Account;
75
- }
76
- export interface BSWithSwap<BSAvailableNetworks extends string = string> {
77
- createSwapService(): SwapService<BSAvailableNetworks>;
73
+ export interface BSWithLedger<BSName extends string = string> {
74
+ ledgerService: LedgerService<BSName>;
75
+ generateAccountFromPublicKey(publicKey: string): Account<BSName>;
78
76
  }
79
77
  export type TransactionNotifications = {
80
78
  eventName: string;
@@ -222,65 +220,61 @@ export type LedgerServiceEmitter = TypedEmitter<{
222
220
  getSignatureStart(): void | Promise<void>;
223
221
  getSignatureEnd(): void | Promise<void>;
224
222
  }>;
225
- export type GetLedgerTransport = (account: Account) => Promise<Transport>;
226
- export interface LedgerService {
223
+ export type GetLedgerTransport<BSName extends string = string> = (account: Account<BSName>) => Promise<Transport>;
224
+ export interface LedgerService<BSName extends string = string> {
227
225
  emitter: LedgerServiceEmitter;
228
- getLedgerTransport?: GetLedgerTransport;
229
- getAccounts(transport: Transport): Promise<Account[]>;
230
- getAccount(transport: Transport, index: number): Promise<Account>;
226
+ getLedgerTransport?: GetLedgerTransport<BSName>;
227
+ getAccounts(transport: Transport): Promise<Account<BSName>[]>;
228
+ getAccount(transport: Transport, index: number): Promise<Account<BSName>>;
231
229
  }
232
- export type SwapRoute = {
233
- tokenToUse: Token;
234
- reserveTokenToUse: string;
235
- tokenToReceive: Token;
236
- reserveTokenToReceive: string;
230
+ export type SwapServiceToken<BSName extends string = string> = {
231
+ id: string;
232
+ blockchain?: BSName;
233
+ imageUrl?: string;
234
+ symbol: string;
235
+ name: string;
236
+ hash?: string;
237
+ decimals?: number;
237
238
  };
238
- export type SwapServiceEvents = {
239
- accountToUse: (account: Account | null) => void | Promise<void>;
240
- amountToUse: (amount: string | null) => void | Promise<void>;
241
- tokenToUse: (token: Token | null) => void | Promise<void>;
242
- reservesToUse: (reserves: string | null) => void | Promise<void>;
243
- amountToReceive: (amount: string | null) => void | Promise<void>;
244
- tokenToReceive: (token: Token | null) => void | Promise<void>;
245
- reservesToReceive: (reserves: string | null) => void | Promise<void>;
246
- minimumReceived: (minimumReceived: string | null) => void | Promise<void>;
247
- maximumSelling: (maximumSelling: string | null) => void | Promise<void>;
248
- deadline: (deadline: string) => void | Promise<void>;
249
- slippage: (slippage: number) => void | Promise<void>;
250
- liquidityProviderFee: (liquidityProviderFee: string | null) => void | Promise<void>;
251
- priceImpact: (priceImpact: string | null) => void | Promise<void>;
252
- priceInverse: (priceInverse: string | null) => void | Promise<void>;
253
- route: (route: SwapRoute[] | null) => void | Promise<void>;
254
- lastAmountChanged: (lastAmountChanged: 'amountToUse' | 'amountToReceive' | null) => void | Promise<void>;
239
+ export type SwapServiceLoadableValue<T> = {
240
+ loading: boolean;
241
+ value: T | null;
255
242
  };
256
- export type SwapServiceSwapArgs<T extends string> = {
257
- address: string;
258
- routePath: Token[];
259
- deadline: string;
260
- network: Network<T>;
243
+ export type SwapServiceValidateValue<T> = SwapServiceLoadableValue<T> & {
244
+ valid: boolean | null;
245
+ };
246
+ export type SwapServiceMinMaxAmount = {
247
+ min: string;
248
+ max: string | null;
249
+ };
250
+ export type SwapServiceEvents<BSName extends string = string> = {
251
+ accountToUse: (account: SwapServiceValidateValue<Account<BSName>>) => void | Promise<void>;
252
+ amountToUse: (amount: SwapServiceLoadableValue<string>) => void | Promise<void>;
253
+ amountToUseMinMax: (minMax: SwapServiceLoadableValue<SwapServiceMinMaxAmount>) => void | Promise<void>;
254
+ tokenToUse: (token: SwapServiceLoadableValue<SwapServiceToken<BSName>>) => void | Promise<void>;
255
+ availableTokensToUse: (tokens: SwapServiceLoadableValue<SwapServiceToken<BSName>[]>) => void | Promise<void>;
256
+ addressToReceive: (account: SwapServiceValidateValue<string>) => void | Promise<void>;
257
+ amountToReceive: (amount: SwapServiceLoadableValue<string>) => void | Promise<void>;
258
+ tokenToReceive: (token: SwapServiceLoadableValue<SwapServiceToken<BSName>>) => void | Promise<void>;
259
+ availableTokensToReceive: (tokens: SwapServiceLoadableValue<SwapServiceToken<BSName>[]>) => void | Promise<void>;
260
+ };
261
+ export type SwapServiceSwapResult = {
262
+ transactionHash: string;
263
+ numberOfTransactions: number;
264
+ id: string;
265
+ };
266
+ export type SwapServiceStatusResponse = {
267
+ status: 'finished' | 'confirming' | 'exchanging' | 'failed';
268
+ transactionHashes: string[];
261
269
  };
262
- export type SwapServiceSwapToUseArgs<T extends string> = {
263
- amountToUse: string;
264
- minimumReceived: string;
265
- type: 'swapTokenToUse';
266
- } & SwapServiceSwapArgs<T>;
267
- export type SwapServiceSwapToReceiveArgs<T extends string> = {
268
- amountToReceive: string;
269
- maximumSelling: string;
270
- type: 'swapTokenToReceive';
271
- } & SwapServiceSwapArgs<T>;
272
- export type PoolGraph = Record<string, string[]>;
273
- export interface SwapService<AvailableNetworkIds extends string> {
274
- eventEmitter: TypedEmitter<SwapServiceEvents>;
275
- buildSwapInvocationArgs(): SwapServiceSwapToUseArgs<AvailableNetworkIds> | SwapServiceSwapToReceiveArgs<AvailableNetworkIds>;
276
- setAccountToUse(account: Account | null): void;
277
- setAmountToUse(amount: string | null): void;
278
- setAmountToReceive(amount: string | null): void;
279
- setDeadline(deadline: string): void;
280
- setSlippage(slippage: number): void;
281
- setTokenToUse(token: Token | null): void;
282
- setTokenToReceive(token: Token | null): void;
283
- startListeningBlockGeneration(): void;
284
- stopListeningBlockGeneration(): void;
285
- swap(isLedger?: boolean): void;
270
+ export interface SwapService<BSName extends string = string> {
271
+ eventEmitter: TypedEmitter<SwapServiceEvents<BSName>>;
272
+ setTokenToUse(token: SwapServiceToken<BSName> | null): Promise<void>;
273
+ setAccountToUse(account: Account<BSName> | null): Promise<void>;
274
+ setAmountToUse(amount: string | null): Promise<void>;
275
+ setTokenToReceive(token: SwapServiceToken<BSName> | null): Promise<void>;
276
+ setAddressToReceive(address: string | null): Promise<void>;
277
+ swap(): Promise<SwapServiceSwapResult>;
278
+ calculateFee(): Promise<string>;
279
+ getStatus(): Promise<SwapServiceStatusResponse>;
286
280
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cityofzion/blockchain-service",
3
- "version": "1.6.0",
3
+ "version": "1.8.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": "https://github.com/CityOfZion/blockchain-services",