-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
143 lines (124 loc) · 3.59 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
import { config } from "./src/config";
import {
checkApprovedValue,
approveValue,
signOrder,
provider,
performOrder
} from "./src/example";
const divConsole = document.getElementById("console");
const btnApproveTransfer = document.getElementById("btnApproveTransfer");
const btnSignOrder = document.getElementById("btnSignOrder");
const btnPerformOrder = document.getElementById("btnPerformOrder");
btnApproveTransfer.addEventListener("click", async () => {
if (config.valueLedgerId === "") {
printWarning(
"No valueLedgerId defined. Set value ledger id in src/config.ts file."
);
return;
}
if (config.assetLedgerId === "") {
printWarning(
"No assetLedgerId defined. Set asset ledger id 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 checkApprovedValue().catch(e => {
printError(e);
});
if (isApproved) {
printMessage("Value already approved.");
} else {
const mutation = await approveValue().catch(e => {
printError(e);
});
if (mutation) {
printMessage("Value approval in progress: " + mutation.id);
printMessage("This may take a while.");
await mutation.complete();
printMessage("Value approval completed.");
}
}
});
btnSignOrder.addEventListener("click", async () => {
if (config.valueLedgerId === "") {
printWarning(
"No valueLedgerId defined. Set value ledger id in src/config.ts file."
);
return;
}
if (config.assetLedgerId === "") {
printWarning(
"No assetLedgerId defined. Set asset ledger id 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;
}
const mutation = await performOrder().catch(e => {
printError(e);
});
if (mutation) {
printMessage("Atomic approval in progress: " + mutation.id);
printMessage("This may take a while.");
await mutation.complete();
printMessage("Atomic approval completed");
printMessage(
"Created asset ledger address: " + mutation.logs[0].createdContract
);
}
});
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);
}