-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
193 lines (170 loc) · 4.92 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import {
checkApprovedAssetTransfer,
approveAssetTransfer,
checkApprovedAssetCreation,
approveAssetCreation,
performOrder,
provider,
signOrderAccount1,
signOrderDynamic
} from "./src/example";
import { config } from "./src/config";
const divConsole = document.getElementById("console");
const btnApproveAssetTransfer = document.getElementById(
"btnApproveAssetTransfer"
);
const btnApproveAssetCreation = document.getElementById(
"btnApproveAssetCreation"
);
const btnSignOrder = document.getElementById("btnSignOrder");
const btnPerformOrder = document.getElementById("btnPerformOrder");
btnSignOrder.addEventListener("click", async () => {
if (config.assetLedgerId === "") {
printWarning(
"No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file."
);
return;
}
if (config.account1Id === "") {
printWarning("No account1Id defined. Please set it in src/config.ts file.");
return;
}
if (provider.accountId === config.account1Id) {
let error = null;
await signOrderAccount1().catch(e => {
error = e;
printError(e);
});
if (!error) {
printMessage(
"Order signing with account 1 sucessfull: " + config.signatureAccount1
);
}
} else {
let error = null;
await signOrderDynamic().catch(e => {
error = e;
printError(e);
});
if (!error) {
printMessage(
"Order signing with any account sucessfull: " + config.signatureDynamic
);
}
}
});
btnPerformOrder.addEventListener("click", async () => {
if (!config.signatureAccount1) {
printWarning(
"No signature from account 1 provided. Please sign the order with account 1 first."
);
return;
}
if (!config.signatureDynamic) {
printWarning(
"No second signature provided. Please sign the order with any account except account 1."
);
return;
}
const mutation = await performOrder().catch(e => {
printError(e);
});
if (mutation) {
printMessage("Atomic order in progress: " + mutation.id);
printMessage("This may take a while.");
await mutation.complete();
printMessage("Atomic order completed");
}
});
btnApproveAssetCreation.addEventListener("click", async () => {
if (config.assetLedgerId === "") {
printWarning(
"No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file."
);
return;
}
if (config.account1Id === "") {
printWarning("No account1Id defined. Please set it in src/config.ts file.");
return;
}
const isApproved = await checkApprovedAssetCreation().catch(e => {
printError(e);
return;
});
if (isApproved === null) {
printError("Error occured when retriving approved status.");
return;
}
if (isApproved) {
printMessage("Already approved.");
} else {
const mutation = await approveAssetCreation().catch(e => {
printError(e);
});
if (mutation) {
printMessage("Asset creation approving progress: " + mutation.id);
printMessage("This may take a while.");
await mutation.complete();
printMessage("Asset creation approval completed");
}
}
});
btnApproveAssetTransfer.addEventListener("click", async () => {
if (config.assetLedgerId === "") {
printWarning(
"No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file."
);
return;
}
if (config.account1Id === "") {
printWarning("No account1Id defined. Please set it in src/config.ts file.");
return;
}
const isApproved = await checkApprovedAssetTransfer().catch(e => {
printError(e);
return;
});
if (isApproved === null) {
printError("Error occured when retriving approved status.");
return;
}
if (isApproved) {
printMessage("Already approved.");
} else {
const mutation = await approveAssetTransfer().catch(e => {
printError(e);
});
if (mutation) {
printMessage("Asset transfer approving progress: " + mutation.id);
printMessage("This may take a while.");
await mutation.complete();
printMessage("Asset transfer approval completed");
}
}
});
function printError(message: any) {
if (typeof message !== "string") {
message = JSON.stringify(message, null, 2);
}
const div = document.createElement("div");
div.innerText = "Error: " + message;
div.className = "error";
divConsole.prepend(div);
}
function printWarning(message: any) {
if (typeof message !== "string") {
message = JSON.stringify(message, null, 2);
}
const div = document.createElement("div");
div.innerText = "Warning: " + message;
div.className = "warning";
divConsole.prepend(div);
}
function printMessage(message: any) {
if (typeof message !== "string") {
message = JSON.stringify(message, null, 2);
}
const div = document.createElement("div");
div.innerText = message;
divConsole.prepend(div);
}