This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
api.ts
117 lines (103 loc) · 3.12 KB
/
api.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
import { Context } from "./context";
import Axios from "axios";
import { TxEncoder, Msg } from "./tx";
import { TxBuilder, TxBuilderConfig } from "./txBuilder";
import { Bech32Config } from "./bech32Config";
import { useGlobalBech32Config } from "../common/address";
import { Key, WalletProvider } from "./walletProvider";
import { TendermintRPC } from "../rpc/tendermint";
import { Rest } from "./rest";
import { QueryAccount } from "./account";
import { ResultBroadcastTx, ResultBroadcastTxCommit } from "../rpc/tx";
import { BIP44 } from "./bip44";
import { Codec } from "@node-a-team/ts-amino";
export interface ApiConfig {
chainId: string;
walletProvider: WalletProvider;
/** Endpoint of rpc */
rpc: string;
/** Endpoint of rest api */
rest: string;
disableGlobalBech32Config?: boolean;
}
export interface CoreConfig<R extends Rest> {
/**
* Encoder for transaction.
*/
txEncoder: TxEncoder;
/**
* Builder for transaction.
*/
txBuilder: TxBuilder;
restFactory: (context: Context) => R;
queryAccount: QueryAccount;
bech32Config: Bech32Config;
bip44: BIP44;
registerCodec: (codec: Codec) => void;
}
export class Api<R extends Rest> {
private _context: Context;
private _rpc: TendermintRPC;
private _rest: R;
constructor(config: ApiConfig, coreConfig: CoreConfig<R>) {
this._context = new Context({
chainId: config.chainId,
txEncoder: coreConfig.txEncoder,
txBuilder: coreConfig.txBuilder,
bech32Config: coreConfig.bech32Config,
walletProvider: config.walletProvider,
rpcInstance: Axios.create({
baseURL: config.rpc
}),
restInstance: Axios.create({
baseURL: config.rest
}),
queryAccount: coreConfig.queryAccount,
bip44: coreConfig.bip44,
codec: new Codec()
});
coreConfig.registerCodec(this.context.get("codec"));
this._rpc = new TendermintRPC(this.context);
this._rest = coreConfig.restFactory(this.context);
if (!config.disableGlobalBech32Config) {
useGlobalBech32Config(coreConfig.bech32Config);
}
}
public async enable(): Promise<void> {
await this.wallet.enable(this.context);
return Promise.resolve();
}
public async getKeys(): Promise<Key[]> {
return await this.wallet.getKeys(this.context);
}
/**
* Send msgs.
* @return If mode is commit, this will return [[ResultBroadcastTx]].
* Or if mode is sync or async, this will return [[ResultBroadcastTxCommit]].
*/
public async sendMsgs(
msgs: Msg[],
config: TxBuilderConfig,
mode: "commit" | "sync" | "async" = "sync"
): Promise<ResultBroadcastTx | ResultBroadcastTxCommit> {
const tx = await this.context.get("txBuilder")(this.context, msgs, config);
const bz = this.context.get("txEncoder")(this.context, tx);
if (mode === "commit") {
return this.rpc.broadcastTxCommit(bz);
} else {
return this.rpc.broadcastTx(bz, mode);
}
}
get context(): Context {
return this._context;
}
get wallet(): WalletProvider {
return this.context.get("walletProvider");
}
get rpc(): TendermintRPC {
return this._rpc;
}
get rest(): R {
return this._rest;
}
}