-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmint_fungible_token.js
96 lines (75 loc) · 3.04 KB
/
mint_fungible_token.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
import Archethic, { Crypto, Utils } from "@archethicjs/sdk"
import { randomBytes } from "crypto"
import { requestFaucet } from "./utils.js"
import { getLogger } from "./logger.js"
const logger = getLogger()
const originPrivateKey = Utils.originPrivateKey;
async function run() {
return new Promise(async function (resolve, reject) {
try {
const endpoint = process.env["ENDPOINT"] || "https://testnet.archethic.net"
const archethic = new Archethic(endpoint)
await archethic.connect()
const seed = randomBytes(32)
const address = Crypto.deriveAddress(seed)
logger.debug("Request funds from faucet...")
await requestFaucet(Utils.uint8ArrayToHex(address), endpoint)
const senderBalance = await archethic.network.getBalance(address)
if (Utils.fromBigInt(senderBalance.uco) != 100) {
reject(`Invalid balance for the sender's address`)
return
}
logger.debug("Funds received from faucet")
const tx = archethic.transaction.new()
.setType("token")
.setContent(JSON.stringify({
supply: Utils.toBigInt(100),
name: "MyToken",
type: "fungible",
symbol: "MTK",
properties: {
description: "This is token used to test token creation"
}
}))
.build(seed)
.originSign(originPrivateKey)
tx
.on("sent", () => {
logger.debug("Token transaction sent")
logger.debug("Await validation ...")
})
.on("requiredConfirmation", async (_confirmations, sender) => {
sender.unsubscribe()
logger.debug(`Token transaction created - ${Utils.uint8ArrayToHex(tx.address)}`)
const senderBalance = await archethic.network.getBalance(address)
const { amount: tokenAmount } = senderBalance.token.find(x => {
return x.address.toLowerCase() == Utils.uint8ArrayToHex(tx.address)
})
if (Utils.fromBigInt(tokenAmount) != 100) {
reject(`Invalid balance for the sender's address (${Utils.uint8ArrayToHex(address)}) after send and should be 100 token`)
return
}
resolve("100 MKT tokens have been minted")
return
})
.on("error", (context, reason) => {
reject(`Token transaction failed - ${reason}`)
return
})
.send()
} catch (e) {
reject(e)
return
}
})
}
run()
.then((msg) => {
logger.info(msg)
return 0
})
.catch((msg) => {
logger.error(msg)
return 1
})
.then(logger.exit_when_flush)