|
| 1 | +import { |
| 2 | + checkApprovedAssetTransfer, |
| 3 | + approveAssetTransfer, |
| 4 | + checkApprovedAssetCreation, |
| 5 | + approveAssetCreation, |
| 6 | + signOrder, |
| 7 | + performOrder, |
| 8 | + provider |
| 9 | +} from "./src/example"; |
| 10 | +import { config } from "./src/config"; |
| 11 | + |
| 12 | +const divConsole = document.getElementById("console"); |
| 13 | +const btnApproveAssetTransfer = document.getElementById( |
| 14 | + "btnApproveAssetTransfer" |
| 15 | +); |
| 16 | +const btnApproveAssetCreation = document.getElementById( |
| 17 | + "btnApproveAssetCreation" |
| 18 | +); |
| 19 | +const btnSignOrder = document.getElementById("btnSignOrder"); |
| 20 | +const btnPerformOrder = document.getElementById("btnPerformOrder"); |
| 21 | + |
| 22 | +btnSignOrder.addEventListener("click", async () => { |
| 23 | + if (config.assetLedgerId === "") { |
| 24 | + printWarning( |
| 25 | + "No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file." |
| 26 | + ); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (config.account1Id === "") { |
| 31 | + printWarning("No account1Id defined. Please set it in src/config.ts file."); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + if (config.account2Id === "") { |
| 36 | + printWarning("No account2Id defined. Please set it in src/config.ts file."); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (provider.accountId !== config.account1Id) { |
| 41 | + printWarning("Select account1 in metamask to sign this order."); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + let error = null; |
| 46 | + await signOrder().catch(e => { |
| 47 | + error = e; |
| 48 | + printError(e); |
| 49 | + }); |
| 50 | + |
| 51 | + if (!error) { |
| 52 | + printMessage("Order signing sucessfull: " + config.signature); |
| 53 | + } |
| 54 | +}); |
| 55 | + |
| 56 | +btnPerformOrder.addEventListener("click", async () => { |
| 57 | + if (config.assetLedgerId === "") { |
| 58 | + printWarning( |
| 59 | + "No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file." |
| 60 | + ); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (config.account2Id === "") { |
| 65 | + printWarning("No account2Id defined. Please set it in src/config.ts file."); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (provider.accountId !== config.account2Id) { |
| 70 | + printWarning("Select account2 in metamask to perform this order."); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const mutation = await performOrder().catch(e => { |
| 75 | + printError(e); |
| 76 | + }); |
| 77 | + |
| 78 | + if (mutation) { |
| 79 | + printMessage("Atomic order in progress: " + mutation.id); |
| 80 | + printMessage("This may take a while."); |
| 81 | + await mutation.complete(); |
| 82 | + printMessage("Atomic order completed"); |
| 83 | + } |
| 84 | +}); |
| 85 | + |
| 86 | +btnApproveAssetCreation.addEventListener("click", async () => { |
| 87 | + if (config.assetLedgerId === "") { |
| 88 | + printWarning( |
| 89 | + "No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file." |
| 90 | + ); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (config.account1Id === "") { |
| 95 | + printWarning("No account1Id defined. Please set it in src/config.ts file."); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + const isApproved = await checkApprovedAssetCreation().catch(e => { |
| 100 | + printError(e); |
| 101 | + return; |
| 102 | + }); |
| 103 | + |
| 104 | + if (isApproved === null) { |
| 105 | + printError("Error occured when retriving approved status."); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (isApproved) { |
| 110 | + printMessage("Already approved."); |
| 111 | + } else { |
| 112 | + const mutation = await approveAssetCreation().catch(e => { |
| 113 | + printError(e); |
| 114 | + }); |
| 115 | + if (mutation) { |
| 116 | + printMessage("Asset creation approving progress: " + mutation.id); |
| 117 | + printMessage("This may take a while."); |
| 118 | + await mutation.complete(); |
| 119 | + printMessage("Asset creation approval completed"); |
| 120 | + } |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +btnApproveAssetTransfer.addEventListener("click", async () => { |
| 125 | + if (config.assetLedgerId === "") { |
| 126 | + printWarning( |
| 127 | + "No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file." |
| 128 | + ); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (config.account1Id === "") { |
| 133 | + printWarning("No account1Id defined. Please set it in src/config.ts file."); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + const isApproved = await checkApprovedAssetTransfer().catch(e => { |
| 138 | + printError(e); |
| 139 | + return; |
| 140 | + }); |
| 141 | + |
| 142 | + if (isApproved === null) { |
| 143 | + printError("Error occured when retriving approved status."); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + if (isApproved) { |
| 148 | + printMessage("Already approved."); |
| 149 | + } else { |
| 150 | + const mutation = await approveAssetTransfer().catch(e => { |
| 151 | + printError(e); |
| 152 | + }); |
| 153 | + if (mutation) { |
| 154 | + printMessage("Asset transfer approving progress: " + mutation.id); |
| 155 | + printMessage("This may take a while."); |
| 156 | + await mutation.complete(); |
| 157 | + printMessage("Asset transfer approval completed"); |
| 158 | + } |
| 159 | + } |
| 160 | +}); |
| 161 | + |
| 162 | +function printError(message: any) { |
| 163 | + if (typeof message !== "string") { |
| 164 | + message = JSON.stringify(message, null, 2); |
| 165 | + } |
| 166 | + const div = document.createElement("div"); |
| 167 | + div.innerText = "Error: " + message; |
| 168 | + div.className = "error"; |
| 169 | + divConsole.prepend(div); |
| 170 | +} |
| 171 | + |
| 172 | +function printWarning(message: any) { |
| 173 | + if (typeof message !== "string") { |
| 174 | + message = JSON.stringify(message, null, 2); |
| 175 | + } |
| 176 | + const div = document.createElement("div"); |
| 177 | + div.innerText = "Warning: " + message; |
| 178 | + div.className = "warning"; |
| 179 | + divConsole.prepend(div); |
| 180 | +} |
| 181 | + |
| 182 | +function printMessage(message: any) { |
| 183 | + if (typeof message !== "string") { |
| 184 | + message = JSON.stringify(message, null, 2); |
| 185 | + } |
| 186 | + const div = document.createElement("div"); |
| 187 | + div.innerText = message; |
| 188 | + divConsole.prepend(div); |
| 189 | +} |
0 commit comments