-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_uco.js
More file actions
91 lines (70 loc) · 3.05 KB
/
send_uco.js
File metadata and controls
91 lines (70 loc) · 3.05 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
import Archethic, { Crypto, Utils } from "@archethicjs/sdk"
import { requestFaucet } from "./utils.js"
import { randomBytes } from "crypto"
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 recipient_address = Crypto.deriveAddress(randomBytes(32))
const tx = archethic.transaction.new()
.setType("transfer")
.addUCOTransfer(recipient_address, Utils.toBigInt(10))
.build(seed)
.originSign(originPrivateKey)
tx
.on("sent", () => {
logger.debug("UCO transaction sent")
logger.debug("Await validation ...")
})
.on("requiredConfirmation", async (_confirmations, sender) => {
sender.unsubscribe()
logger.debug(`UCO transaction created - ${Utils.uint8ArrayToHex(tx.address)}`)
const recipientBalance = await archethic.network.getBalance(recipient_address)
if (Utils.fromBigInt(recipientBalance.uco) != 10) {
reject(`Invalid balance for the recipient's address (${Utils.uint8ArrayToHex(recipient_address)}) after send and should be 10 UCO`)
return
}
const senderBalance = await archethic.network.getBalance(address)
if (Math.ceil(Utils.fromBigInt(senderBalance.uco)) != 90) {
reject(`Invalid balance for the sender's address (${Utils.uint8ArrayToHex(address)}) after send and should be ~90 UCO`)
return
}
resolve(`${Utils.uint8ArrayToHex(recipient_address)} received 10 UCO`)
return
})
.on("error", (context, reason) => {
reject(`UCO 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)