|
| 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) |
0 commit comments