-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreezeCoin.js
52 lines (45 loc) · 2.12 KB
/
FreezeCoin.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
const Integer = Java.type("java.lang.Integer")
const Long = Java.type("java.lang.Long")
const Address = Java.type("org.ergoplatform.appkit.Address")
const RestApiErgoClient = Java.type("org.ergoplatform.appkit.RestApiErgoClient")
const ErgoClientException = Java.type("org.ergoplatform.appkit.ErgoClientException")
const ConstantsBuilder = Java.type("org.ergoplatform.appkit.ConstantsBuilder")
const ErgoToolConfig = Java.type("org.ergoplatform.appkit.config.ErgoToolConfig")
const Parameters = Java.type("org.ergoplatform.appkit.Parameters")
const amountToPay = Long.parseLong(process.argv[2]);
const conf = ErgoToolConfig.load("freeze_coin_config.json");
const nodeConf = conf.getNode();
const newBoxSpendingDelay = Integer.parseInt(conf.getParameters().get("newBoxSpendingDelay"));
const ownerAddress = Address.create(conf.getParameters().get("ownerAddress"));
const ergoClient = RestApiErgoClient.create(nodeConf);
const txJson = ergoClient.execute(function (ctx) {
const wallet = ctx.getWallet();
const totalToSpend = amountToPay + Parameters.MinFee;
const boxes = wallet.getUnspentBoxes(totalToSpend);
if (!boxes.isPresent())
throw new ErgoClientException("Not enough coins in the wallet to pay " + totalToSpend, null);
const prover = ctx.newProverBuilder()
.withMnemonic(
nodeConf.getWallet().getMnemonic(),
nodeConf.getWallet().getPassword())
.build();
const txB = ctx.newTxBuilder();
const newBox = txB.outBoxBuilder()
.value(amountToPay)
.contract(ctx.compileContract(
ConstantsBuilder.create()
.item("freezeDeadline", ctx.getHeight() + newBoxSpendingDelay)
.item("ownerPk", ownerAddress.getPublicKey())
.build(),
"{ sigmaProp(HEIGHT > freezeDeadline) && ownerPk }"))
.build();
const tx = txB.boxesToSpend(boxes.get())
.outputs(newBox)
.fee(Parameters.MinFee)
.sendChangeTo(prover.getP2PKAddress())
.build();
const signed = prover.sign(tx);
const txId = ctx.sendTransaction(signed);
return signed.toJson(true);
});
print(txJson);