-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
182 lines (158 loc) · 4.72 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
import {
checkApprovedAssetTransfer,
approveAssetTransfer,
checkApprovedAssetCreation,
approveAssetCreation,
signOrder,
performOrder,
provider
} 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 (config.account2Id === "") {
printWarning("No account2Id defined. Please set it in src/config.ts file.");
return;
}
if (provider.accountId !== config.account1Id) {
printWarning("Select account1 in metamask to sign this order.");
return;
}
let error = null;
await signOrder().catch(e => {
error = e;
printError(e);
});
if (!error) {
printMessage("Order signing sucessfull: " + config.signature);
}
});
btnPerformOrder.addEventListener("click", async () => {
if (config.signature === "") {
printWarning("No signature provided. Please sign the order first.");
return;
}
if (provider.accountId !== config.account2Id) {
printWarning("Select account2 in metamask to perform this order.");
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);
}