Skip to content

Commit f9bac1e

Browse files
committed
Signed fixed actions order logic.
1 parent e6b1126 commit f9bac1e

File tree

4 files changed

+58
-36
lines changed

4 files changed

+58
-36
lines changed

index.ts

+33-22
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import {
33
approveAssetTransfer,
44
checkApprovedAssetCreation,
55
approveAssetCreation,
6-
signOrder,
76
performOrder,
8-
provider
7+
provider,
8+
signOrderAccount1,
9+
signOrderAccount2
910
} from "./src/example";
1011
import { config } from "./src/config";
1112

@@ -37,37 +38,47 @@ btnSignOrder.addEventListener("click", async () => {
3738
return;
3839
}
3940

40-
if (provider.accountId !== config.account1Id) {
41-
printWarning("Select account1 in metamask to sign this order.");
42-
return;
43-
}
41+
if (provider.accountId === config.account1Id) {
42+
let error = null;
43+
await signOrderAccount1().catch(e => {
44+
error = e;
45+
printError(e);
46+
});
4447

45-
let error = null;
46-
await signOrder().catch(e => {
47-
error = e;
48-
printError(e);
49-
});
48+
if (!error) {
49+
printMessage(
50+
"Order signing with account1 sucessfull: " + config.signatureAccount1
51+
);
52+
}
53+
} else if (provider.accountId === config.account2Id) {
54+
let error = null;
55+
await signOrderAccount2().catch(e => {
56+
error = e;
57+
printError(e);
58+
});
5059

51-
if (!error) {
52-
printMessage("Order signing sucessfull: " + config.signature);
60+
if (!error) {
61+
printMessage(
62+
"Order signing with account2 sucessfull: " + config.signatureAccount2
63+
);
64+
}
65+
} else {
66+
printWarning("Select account1 or account2 in metamask to sign this order.");
5367
}
5468
});
5569

5670
btnPerformOrder.addEventListener("click", async () => {
57-
if (config.assetLedgerId === "") {
71+
if (config.signatureAccount1 === "") {
5872
printWarning(
59-
"No assetLedgerSource defined. Either deploy a new asset ledger or set asset ledger source in src/config.ts file."
73+
"No signature from account 1 provided. Please sign order with account 1."
6074
);
6175
return;
6276
}
6377

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.");
78+
if (config.signatureAccount2 === "") {
79+
printWarning(
80+
"No signature from account 2 provided. Please sign order with account 2."
81+
);
7182
return;
7283
}
7384

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "example-atomic-orders",
2+
"name": "example-signed-fixed-actions-order",
33
"version": "0.0.1",
44
"description": "",
55
"main": "index.html",
@@ -17,4 +17,4 @@
1717
"parcel-bundler": "^1.6.1"
1818
},
1919
"keywords": []
20-
}
20+
}

src/config.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
OrderKind,
33
ActionsOrderActionKind,
4-
FixedActionsOrder
4+
SignedFixedActionsOrder
55
} from "@0xcert/ethereum-gateway";
66

77
export const config = {
@@ -14,14 +14,15 @@ export const config = {
1414
valueLedgerDeployOrderId: "0x327577e70e21AcEe01447AD06939Fb4057232b2A"
1515
}
1616
},
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: ""
17+
assetLedgerId: "0x8ABa05cA8c8F2c614486AD0EbaFf794C297bc6e5", // Input you own asset ledger id
18+
account1Id: "0xF9196F9f176fd2eF9243E8960817d5FbE63D79aa", // Input your primary metamask account Id.
19+
account2Id: "0x44e44897FC076Bc46AaE6b06b917D0dfD8B2dae9", // Input your secondary metamask account Id
20+
signatureAccount1: "",
21+
signatureAccount2: ""
2122
};
2223

2324
export const order = {
24-
kind: OrderKind.FIXED_ACTIONS_ORDER,
25+
kind: OrderKind.SIGNED_FIXED_ACTIONS_ORDER,
2526
signers: [config.account1Id, config.account2Id],
2627
seed: Date.now(),
2728
expiration: Date.now() + 86400000,
@@ -43,4 +44,4 @@ export const order = {
4344
"c6c14772f269bed1161d4350403f4c867c749b3cce7abe84c6d0605068cd8a87"
4445
}
4546
]
46-
} as FixedActionsOrder;
47+
} as SignedFixedActionsOrder;

src/example.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,28 @@ export async function approveAssetCreation() {
5959
]);
6060
}
6161

62-
export async function signOrder() {
62+
export async function signOrderAccount1() {
6363
await enableMetamask();
6464
const gateway = new Gateway(provider);
65-
console.log(order);
6665
const signature = await gateway.sign(order).catch(e => {
67-
console.log(e);
6866
throw e;
6967
});
70-
config.signature = signature;
68+
config.signatureAccount1 = signature;
69+
}
70+
71+
export async function signOrderAccount2() {
72+
await enableMetamask();
73+
const gateway = new Gateway(provider);
74+
const signature = await gateway.sign(order).catch(e => {
75+
throw e;
76+
});
77+
config.signatureAccount2 = signature;
7178
}
7279

7380
export async function performOrder() {
7481
const gateway = new Gateway(provider);
75-
return gateway.perform(order, [config.signature]);
82+
return gateway.perform(order, [
83+
config.signatureAccount1,
84+
config.signatureAccount2
85+
]);
7686
}

0 commit comments

Comments
 (0)