-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_token.js
More file actions
134 lines (110 loc) · 5.15 KB
/
send_token.js
File metadata and controls
134 lines (110 loc) · 5.15 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import Archethic, { Crypto, Utils } from "@archethicjs/sdk"
import { randomBytes } from "crypto"
import { requestFaucet, findTokenBalance } from "./utils.js"
import { getLogger } from "./logger.js"
const logger = getLogger()
const originPrivateKey = Utils.originPrivateKey;
let archethic
function getMintTokenTransaction() {
return 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"
}
}))
}
function getTokenTransferTransaction(recipient_address, amount, tokenAddress) {
return archethic.transaction.new()
.setType("transfer")
.addTokenTransfer(recipient_address, Utils.toBigInt(amount), tokenAddress)
}
async function run() {
return new Promise(async function (resolve, reject) {
try {
const endpoint = process.env["ENDPOINT"] || "https://testnet.archethic.net"
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 mintTx = getMintTokenTransaction()
.build(seed, 0)
.originSign(originPrivateKey)
mintTx
.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(mintTx.address)}`)
const { amount: tokenAmount, address: tokenAddress } = await findTokenBalance(archethic, address, Utils.uint8ArrayToHex(mintTx.address).toUpperCase())
if (Utils.fromBigInt(tokenAmount) != 100) {
reject(`Invalid balance for the sender's address (${Utils.uint8ArrayToHex(address)}) after send and should be 100 token`)
return
}
logger.debug("100 MKT tokens have been minted")
const recipientAddress = Crypto.deriveAddress(randomBytes(32))
const transferTx = getTokenTransferTransaction(recipientAddress, 20, tokenAddress)
.build(seed, 1)
.originSign(originPrivateKey)
transferTx
.on("sent", () => {
logger.debug("Transfer transaction sent")
logger.debug("Await validation ...")
})
.on("requiredConfirmation", async (_confirmations, sender) => {
sender.unsubscribe()
logger.debug(`Transfer transaction created - ${Utils.uint8ArrayToHex(transferTx.address)}`)
const { amount: receivedTokens } = await findTokenBalance(archethic, recipientAddress, tokenAddress)
if (Utils.fromBigInt(receivedTokens) != 20) {
reject(`Invalid balance for the recipient's address (${Utils.uint8ArrayToHex(recipientAddress)}) after send and should be 20 token`)
return
}
const { amount: remainingTokens } = await findTokenBalance(archethic, Utils.uint8ArrayToHex(address), tokenAddress)
if (Utils.fromBigInt(remainingTokens) != 80) {
reject(`Invalid balance for the sender's address (${Utils.uint8ArrayToHex(address)}) after send and should be 80 tokens`)
return
}
resolve("Token transfered with success")
return
})
.on("error", (context, reason) => {
reject(`Transfer transaction failed - ${reason}`)
return
})
.send()
})
.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)