Skip to content

Commit e6b1126

Browse files
committed
Copy codebase from fixed actions order
1 parent 695d450 commit e6b1126

8 files changed

+425
-2
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
# example-signed-fixed-actions-order
2-
Code example: Signed fixed actions order
1+
This is an example of how to use signed fixed actions order in 0xcert framework v2.
2+
3+
Project stucture:
4+
5+
| Path | Description |
6+
| -------------- | --------------------------------------------- |
7+
| src/example.ts | Main logic showing the use. |
8+
| index.html | Front end styling. |
9+
| index.ts | Controller connecting front end to the logic. |
10+
| package.json | Dependencies. |
11+
| config.ts | Project configuration. |

index.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
<head>
3+
<title>0xcert framework v2 using signed fixed actions order example</title>
4+
<link rel="stylesheet" href="style.css" />
5+
<meta charset="UTF-8" />
6+
</head>
7+
8+
<body>
9+
<div id="buttons">
10+
<button id="btnApproveAssetTransfer">Approve asset transfer</button>
11+
<button id="btnApproveAssetCreation">Approve asset creation</button>
12+
<button id="btnSignOrder">Sign order</button>
13+
<button id="btnPerformOrder">Perform order</button>
14+
</div>
15+
<div id="console">
16+
<div>Console output.</div>
17+
</div>
18+
<script src="index.ts"></script>
19+
</body>
20+
</html>

index.ts

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
}

package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "example-atomic-orders",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.html",
6+
"scripts": {
7+
"start": "parcel index.html --open",
8+
"build": "parcel build index.html"
9+
},
10+
"dependencies": {
11+
"@0xcert/ethereum-asset-ledger": "2.0.0-alpha14",
12+
"@0xcert/ethereum-gateway": "2.0.0-alpha14",
13+
"@0xcert/ethereum-metamask-provider": "2.0.0-alpha14",
14+
"@0xcert/ethereum-value-ledger": "2.0.0-alpha14"
15+
},
16+
"devDependencies": {
17+
"parcel-bundler": "^1.6.1"
18+
},
19+
"keywords": []
20+
}

src/config.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
OrderKind,
3+
ActionsOrderActionKind,
4+
FixedActionsOrder
5+
} from "@0xcert/ethereum-gateway";
6+
7+
export const config = {
8+
providerConfig: {
9+
requiredConfirmations: 1,
10+
// ropsten config
11+
gatewayConfig: {
12+
actionsOrderId: "0x6Cb40DB529637C218824a2660EFC7CbaD5485115",
13+
assetLedgerDeployOrderId: "0x9de066264347165693eC890ccC1C8Af8f9A15f51",
14+
valueLedgerDeployOrderId: "0x327577e70e21AcEe01447AD06939Fb4057232b2A"
15+
}
16+
},
17+
assetLedgerId: "", // Input you own asset ledger id
18+
account1Id: "", // Input your primary metamask account Id.
19+
account2Id: "", // Input your secondary metamask account Id
20+
signature: ""
21+
};
22+
23+
export const order = {
24+
kind: OrderKind.FIXED_ACTIONS_ORDER,
25+
signers: [config.account1Id, config.account2Id],
26+
seed: Date.now(),
27+
expiration: Date.now() + 86400000,
28+
actions: [
29+
{
30+
kind: ActionsOrderActionKind.TRANSFER_ASSET,
31+
ledgerId: config.assetLedgerId,
32+
senderId: config.account1Id,
33+
receiverId: config.account2Id,
34+
assetId: "100"
35+
},
36+
{
37+
kind: ActionsOrderActionKind.CREATE_ASSET,
38+
ledgerId: config.assetLedgerId,
39+
senderId: config.account1Id,
40+
receiverId: config.account2Id,
41+
assetId: "101",
42+
assetImprint:
43+
"c6c14772f269bed1161d4350403f4c867c749b3cce7abe84c6d0605068cd8a87"
44+
}
45+
]
46+
} as FixedActionsOrder;

src/example.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {
2+
AssetLedger,
3+
GeneralAssetLedgerAbility
4+
} from "@0xcert/ethereum-asset-ledger";
5+
import { Gateway, ProxyKind } from "@0xcert/ethereum-gateway";
6+
import { MetamaskProvider } from "@0xcert/ethereum-metamask-provider";
7+
import { config, order } from "./config";
8+
9+
// We create a new instance of metamask provider.
10+
export const provider = new MetamaskProvider(config.providerConfig);
11+
12+
export async function enableMetamask() {
13+
// We first check if metamask is already enabled.
14+
if (!(await provider.isEnabled())) {
15+
// If metamask is not enabled, we enable it.
16+
await provider.enable();
17+
}
18+
}
19+
20+
export async function checkApprovedAssetTransfer() {
21+
await enableMetamask();
22+
const assetLedger = new AssetLedger(provider, config.assetLedgerId);
23+
const gateway = new Gateway(provider);
24+
const transferProxy = await gateway.getProxyAccountId(
25+
ProxyKind.TRANSFER_ASSET
26+
);
27+
return assetLedger.isApprovedOperator(config.account1Id, transferProxy);
28+
}
29+
30+
export async function checkApprovedAssetCreation() {
31+
await enableMetamask();
32+
const assetLedger = new AssetLedger(provider, config.assetLedgerId);
33+
const gateway = new Gateway(provider);
34+
const createProxy = await gateway.getProxyAccountId(ProxyKind.CREATE_ASSET);
35+
const abilities = await assetLedger.getAbilities(createProxy);
36+
if (typeof abilities === "undefined") {
37+
return false;
38+
}
39+
return abilities.indexOf(GeneralAssetLedgerAbility.CREATE_ASSET) !== -1;
40+
}
41+
42+
export async function approveAssetTransfer() {
43+
await enableMetamask();
44+
const assetLedger = new AssetLedger(provider, config.assetLedgerId);
45+
const gateway = new Gateway(provider);
46+
const transferProxy = await gateway.getProxyAccountId(
47+
ProxyKind.TRANSFER_ASSET
48+
);
49+
return assetLedger.approveOperator(transferProxy);
50+
}
51+
52+
export async function approveAssetCreation() {
53+
await enableMetamask();
54+
const assetLedger = new AssetLedger(provider, config.assetLedgerId);
55+
const gateway = new Gateway(provider);
56+
const createProxy = await gateway.getProxyAccountId(ProxyKind.CREATE_ASSET);
57+
return assetLedger.grantAbilities(createProxy, [
58+
GeneralAssetLedgerAbility.CREATE_ASSET
59+
]);
60+
}
61+
62+
export async function signOrder() {
63+
await enableMetamask();
64+
const gateway = new Gateway(provider);
65+
console.log(order);
66+
const signature = await gateway.sign(order).catch(e => {
67+
console.log(e);
68+
throw e;
69+
});
70+
config.signature = signature;
71+
}
72+
73+
export async function performOrder() {
74+
const gateway = new Gateway(provider);
75+
return gateway.perform(order, [config.signature]);
76+
}

0 commit comments

Comments
 (0)