@cityofzion/blockchain-service 0.1.1

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.
@@ -0,0 +1,16 @@
1
+ import { BlockchainService, Claimable } from "./interfaces";
2
+ export declare class BSAgreggator<BSCustom extends BlockchainService = BlockchainService, BSCustomName extends string = string> {
3
+ readonly blockchainservices: Record<BSCustomName, BSCustom>;
4
+ private bsList;
5
+ constructor(blockchainservices: Record<BSCustomName, BSCustom>);
6
+ private haveBlockchainServices;
7
+ addBlockchain(name: BSCustomName, blockchain: BSCustom): void;
8
+ validateAddressesAllBlockchains(address: string): boolean;
9
+ validateTextAllBlockchains(text: string): boolean;
10
+ validateWifAllBlockchains(wif: string): boolean;
11
+ validateEncryptedKeyAllBlockchains(encryptedKey: string): boolean;
12
+ getBlockchainByAddress(address: string): BlockchainService<BSCustomName> | null;
13
+ getBlockchainByWif(wif: string): BlockchainService<BSCustomName> | null;
14
+ getBlockchainByEncryptedKey(encryptedKey: string): BlockchainService<BSCustomName> | null;
15
+ getBlockchainsClaimable(): (BlockchainService<BSCustomName> & Claimable)[];
16
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BSAgreggator = void 0;
4
+ const excpetions_1 = require("./excpetions");
5
+ class BSAgreggator {
6
+ constructor(blockchainservices) {
7
+ this.blockchainservices = blockchainservices;
8
+ this.bsList = Object.values(blockchainservices);
9
+ }
10
+ haveBlockchainServices() {
11
+ const blockchainservices = Object.values(this.blockchainservices);
12
+ const rules = [
13
+ Object.keys(blockchainservices).length > 1,
14
+ Object.values(blockchainservices).length > 1,
15
+ ];
16
+ return rules.every(rule => rule === true);
17
+ }
18
+ addBlockchain(name, blockchain) {
19
+ if (this.blockchainservices[name])
20
+ excpetions_1.exception.blockchainAlreadyExist(name);
21
+ this.blockchainservices[name] = blockchain;
22
+ this.bsList = Object.values(this.blockchainservices);
23
+ }
24
+ validateAddressesAllBlockchains(address) {
25
+ if (this.haveBlockchainServices())
26
+ excpetions_1.exception.invalidBlockchainService(JSON.stringify(this.blockchainservices));
27
+ return this.bsList.some(bs => bs.validateAddress(address));
28
+ }
29
+ validateTextAllBlockchains(text) {
30
+ if (this.haveBlockchainServices())
31
+ excpetions_1.exception.invalidBlockchainService(JSON.stringify(this.blockchainservices));
32
+ return this.bsList.some(bs => [bs.validateAddress(text), bs.validateEncryptedKey(text), bs.validateWif(text)].some(it => it === true));
33
+ }
34
+ validateWifAllBlockchains(wif) {
35
+ if (this.haveBlockchainServices())
36
+ excpetions_1.exception.invalidBlockchainService(JSON.stringify(this.blockchainservices));
37
+ return this.bsList.some(bs => bs.validateWif(wif));
38
+ }
39
+ validateEncryptedKeyAllBlockchains(encryptedKey) {
40
+ if (this.haveBlockchainServices())
41
+ excpetions_1.exception.invalidBlockchainService(JSON.stringify(this.blockchainservices));
42
+ return this.bsList.some(bs => bs.validateEncryptedKey(encryptedKey));
43
+ }
44
+ getBlockchainByAddress(address) {
45
+ var _a;
46
+ if (this.haveBlockchainServices())
47
+ excpetions_1.exception.invalidBlockchainService(JSON.stringify(this.blockchainservices));
48
+ return (_a = this.bsList.find(bs => bs.validateAddress(address))) !== null && _a !== void 0 ? _a : null;
49
+ }
50
+ getBlockchainByWif(wif) {
51
+ var _a;
52
+ if (this.haveBlockchainServices())
53
+ excpetions_1.exception.invalidBlockchainService(JSON.stringify(this.blockchainservices));
54
+ return (_a = this.bsList.find(bs => bs.validateWif(wif))) !== null && _a !== void 0 ? _a : null;
55
+ }
56
+ getBlockchainByEncryptedKey(encryptedKey) {
57
+ var _a;
58
+ if (this.haveBlockchainServices())
59
+ excpetions_1.exception.invalidBlockchainService(JSON.stringify(this.blockchainservices));
60
+ return (_a = this.bsList.find(bs => bs.validateEncryptedKey(encryptedKey))) !== null && _a !== void 0 ? _a : null;
61
+ }
62
+ getBlockchainsClaimable() {
63
+ const methodName = { claim: 'claim', getUnclaimed: 'getUnclaimed', tokenClaim: 'tokenClaim' };
64
+ const claimableBlockchains = this.bsList.filter(blockchain => methodName.claim in blockchain &&
65
+ methodName.getUnclaimed in blockchain.dataService &&
66
+ methodName.tokenClaim in blockchain);
67
+ return claimableBlockchains;
68
+ }
69
+ }
70
+ exports.BSAgreggator = BSAgreggator;
@@ -0,0 +1,8 @@
1
+ import { Currency, Exchange } from '../../interfaces';
2
+ export declare class Flamingo implements Exchange {
3
+ private request;
4
+ getTokenPrices(currency: Currency): Promise<{
5
+ amount: number;
6
+ Symbol: string;
7
+ }[]>;
8
+ }
@@ -0,0 +1,37 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Flamingo = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const FlamingoRoutes_1 = require("./FlamingoRoutes");
18
+ class Flamingo {
19
+ constructor() {
20
+ this.request = axios_1.default.create({ baseURL: 'https://api.flamingo.finance' });
21
+ }
22
+ getTokenPrices(currency) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const { data: prices } = yield this.request.get(`/${FlamingoRoutes_1.FLAMINGO_TOKENINFO_PRICES}`);
25
+ let currencyRatio = 1;
26
+ if (currency !== 'USD') {
27
+ const { data } = yield this.request.get(`/${(0, FlamingoRoutes_1.FLAMINGO_FIAT_EXCHANGE_RATE)('USD', currency)}`);
28
+ currencyRatio = data;
29
+ }
30
+ return prices.map(price => ({
31
+ amount: price.usd_price * currencyRatio,
32
+ Symbol: price.symbol
33
+ }));
34
+ });
35
+ }
36
+ }
37
+ exports.Flamingo = Flamingo;
@@ -0,0 +1,4 @@
1
+ export type FlamingoTokenInfoPricesResponse = {
2
+ symbol: string;
3
+ usd_price: number;
4
+ }[];
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import { Currency } from "../../interfaces";
2
+ export declare const FLAMINGO_TOKENINFO_PRICES = "token-info/prices";
3
+ export declare const FLAMINGO_FIAT_EXCHANGE_RATE: (currencyFrom: Currency, currencyTo: Currency) => string;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FLAMINGO_FIAT_EXCHANGE_RATE = exports.FLAMINGO_TOKENINFO_PRICES = void 0;
4
+ exports.FLAMINGO_TOKENINFO_PRICES = 'token-info/prices';
5
+ const FLAMINGO_FIAT_EXCHANGE_RATE = (currencyFrom, currencyTo) => {
6
+ return `fiat/exchange-rate?pair=${currencyFrom}_${currencyTo}`;
7
+ };
8
+ exports.FLAMINGO_FIAT_EXCHANGE_RATE = FLAMINGO_FIAT_EXCHANGE_RATE;
@@ -0,0 +1,7 @@
1
+ export * from './flamingo/Flamingo';
2
+ export * from './flamingo/FlamingoResponses';
3
+ export * from './flamingo/FlamingoRoutes';
4
+ import { Flamingo } from "./flamingo/Flamingo";
5
+ export declare const exchangeOptions: {
6
+ flamingo: Flamingo;
7
+ };
@@ -0,0 +1,24 @@
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
+ exports.exchangeOptions = void 0;
18
+ __exportStar(require("./flamingo/Flamingo"), exports);
19
+ __exportStar(require("./flamingo/FlamingoResponses"), exports);
20
+ __exportStar(require("./flamingo/FlamingoRoutes"), exports);
21
+ const Flamingo_1 = require("./flamingo/Flamingo");
22
+ exports.exchangeOptions = {
23
+ flamingo: new Flamingo_1.Flamingo()
24
+ };
@@ -0,0 +1,4 @@
1
+ export declare const exception: {
2
+ invalidBlockchainService: (message?: string) => never;
3
+ blockchainAlreadyExist: (blockchainName: string) => never;
4
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exception = void 0;
4
+ exports.exception = {
5
+ invalidBlockchainService: (message) => {
6
+ throw new Error(`Invalid blockchainServices => ${message}`);
7
+ },
8
+ blockchainAlreadyExist: (blockchainName) => {
9
+ throw new Error(`The blockchain ${blockchainName} already exist`);
10
+ }
11
+ };
@@ -0,0 +1,3 @@
1
+ export * from './interfaces';
2
+ export * from './BSAgreggator';
3
+ export * from './exchanges';
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
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("./interfaces"), exports);
18
+ __exportStar(require("./BSAgreggator"), exports);
19
+ __exportStar(require("./exchanges"), exports);
@@ -0,0 +1,180 @@
1
+ export type SendTransactionParam = {
2
+ senderAccount: Account;
3
+ transactionIntents: IntentTransactionParam[];
4
+ priorityFee?: number;
5
+ };
6
+ export type IntentTransactionParam = {
7
+ receiverAddress: string;
8
+ tokenHash: string;
9
+ amount: number;
10
+ };
11
+ export interface Account {
12
+ getWif(): string;
13
+ getAddress(): string;
14
+ }
15
+ export type CalculateTransferFeeDetails = {
16
+ systemFee?: string;
17
+ networkFee?: string;
18
+ };
19
+ export type Currency = "USD" | "BRL" | "EUR";
20
+ export type ExchangeInfo = {
21
+ symbol: string;
22
+ amount: number;
23
+ };
24
+ export interface Claimable {
25
+ claim(account: Account): Promise<{
26
+ txid: string;
27
+ symbol: string;
28
+ hash: string;
29
+ }>;
30
+ dataService: BlockchainDataService & BDSClaimable;
31
+ tokenClaim: {
32
+ hash: string;
33
+ symbol: string;
34
+ decimals: number;
35
+ };
36
+ }
37
+ export declare enum NNSRecordTypes {
38
+ IPV4 = "1",
39
+ CANONICAL_NAME = "5",
40
+ TEXT = "16",
41
+ IPV6 = "28"
42
+ }
43
+ export type NNSResponse = {
44
+ jsonrpc: string;
45
+ id: number;
46
+ result: {
47
+ script: string;
48
+ state: string;
49
+ gasconsumed: string;
50
+ exception: string;
51
+ stack: {
52
+ type: string;
53
+ value: string;
54
+ }[];
55
+ };
56
+ };
57
+ export interface NeoNameService {
58
+ getNeoNsRecord(domainName: string, type: typeof NNSRecordTypes): Promise<NNSResponse>;
59
+ getOwnerOfNeoNsRecord(domainName: string): Promise<NNSResponse>;
60
+ }
61
+ export type Token = {
62
+ name: string;
63
+ symbol: string;
64
+ hash: string;
65
+ decimals: number;
66
+ };
67
+ export interface BlockchainService<BSCustomName extends string = string> {
68
+ readonly dataService: BlockchainDataService;
69
+ readonly blockchainName: BSCustomName;
70
+ readonly derivationPath: string;
71
+ readonly feeToken: {
72
+ hash: string;
73
+ symbol: string;
74
+ decimals: number;
75
+ };
76
+ readonly exchange: Exchange;
77
+ readonly tokens: Token[];
78
+ sendTransaction(param: SendTransactionParam): Promise<string>;
79
+ generateMnemonic(): string;
80
+ generateWif(mnemonic: string, index: number): string;
81
+ generateAccount(mnemonic: string, index: number): {
82
+ wif: string;
83
+ address: string;
84
+ };
85
+ generateAccountFromWif(wif: string): string;
86
+ decryptKey(encryptedKey: string, password: string): Promise<{
87
+ wif: string;
88
+ address: string;
89
+ }>;
90
+ validateAddress(address: string): boolean;
91
+ validateEncryptedKey(encryptedKey: string): boolean;
92
+ validateWif(wif: string): boolean;
93
+ calculateTransferFee(param: SendTransactionParam, details?: boolean): Promise<{
94
+ result: number;
95
+ details?: CalculateTransferFeeDetails;
96
+ }>;
97
+ }
98
+ export type TransactionNotifications = {
99
+ contract: string;
100
+ event_name: string;
101
+ state: {
102
+ type: string;
103
+ value: string;
104
+ }[];
105
+ };
106
+ export type TransactionResponse = {
107
+ txid: string;
108
+ block: number;
109
+ time: string;
110
+ transfers: Omit<TransactionTransfer, 'txid'>[];
111
+ sysfee: string;
112
+ netfee: string;
113
+ totfee: string;
114
+ notifications: TransactionNotifications[];
115
+ };
116
+ export type TransactionTransfer = {
117
+ amount: string;
118
+ to: string;
119
+ from: string;
120
+ hash: string;
121
+ txid: string;
122
+ };
123
+ export type ContractParameter = {
124
+ name: string;
125
+ type: string;
126
+ };
127
+ export type TransactionHistoryResponse = {
128
+ totalCount: number;
129
+ transactions: TransactionResponse[];
130
+ };
131
+ export type ContractMethod = {
132
+ name: string;
133
+ parameters: ContractParameter[];
134
+ };
135
+ export type ContractResponse = {
136
+ hash: string;
137
+ name: string;
138
+ methods: ContractMethod[];
139
+ };
140
+ export type ConsensusNodeResponse = {
141
+ url: string;
142
+ height: number;
143
+ };
144
+ export type TokenInfoResponse = {
145
+ symbol: string;
146
+ decimals: number;
147
+ };
148
+ export type BlockchainNetwork = "mainnet" | "testnet" | "privatenet";
149
+ export type BalanceResponse = {
150
+ amount: number;
151
+ hash: string;
152
+ symbol: string;
153
+ name: string;
154
+ };
155
+ export type UnclaimedResponse = {
156
+ address: string;
157
+ unclaimed: number;
158
+ };
159
+ export interface BDSClaimable {
160
+ getUnclaimed(address: string): Promise<UnclaimedResponse>;
161
+ }
162
+ export interface BlockchainDataService {
163
+ readonly explorer: string;
164
+ network: BlockchainNetwork;
165
+ setNetwork(network: BlockchainNetwork): void;
166
+ getTransaction(txid: string): Promise<TransactionResponse>;
167
+ getHistoryTransactions(address: string, page: number): Promise<TransactionHistoryResponse>;
168
+ getContract(contractHash: string): Promise<ContractResponse>;
169
+ getTokenInfo(tokenHash: string): Promise<TokenInfoResponse>;
170
+ getBalance(address: string): Promise<BalanceResponse[]>;
171
+ getAllNodes(): Promise<ConsensusNodeResponse[]>;
172
+ getHigherNode(): Promise<ConsensusNodeResponse>;
173
+ }
174
+ export type ToeknPricesResponse = {
175
+ amount: number;
176
+ Symbol: string;
177
+ };
178
+ export interface Exchange {
179
+ getTokenPrices(currency: Currency): Promise<ToeknPricesResponse[]>;
180
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NNSRecordTypes = void 0;
4
+ var NNSRecordTypes;
5
+ (function (NNSRecordTypes) {
6
+ NNSRecordTypes["IPV4"] = "1";
7
+ NNSRecordTypes["CANONICAL_NAME"] = "5";
8
+ NNSRecordTypes["TEXT"] = "16";
9
+ NNSRecordTypes["IPV6"] = "28";
10
+ })(NNSRecordTypes = exports.NNSRecordTypes || (exports.NNSRecordTypes = {}));
11
+ //*****************************************************************************
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@cityofzion/blockchain-service",
3
+ "version": "0.1.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "author": "Coz",
7
+ "license": "MIT",
8
+ "files": ["/dist"],
9
+ "scripts": {
10
+ "build": "tsc && typedoc src/index.ts"
11
+ },
12
+ "dependencies": {
13
+ "axios": "1.3.2"
14
+ },
15
+ "devDependencies": {
16
+ "ts-node": "10.9.1",
17
+ "typescript": "4.9.5",
18
+ "typedoc": "0.23.24"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/CityOfZion/blockchain-services",
23
+ "directory": "packages/blockchain-service"
24
+ }
25
+ }