Skip to content

Commit 20819b9

Browse files
author
GeunWoo Kim
committed
Set network id for the SDK
1 parent d1073ba commit 20819b9

File tree

7 files changed

+237
-5
lines changed

7 files changed

+237
-5
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ npm-debug.log*
1414
yarn-debug.log*
1515
yarn-error.log*
1616

17+
/examples/package.json
18+
/examples/package-lock.json
19+
/examples/yarn.lock
20+
/examples/node_modules
21+
1722
# Editor directories and files
1823
.idea
1924
.vscode

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@types/validator": "^10.11.0",
2626
"chai": "^4.2.0",
2727
"codechain-primitives": "^1.0.0",
28-
"codechain-sdk": "^1.1.0",
28+
"codechain-sdk": "^1.2.0",
2929
"cors": "^2.8.5",
3030
"express": "^4.16.4",
3131
"mocha": "^5.2.0",

server/engine/matching.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import { OrderAttriubutes, OrderInstance } from "../models/order";
1717

1818
const env: string = process.env.NODE_ENV || "development";
1919
const rpcServer: string = require("../config/dex.json").node[env].rpc;
20-
const sdk = new SDK({ server: rpcServer });
20+
const networkId: string = require("../config/dex.json").node[env]["network-id"];
21+
const sdk = new SDK({ server: rpcServer, networkId });
2122
const DEX_ASSET_ADDRESS = Config["dex-asset-address"];
2223
// const passpharase = Config["dex-passphrase"];
2324
const FEE_RATE = Config["fee-rate"];
@@ -463,8 +464,8 @@ async function matchSame(
463464
const relayedRemainedAsset = isFeePayingOrder
464465
? relayedAmount - relayedOrder.assetQuantityFrom.value.toNumber()
465466
: relayedAmount -
466-
relayedOrder.assetQuantityFrom.value.toNumber() -
467-
relayedOrder.assetQuantityFee.value.toNumber();
467+
relayedOrder.assetQuantityFrom.value.toNumber() -
468+
relayedOrder.assetQuantityFee.value.toNumber();
468469
if (relayedRemainedAsset > 0) {
469470
transferTx.addOutputs({
470471
recipient: relayedOrderAddress,

server/example/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"axios": "^0.18.0"
8+
}
9+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
const SDK = require("codechain-sdk");
2+
const axios = require('axios');
3+
4+
const sdk = new SDK({
5+
server: "http://127.0.0.1:8080",
6+
networkId: "tc"
7+
});
8+
9+
const delay = ms => new Promise(res => setTimeout(res, ms));
10+
11+
const ACCOUNT_ADDRESS = "tccq9h7vnl68frvqapzv3tujrxtxtwqdnxw6yamrrgd";
12+
const ACCOUNT_SECRET = "ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd";
13+
const FEE_ASSET_TYPE = "0x0000000000000000000000000000000000000000";
14+
15+
16+
const shardId = 0;
17+
(async () => {
18+
const DEX_ASSET_ADDRESS = await sdk.key.createAssetAddress();
19+
const aliceAddress = await sdk.key.createAssetAddress({
20+
type: "P2PKH"
21+
});
22+
const bobAddress = await sdk.key.createAssetAddress({
23+
type: "P2PKH"
24+
});
25+
26+
// Mint gold asset for Alice
27+
const goldAssetScheme = sdk.core.createAssetScheme({
28+
shardId,
29+
metadata: {
30+
name: "Gold",
31+
description: "An asset example",
32+
icon_url: "https://gold.image/"
33+
},
34+
supply: 10000,
35+
registrar: null
36+
});
37+
const goldMintTx = sdk.core.createMintAssetTransaction({
38+
scheme: goldAssetScheme,
39+
recipient: aliceAddress
40+
});
41+
let seq = await sdk.rpc.chain.getSeq(ACCOUNT_ADDRESS);
42+
await sdk.rpc.chain.sendSignedTransaction(
43+
goldMintTx.sign({
44+
secret: ACCOUNT_SECRET,
45+
fee: 100000,
46+
seq
47+
})
48+
);
49+
console.log("Send gold mint")
50+
let transferTxResults = await sdk.rpc.chain.getTransactionResultsByTracker(
51+
goldMintTx.tracker(), {
52+
timeout: 300 * 1000
53+
}
54+
);
55+
if (!transferTxResults[0]) {
56+
throw Error(
57+
`GoldMintTransaction failed: ${JSON.stringify(transferTxResults[0])}`
58+
);
59+
}
60+
const gold = goldMintTx.getMintedAsset();
61+
62+
// Mint silver asset for Bob
63+
const silverAssetScheme = sdk.core.createAssetScheme({
64+
shardId,
65+
metadata: {
66+
name: "Silver",
67+
description: "An asset example",
68+
icon_url: "https://silver.image/"
69+
},
70+
supply: 100000,
71+
registrar: null
72+
});
73+
const silverMintTx = sdk.core.createMintAssetTransaction({
74+
scheme: silverAssetScheme,
75+
recipient: bobAddress
76+
});
77+
seq = await sdk.rpc.chain.getSeq(ACCOUNT_ADDRESS);
78+
await sdk.rpc.chain.sendSignedTransaction(
79+
silverMintTx.sign({
80+
secret: ACCOUNT_SECRET,
81+
fee: 100000,
82+
seq
83+
})
84+
);
85+
console.log("Send silver mint")
86+
transferTxResults = await sdk.rpc.chain.getTransactionResultsByTracker(
87+
silverMintTx.tracker(), {
88+
timeout: 300 * 1000
89+
}
90+
);
91+
if (!transferTxResults[0]) {
92+
throw Error(
93+
`SilverMintTransaction failed: ${JSON.stringify(transferTxResults[0])}`
94+
);
95+
}
96+
const silver = silverMintTx.getMintedAsset();
97+
98+
// Wrap 1000 CCC into the Wrapped CCC asset type and send to bob.
99+
const wrapCCC = sdk.core.createWrapCCCTransaction({
100+
shardId: 0,
101+
recipient: bobAddress,
102+
quantity: 1000,
103+
payer: ACCOUNT_ADDRESS
104+
});
105+
seq = await sdk.rpc.chain.getSeq(ACCOUNT_ADDRESS);
106+
await sdk.rpc.chain.sendSignedTransaction(
107+
wrapCCC.sign({
108+
secret: ACCOUNT_SECRET,
109+
fee: 100000,
110+
seq
111+
})
112+
);
113+
console.log("wCCC mint")
114+
const wccc = wrapCCC.getAsset();
115+
116+
117+
const goldInput = gold.createTransferInput();
118+
const silverInput = silver.createTransferInput();
119+
const wcccInput = wccc.createTransferInput();
120+
121+
const expiration = Math.round(Date.now() / 1000) + 120;
122+
// Order for Alice
123+
const orderA = sdk.core.createOrder({
124+
assetTypeFrom: gold.assetType,
125+
assetTypeTo: silver.assetType,
126+
shardIdFrom: shardId,
127+
shardIdTo: shardId,
128+
assetQuantityFrom: 10,
129+
assetQuantityTo: 100,
130+
expiration,
131+
originOutputs: [goldInput.prevOut],
132+
recipientFrom: aliceAddress
133+
});
134+
await sdk.key.signTransactionInputWithOrder(goldInput, orderA);
135+
136+
// Order for Bob
137+
const orderB = sdk.core.createOrder({
138+
assetTypeFrom: silver.assetType,
139+
assetTypeTo: gold.assetType,
140+
assetTypeFee: FEE_ASSET_TYPE,
141+
shardIdFrom: shardId,
142+
shardIdTo: shardId,
143+
shardIdFee: shardId,
144+
assetQuantityFrom: 100,
145+
assetQuantityTo: 10,
146+
assetQuantityFee: 100,
147+
expiration,
148+
originOutputs: [silverInput.prevOut, wcccInput.prevOut],
149+
recipientFrom: bobAddress,
150+
recipientFee: DEX_ASSET_ADDRESS
151+
});
152+
await sdk.key.signTransactionInputWithOrder(silverInput, orderB);
153+
await sdk.key.signTransactionInputWithOrder(wcccInput, orderB);
154+
155+
// send post request
156+
await axios.post('http://localhost:8448/api/orders', {
157+
assetList: [goldInput],
158+
order: orderA,
159+
makerAddress: aliceAddress.toString(),
160+
splitTx: null
161+
})
162+
.then(function (response) {
163+
console.log(response);
164+
})
165+
.catch(function (error) {
166+
console.log(error);
167+
});
168+
await delay(10000)
169+
170+
await axios.post('http://localhost:8448/api/orders', {
171+
assetList: [silverInput, wcccInput],
172+
order: orderB,
173+
makerAddress: bobAddress.toString(),
174+
splitTx: null
175+
})
176+
.then(function (response) {
177+
console.log(response);
178+
})
179+
.catch(function (error) {
180+
console.log(error);
181+
});
182+
})().catch(console.error)

server/example/yarn.lock

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
axios@^0.18.0:
6+
version "0.18.0"
7+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
8+
integrity sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=
9+
dependencies:
10+
follow-redirects "^1.3.0"
11+
is-buffer "^1.1.5"
12+
13+
debug@^3.2.6:
14+
version "3.2.6"
15+
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
16+
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
17+
dependencies:
18+
ms "^2.1.1"
19+
20+
follow-redirects@^1.3.0:
21+
version "1.7.0"
22+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.7.0.tgz#489ebc198dc0e7f64167bd23b03c4c19b5784c76"
23+
integrity sha512-m/pZQy4Gj287eNy94nivy5wchN3Kp+Q5WgUPNy5lJSZ3sgkVKSYV/ZChMAQVIgx1SqfZ2zBZtPA2YlXIWxxJOQ==
24+
dependencies:
25+
debug "^3.2.6"
26+
27+
is-buffer@^1.1.5:
28+
version "1.1.6"
29+
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
30+
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
31+
32+
ms@^2.1.1:
33+
version "2.1.1"
34+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a"
35+
integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ codechain-primitives@^1.0.0:
718718
ripemd160 "^2.0.2"
719719
rlp "^2.1.0"
720720

721-
codechain-sdk@^1.1.0:
721+
codechain-sdk@^1.2.0:
722722
version "1.2.0"
723723
resolved "https://registry.yarnpkg.com/codechain-sdk/-/codechain-sdk-1.2.0.tgz#10ce7dd3536c9b3549dec604f07060ecee9412f1"
724724
integrity sha512-2CoO/9xSQY34YaSAkf3DEMVY0hmM1pZYDbcXl9euS49Rds/5QsRuLs+u78EPrruB5DWltb7J+OCDJIQfBdhIXQ==

0 commit comments

Comments
 (0)