-
Notifications
You must be signed in to change notification settings - Fork 7
/
blockchain.ts
155 lines (125 loc) · 4.63 KB
/
blockchain.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
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import {
algorand,
BlockchainAssetsModel,
BlockchainCreateModel,
BlockchainCreateResultModel,
BlockchainDeleteModel,
BlockchainGetModel,
BlockchainGetResultModel,
BlockchainInitModel,
BlockchainListModel,
BlockchainListResultModel,
BlockchainPayModel,
BlockchainPayNoNameModel,
BlockchainSignModel,
BlockchainSignNoNameModel,
stellar,
tfchain,
} from ".";
import blockchainInterface, { blockchainType } from "./blockchainInterface";
class Blockchain implements blockchainInterface {
stellar: stellar;
algorand: algorand;
tfchain: tfchain;
current_account: string;
blockchain_type: blockchainType;
constructor(config: GridClientConfig) {
this.stellar = new stellar(config);
this.algorand = new algorand(config);
this.tfchain = new tfchain(config);
}
async exist_in(options: BlockchainGetModel): Promise<blockchainType> {
if (await this.stellar.exist(options)) return blockchainType.stellar;
else if (await this.algorand.exist(options)) return blockchainType.algorand;
else if (await this.tfchain.exist(options)) return blockchainType.tfchain;
}
@expose
@validateInput
async exist(options: BlockchainGetModel) {
return (
(await this.stellar.exist({ name: options.name })) ||
(await this.algorand.exist({ name: options.name })) ||
(await this.tfchain.exist({ name: options.name }))
);
}
@expose
@validateInput
async select(options: BlockchainGetModel) {
const account_exists = await this.exist(options);
if (!account_exists) throw Error(`Account ${options.name} doesn't exist`);
this.current_account = options.name;
this.blockchain_type = await this.exist_in(options);
}
@expose
@validateInput
async create(options: BlockchainCreateModel): Promise<BlockchainCreateResultModel> {
const account_exists = await this.exist(options);
if (account_exists) throw Error(`Name ${options.name} already exists`);
return this[options.blockchain_type].create(options);
}
@expose
@validateInput
async sign(options: BlockchainSignNoNameModel) {
if (!this.current_account) throw Error(`No account is selected. Please select an account first`);
const modified_options: BlockchainSignModel = { name: this.current_account, content: options.content };
return this[this.blockchain_type].sign(modified_options);
}
@expose
@validateInput
async init(options: BlockchainInitModel) {
return this[options.blockchain_type].init(options);
}
@expose
@validateInput
async get(): Promise<BlockchainGetResultModel> {
if (!this.current_account) throw Error(`No account is selected. Please select an account first`);
const options = { name: this.current_account };
return this[this.blockchain_type].get(options);
}
@expose
@validateInput
async list(options?: BlockchainListModel): Promise<BlockchainListResultModel[]> {
if (!options || !options.blockchain_type) {
const stellar_accounts = await this.stellar.list();
const algorand_accounts = await this.algorand.list();
const tfchain_accounts = await this.tfchain.list();
return stellar_accounts.concat(algorand_accounts).concat(tfchain_accounts);
}
return this[options.blockchain_type].list();
}
@expose
@validateInput
async assets(): Promise<BlockchainAssetsModel> {
if (!this.current_account) throw Error(`No account is selected. Please select an account first`);
const options = { name: this.current_account };
return this[this.blockchain_type].assets(options);
}
@expose
@validateInput
// TODO : bridge, still
async pay(options: BlockchainPayNoNameModel) {
if (!this.current_account) throw Error(`No account is selected. Please select an account first`);
if (this.blockchain_type != options.blockchain_type_dest)
throw Error(`Transfer between blockchains isn't implemented yet`);
const modified_options: BlockchainPayModel = {
name: this.current_account,
blockchain_type_dest: options.blockchain_type_dest,
description: options.description,
address_dest: options.address_dest,
amount: options.amount,
asset: options.asset,
};
return this[this.blockchain_type].pay(modified_options);
}
@expose
@validateInput
async delete() {
if (!this.current_account) throw Error(`No account is selected. Please select an account first`);
const options: BlockchainDeleteModel = { name: this.current_account };
return this[this.blockchain_type].delete(options);
}
}
export { Blockchain as blockchain };