This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
/
client.ts
166 lines (136 loc) · 5.27 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Coins, Contracts, DTO, Exceptions, Helpers } from "@arkecosystem/platform-sdk";
import { Arr } from "@arkecosystem/platform-sdk-support";
import { WalletData } from "../dto";
import * as TransactionDTO from "../dto";
export class ClientService implements Contracts.ClientService {
readonly #http: Contracts.HttpClient;
readonly #peer: string;
readonly #broadcastErrors: Record<string, string> = {
"failed to marshal JSON bytes": "ERR_JSON_MARSHAL",
"failed to unmarshal JSON bytes": "ERR_JSON_UNMARSHAL",
"insufficient account funds": "ERR_INSUFFICIENT_FUNDS",
"insufficient fee": "ERR_INSUFFICIENT_FEE",
"insufficient funds": "ERR_INSUFFICIENT_FUNDS",
"invalid account password": "ERR_WRONG_PASSWORD",
"invalid address": "ERR_INVALID_ADDRESS",
"invalid coins": "ERR_INVALID_COINS",
"invalid gas adjustment": "ERROR_INVALID_GAS_ADJUSTMENT",
"invalid pubkey": "ERR_INVALID_PUB_KEY",
"invalid request": "ERR_INVALID_REQUEST",
"invalid sequence": "ERR_INVALID_SEQUENCE",
"key not found": "ERR_KEY_NOT_FOUND",
"maximum number of signatures exceeded": "ERR_TOO_MANY_SIGNATURES",
"memo too large": "ERR_MEMO_TOO_LARGE",
"mempool is full": "ERR_MEMPOOL_IS_FULL",
"no signatures supplied": "ERR_NO_SIGNATURES",
"out of gas": "ERR_OUT_OF_GAS",
"tx already in mempool": "ERR_TX_IN_MEMPOOL_CACHE",
"tx intended signer does not match the given signer": "ERROR_INVALID_SIGNER",
"tx parse error": "ERR_TX_DECODE",
"tx too large": "ERR_TX_TOO_LARGE",
"unknown address": "ERR_UNKNOWN_ADDRESS",
"unknown request": "ERR_UNKNOWN_REQUEST",
internal: "ERR_INTERNAL",
panic: "ERR_PANIC",
unauthorized: "ERR_UNAUTHORIZED",
};
private constructor({ http, peer }) {
this.#http = http;
this.#peer = peer;
}
public static async construct(config: Coins.Config): Promise<ClientService> {
try {
return new ClientService({
http: config.get<Contracts.HttpClient>("httpClient"),
peer: config.get<string>("peer"),
});
} catch {
return new ClientService({
http: config.get<Contracts.HttpClient>("httpClient"),
peer: Arr.randomElement(config.get<Coins.CoinNetwork>("network").hosts),
});
}
}
public async destruct(): Promise<void> {
//
}
public async transaction(id: string): Promise<Contracts.TransactionData> {
const response = await this.get(`txs/${id}`);
return Helpers.createTransactionDataWithType(response, TransactionDTO);
}
public async transactions(query: Contracts.ClientTransactionsInput): Promise<Coins.TransactionDataCollection> {
const response = await this.get("txs", {
"message.action": "send",
"message.sender": query.address,
page: query.cursor || 1,
limit: query.limit || 100,
});
return Helpers.createTransactionDataCollectionWithType(
response.txs,
{
prev: undefined, // @TODO: build from URL and page_number
self: undefined, // @TODO: build from URL and page_number
next: undefined, // @TODO: build from URL and page_number
},
TransactionDTO,
);
}
public async wallet(id: string): Promise<Contracts.WalletData> {
const { result } = await this.get(`auth/accounts/${id}`);
return new WalletData(result.value);
}
public async wallets(query: Contracts.ClientWalletsInput): Promise<Coins.WalletDataCollection> {
throw new Exceptions.NotImplemented(this.constructor.name, "wallets");
}
public async delegate(id: string): Promise<Contracts.WalletData> {
throw new Exceptions.NotImplemented(this.constructor.name, "delegate");
}
public async delegates(query?: Contracts.KeyValuePair): Promise<Coins.WalletDataCollection> {
throw new Exceptions.NotImplemented(this.constructor.name, "delegates");
}
public async votes(id: string, query?: Contracts.KeyValuePair): Promise<Coins.TransactionDataCollection> {
throw new Exceptions.NotImplemented(this.constructor.name, "votes");
}
public async voters(id: string, query?: Contracts.KeyValuePair): Promise<Coins.WalletDataCollection> {
throw new Exceptions.NotImplemented(this.constructor.name, "voters");
}
public async syncing(): Promise<boolean> {
const { syncing } = await this.get("syncing");
return syncing;
}
public async broadcast(transactions: Contracts.SignedTransactionData[]): Promise<Contracts.BroadcastResponse> {
const result: Contracts.BroadcastResponse = {
accepted: [],
rejected: [],
errors: {},
};
for (const transaction of transactions) {
const { logs, txhash } = await this.post("txs", { mode: "block", tx: transaction });
if (logs[0].success === true) {
result.accepted.push(txhash);
} else {
const { message } = JSON.parse(logs[0].log);
if (message) {
result.rejected.push(txhash);
if (!Array.isArray(result.errors[txhash])) {
result.errors[txhash] = [];
}
for (const [key, value] of Object.entries(this.#broadcastErrors)) {
if (message.includes(key)) {
result.errors[txhash].push(value);
}
}
}
}
}
return result;
}
private async get(path: string, query?: Contracts.KeyValuePair): Promise<Contracts.KeyValuePair> {
const response = await this.#http.get(`${this.#peer}/${path}`, query);
return response.json();
}
private async post(path: string, body: Contracts.KeyValuePair): Promise<Contracts.KeyValuePair> {
const response = await this.#http.post(`${this.#peer}/${path}`, body);
return response.json();
}
}