-
Notifications
You must be signed in to change notification settings - Fork 148
/
zeroTokenOperations.js
195 lines (163 loc) · 7.27 KB
/
zeroTokenOperations.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// This example is for HIP-564 described here: https://hips.hedera.com/hip/hip-564
import * as hashgraph from "@hashgraph/sdk";
import ContractHelper from "./ContractHelper.js";
import contract from "./precompile-example/ZeroTokenOperations.json" assert { type: "json" };
import dotenv from "dotenv";
dotenv.config();
//Steps 1-5 are executed through ContractHelper and calling HIP564Example Contract.
//Step 6 is executed through the SDK
async function main() {
// Keys should be ED25519
// TODO: Fix the wallet to work with ECDSA
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
process.env.HEDERA_NETWORK == null
) {
throw new Error(
"Environment variables OPERATOR_ID, HEDERA_NETWORK, and OPERATOR_KEY are required.",
);
}
const myAccountId = hashgraph.AccountId.fromString(process.env.OPERATOR_ID);
const provider = new hashgraph.LocalProvider();
const aliceProvider = new hashgraph.LocalProvider();
const wallet = new hashgraph.Wallet(
process.env.OPERATOR_ID,
process.env.OPERATOR_KEY,
provider,
);
const operatorPrivateKey = hashgraph.PrivateKey.fromStringDer(
process.env.OPERATOR_KEY,
);
const operatorPublicKey = operatorPrivateKey.publicKey;
const operatorAccountId = hashgraph.AccountId.fromString(
process.env.OPERATOR_ID,
);
const alicePrivateKey = hashgraph.PrivateKey.generateED25519();
const alicePublicKey = alicePrivateKey.publicKey;
try {
let transaction = await new hashgraph.AccountCreateTransaction()
.setKey(alicePublicKey)
.setInitialBalance(hashgraph.Hbar.fromString("10"))
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
let response = await transaction.executeWithSigner(wallet);
const aliceAccountId = (await response.getReceiptWithSigner(wallet))
.accountId;
const walletWithAlice = new hashgraph.Wallet(
aliceAccountId,
alicePrivateKey,
aliceProvider,
);
// Instantiate ContractHelper
// The contract bytecode is located on the `object` field
const contractBytecode = /** @type {string} */ (
contract.bytecode.object
);
const contractHelper = await ContractHelper.init(
contractBytecode,
new hashgraph.ContractFunctionParameters()
.addAddress(wallet.getAccountId().toSolidityAddress())
.addAddress(aliceAccountId.toSolidityAddress()),
wallet,
);
// Update the signer to have contractId KeyList (this is by security requirement)
let accountUpdateOpratorTransaction =
await new hashgraph.AccountUpdateTransaction()
.setAccountId(operatorAccountId)
.setKey(
new hashgraph.KeyList(
[operatorPublicKey, contractHelper.contractId],
1,
),
)
.freezeWithSigner(wallet);
accountUpdateOpratorTransaction =
await accountUpdateOpratorTransaction.signWithSigner(wallet);
await accountUpdateOpratorTransaction.executeWithSigner(wallet);
// Update the Alice account to have contractId KeyList (this is by security requirement)
let accountUpdateAliceTransaction =
await new hashgraph.AccountUpdateTransaction()
.setAccountId(aliceAccountId)
.setKey(
new hashgraph.KeyList(
[alicePublicKey, contractHelper.contractId],
1,
),
)
.freezeWithSigner(walletWithAlice);
accountUpdateAliceTransaction =
await accountUpdateAliceTransaction.signWithSigner(walletWithAlice);
await accountUpdateAliceTransaction.executeWithSigner(walletWithAlice);
// Configure steps in ContracHelper
contractHelper
.setPayableAmountForStep(0, new hashgraph.Hbar(40))
.addSignerForStep(1, alicePrivateKey);
// step 0 creates a fungible token
// step 1 Associate with account
// step 2 transfer the token by passing a zero value
// step 3 mint the token by passing a zero value
// step 4 burn the token by passing a zero value
// step 5 wipe the token by passing a zero value
// step 6 use SDK and transfer passing a zero value
await contractHelper.executeSteps(
/* from step */ 0,
/* to step */ 5,
wallet,
);
// step 6 use SDK and transfer passing a zero value
//Create Fungible Token
console.log(`Attempting to execute step 6`);
let tokenCreateTransaction =
await new hashgraph.TokenCreateTransaction()
.setTokenName("Black Sea LimeChain Token")
.setTokenSymbol("BSL")
.setTreasuryAccountId(myAccountId)
.setInitialSupply(10000) // Total supply = 10000 / 10 ^ 2
.setDecimals(2)
.setAutoRenewAccountId(myAccountId)
.freezeWithSigner(wallet);
tokenCreateTransaction =
await tokenCreateTransaction.signWithSigner(wallet);
let responseTokenCreate =
await tokenCreateTransaction.executeWithSigner(wallet);
const tokenId = (await responseTokenCreate.getReceiptWithSigner(wallet))
.tokenId;
//Associate Token with Account
// Accounts on hedera have to opt in to receive any types of token that aren't HBAR
const tokenAssociateTransaction =
await new hashgraph.TokenAssociateTransaction()
.setAccountId(aliceAccountId)
.setTokenIds([tokenId])
.freezeWithSigner(walletWithAlice);
const signedTxForAssociateToken =
await tokenAssociateTransaction.signWithSigner(walletWithAlice);
const txResponseAssociatedToken =
await signedTxForAssociateToken.executeWithSigner(wallet);
const status = (
await txResponseAssociatedToken.getReceiptWithSigner(wallet)
).status;
console.log("Associate Status", status.toString());
//Transfer token
const transferToken = await new hashgraph.TransferTransaction()
.addTokenTransfer(tokenId, myAccountId, 0) // deduct 0 tokens
.addTokenTransfer(tokenId, aliceAccountId, 0) // increase balance by 0
.freezeWithSigner(wallet);
const signedTransferTokenTX =
await transferToken.signWithSigner(wallet);
const txResponseTransferToken =
await signedTransferTokenTX.executeWithSigner(wallet);
//Verify the transaction reached consensus
const transferReceipRecord =
await txResponseTransferToken.getRecordWithSigner(wallet);
console.log(
`step 6 completed, and returned valid result. (TransactionId "${transferReceipRecord.transactionId.toString()}")`,
);
console.log("All steps completed with valid results.");
} catch (error) {
console.error(error);
}
provider.close();
aliceProvider.close();
}
void main();