-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy_contract.js
More file actions
170 lines (137 loc) · 5.57 KB
/
deploy_contract.js
File metadata and controls
170 lines (137 loc) · 5.57 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
let archethic
function contractCode() {
return `@version 1
condition triggered_by: transaction, on: exec(), as: [
content: Crypto.hash(contract.code)
]
actions triggered_by: transaction, on: exec() do
Contract.set_content "Contract executed"
end`
}
async function getContractTransaction(seed) {
const secretKey = Crypto.randomSecretKey();
const cipher = Crypto.aesEncrypt(seed, secretKey);
const storageNoncePublicKey = await archethic.network.getStorageNoncePublicKey()
const encryptedSecretKey = Crypto.ecEncrypt(secretKey, storageNoncePublicKey);
const authorizedKeys = [
{
publicKey: storageNoncePublicKey,
encryptedSecretKey: encryptedSecretKey,
}
]
return archethic.transaction.new()
.setType("contract")
.setCode(contractCode())
.addOwnership(cipher, authorizedKeys)
.build(seed, 0)
.originSign(originPrivateKey)
}
function getCallTransaction(contractAddress, callerSeed) {
const hashCode = Utils.uint8ArrayToHex(Crypto.hash(contractCode()))
return archethic.transaction.new()
.setType("transfer")
.setContent(hashCode.toUpperCase().slice(2))
.addRecipient(contractAddress, "exec")
.build(callerSeed, 0)
.originSign(originPrivateKey)
}
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 contractSeed = randomBytes(32)
const contractAddress = Crypto.deriveAddress(contractSeed)
const callerSeed = randomBytes(32)
const callerAddress = Crypto.deriveAddress(callerSeed)
logger.debug("Request funds from faucet...")
await requestFaucet(Utils.uint8ArrayToHex(contractAddress), endpoint)
await requestFaucet(Utils.uint8ArrayToHex(callerAddress), endpoint)
const contractBalance = await archethic.network.getBalance(contractAddress)
if (Utils.fromBigInt(contractBalance.uco) != 100) {
reject(`Invalid balance for the contract's address`)
return
}
const callerBalance = await archethic.network.getBalance(callerAddress)
if (Utils.fromBigInt(callerBalance.uco) != 100) {
reject(`Invalid balance for the caller's address`)
return
}
const contractTx = await getContractTransaction(contractSeed)
contractTx
.on("sent", () => {
logger.debug("Contract transaction sent")
logger.debug("Await validation ...")
})
.on("error", (context, reason) => {
reject(`Contract transaction failed - ${reason}`)
return
})
.on("requiredConfirmation", (confirmations, sender) => {
sender.unsubscribe()
logger.debug(`Contract transaction created - ${Utils.uint8ArrayToHex(contractTx.address)}`)
const callTx = getCallTransaction(contractTx.address, callerSeed)
callTx
.on("sent", () => {
logger.debug("Contract's call transaction sent")
logger.debug("Await validation ...")
})
.on("error", (context, reason) => {
reject(`Contract's call transaction failed - ${reason}`)
return
})
.on("requiredConfirmation", async (confirmations, sender) => {
sender.unsubscribe()
logger.debug(`Contract's call transaction created - ${Utils.uint8ArrayToHex(callTx.address)}`)
awaitTriggeredTransaction(archethic, contractTx.address)
.then(() => {
resolve("Contract's call transaction executed with success")
})
.catch(reject)
})
.send()
})
.send()
} catch (e) {
reject(e)
return
}
})
}
async function awaitTriggeredTransaction(archethic, contractAddress, retries = 0) {
await new Promise(r => setTimeout(r, 3000));
const { lastTransaction: { data: { content: lastContent } } } = await archethic.network.rawGraphQLQuery(`query
{
lastTransaction(address: "${Utils.uint8ArrayToHex(contractAddress)}") {
data {
content
}
}
}`)
if (lastContent != "Contract executed") {
if (retries == 3) {
throw "Contract self trigger transaction not executed"
}
else {
logger.info(`Retry attempt #${retries + 1}`)
await awaitTriggeredTransaction(archethic, contractAddress, retries + 1)
}
}
}
run()
.then((msg) => {
logger.info(msg)
return 0
})
.catch((msg) => {
logger.error(msg)
return 1
})
.then(logger.exit_when_flush)