-
Notifications
You must be signed in to change notification settings - Fork 17
/
mint-and-transfer.js
97 lines (89 loc) · 2.89 KB
/
mint-and-transfer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const SDK = require("codechain-sdk");
const sdk = new SDK({
server: process.env.CODECHAIN_RPC_HTTP || "http://localhost:8080",
networkId: process.env.CODECHAIN_NETWORK_ID || "tc"
});
const ACCOUNT_ADDRESS =
process.env.ACCOUNT_ADDRESS ||
"tccq9h7vnl68frvqapzv3tujrxtxtwqdnxw6yamrrgd";
const ACCOUNT_PASSPHRASE = process.env.ACCOUNT_PASSPHRASE || "satoshi";
(async () => {
const shardId = 0;
const aliceAddress = await sdk.key.createAssetAddress();
const bobAddress = "tcaqyqckq0zgdxgpck6tjdg4qmp52p2vx3qaexqnegylk";
// Create asset named Gold. Total supply of Gold is 10000. The approver is set
// to null, which means this type of asset can be transferred freely.
const goldAssetScheme = sdk.core.createAssetScheme({
shardId,
metadata: {
name: "Gold",
description: "An asset example",
icon_url: "https://gold.image/"
},
supply: 10000,
approver: null
});
const mintTx = sdk.core.createMintAssetTransaction({
scheme: goldAssetScheme,
recipient: aliceAddress
});
const firstGold = mintTx.getMintedAsset();
const transferTx = sdk.core
.createTransferAssetTransaction()
.addInputs(firstGold)
.addOutputs(
{
recipient: bobAddress,
quantity: 3000,
assetType: firstGold.assetType,
shardId
},
{
recipient: aliceAddress,
quantity: 7000,
assetType: firstGold.assetType,
shardId
}
);
await sdk.key.signTransactionInput(transferTx, 0);
await sdk.rpc.chain.sendTransaction(mintTx, {
account: ACCOUNT_ADDRESS,
passphrase: ACCOUNT_PASSPHRASE
});
await sdk.rpc.chain.sendTransaction(transferTx, {
account: ACCOUNT_ADDRESS,
passphrase: ACCOUNT_PASSPHRASE
});
const mintTxResults = await sdk.rpc.chain.getTransactionResultsByTracker(
mintTx.tracker(),
{
timeout: 300 * 1000
}
);
if (!mintTxResults[0]) {
throw Error(
`AssetMintTransaction failed: ${JSON.stringify(
mintTxResults[0].error
)}`
);
}
const transferTxResults = await sdk.rpc.chain.getTransactionResultsByTracker(
transferTx.tracker(),
{
timeout: 300 * 1000
}
);
if (!transferTxResults[0]) {
throw Error(
`AssetTransferTransaction failed: ${JSON.stringify(
transferTxResults[0].error
)}`
);
}
// Unspent Bob's 3000 golds
console.log(await sdk.rpc.chain.getAsset(transferTx.tracker(), 0, shardId));
// Unspent Alice's 7000 golds
console.log(await sdk.rpc.chain.getAsset(transferTx.tracker(), 1, shardId));
})().catch(err => {
console.error(`Error:`, err);
});