-
Notifications
You must be signed in to change notification settings - Fork 1
/
0_test_SDK_FT.js
245 lines (218 loc) · 8.68 KB
/
0_test_SDK_FT.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
console.clear();
require("dotenv").config();
const {
Client,
AccountId,
PrivateKey,
TokenCreateTransaction,
ContractExecuteTransaction,
TokenInfoQuery,
AccountBalanceQuery,
Hbar,
TransactionRecordQuery,
ContractInfoQuery,
TokenAssociateTransaction,
TransferTransaction,
ContractCreateFlow,
AccountAllowanceApproveTransaction,
AccountInfoQuery,
TopicCreateTransaction,
TokenMintTransaction,
NftId,
TokenType,
AccountAllowanceDeleteTransaction,
ContractFunctionParameters,
AccountCreateTransaction,
TransactionId,
} = require("@hashgraph/sdk");
const fs = require("fs");
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
client.setDefaultMaxTransactionFee(new Hbar(100));
async function main() {
// STEP 1 ===================================
console.log(`STEP 1 ===================================`);
const bytecode = fs.readFileSync("./0_test_sol_hbar2Contract.bin");
const initBalance = new Hbar(10);
const aliceKey = PrivateKey.generateED25519();
const [aliceSt, aliceId] = await accountCreateFcn(aliceKey, initBalance, client);
console.log(`- Alice's account: https://hashscan.io/#/testnet/account/${aliceId}`);
const bobKey = PrivateKey.generateED25519();
const [bobSt, bobId] = await accountCreateFcn(bobKey, initBalance, client);
console.log(`- Bob's account: https://hashscan.io/#/testnet/account/${bobId}`);
const treasuryKey = PrivateKey.generateED25519();
const [treasurySt, treasuryId] = await accountCreateFcn(treasuryKey, initBalance, client);
console.log(`- Treasury's account: https://hashscan.io/#/testnet/account/${treasuryId}`);
console.log(`- Done \n`);
// STEP 2 ===================================
console.log(`STEP 2 ===================================`);
//Create a token
const [tokenId, tokenInfo] = await ftCreate();
console.log(`- Token ID: ${tokenId}`);
console.log(`- Initial token supply: ${tokenInfo.totalSupply.low} \n`);
// STEP 3 ===================================
console.log(`STEP 3 ===================================`);
const tokenAssociateTx = new TokenAssociateTransaction()
.setTokenIds([tokenId])
.setAccountId(aliceId)
.freezeWith(client);
const tokenAssociateSign = await tokenAssociateTx.sign(aliceKey);
const tokenAssociateSubmit = await tokenAssociateSign.execute(client);
const tokenAssociateRx = await tokenAssociateSubmit.getReceipt(client);
console.log(`- Alice associated with token: ${tokenAssociateRx.status}`);
// STEP 4 ===================================
console.log(`STEP 4 ===================================`);
// const sendTx = new TransferTransaction()
// .addTokenTransfer(tokenId, treasuryId, -1)
// .addTokenTransfer(tokenId, aliceId, 1)
// .freezeWith(client);
// const sendSign = await sendTx.sign(treasuryKey);
// const sendSubmit = await sendSign.execute(client);
// const sendRx = await sendSubmit.getReceipt(client);
// console.log(`\n- Treasury2Alice transfer status: ${sendRx.status}`);
const allowanceTx = new AccountAllowanceApproveTransaction()
.approveTokenAllowance(tokenId, treasuryId, aliceId, 50)
.freezeWith(client);
const allowanceSign = await allowanceTx.sign(treasuryKey);
const allowanceSubmit = await allowanceSign.execute(client);
const allowanceRx = await allowanceSubmit.getReceipt(client);
console.log(`\n- Allowance approved: ${allowanceRx.status}\n`);
//
await balanceCheckerFcn(treasuryId, tokenId, client);
await balanceCheckerFcn(aliceId, tokenId, client);
await balanceCheckerFcn(bobId, tokenId, client);
//
// client.setOperator(aliceId, aliceyKey);
const approvedSendTx = new TransferTransaction()
.addApprovedTokenTransfer(tokenId, treasuryId, -10)
.addTokenTransfer(tokenId, bobId, 10)
.setTransactionId(TransactionId.generate(aliceId))
.freezeWith(client);
const approvedSendSign = await approvedSendTx.sign(aliceKey);
const approvedSendSubmit = await approvedSendSign.execute(client);
const approvedSendRx = await approvedSendSubmit.getReceipt(client);
console.log(`\n- Allowance transfer status: ${approvedSendRx.status}\n`);
await balanceCheckerFcn(treasuryId, tokenId, client);
await balanceCheckerFcn(aliceId, tokenId, client);
await balanceCheckerFcn(bobId, tokenId, client);
//
console.log(`end`);
// ========================================
// FUNCTIONS
async function tQueryFcn(tId) {
let info = await new TokenInfoQuery().setTokenId(tId).execute(client);
return info;
}
async function aQueryFcn(aId) {
let info = await new AccountInfoQuery().setAccountId(aId).execute(client);
return info;
}
async function bCheckerFcn(aId) {
let balanceCheckTx = await new AccountBalanceQuery().setAccountId(aId).execute(client);
return balanceCheckTx.hbars;
}
async function ftCreate() {
// CREATE FT
const tokenCreateTx = await new TokenCreateTransaction()
.setTokenName("hbarRocks")
.setTokenSymbol("HROK")
.setDecimals(0)
.setInitialSupply(100)
.setTreasuryAccountId(treasuryId)
// .setAdminKey(treasuryKey)
// .setSupplyKey(treasuryKey)
.freezeWith(client)
.sign(treasuryKey);
const tokenCreateSubmit = await tokenCreateTx.execute(client);
const tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
const tokenId = tokenCreateRx.tokenId;
// Token query
const tokenInfo = await tQueryFcn(tokenId);
return [tokenId, tokenInfo];
}
async function nftCreateMint() {
// DEFINE CUSTOM FEE SCHEDULE
// let nftCustomFee = await new CustomRoyaltyFee()
// .setNumerator(5)
// .setDenominator(10)
// .setFeeCollectorAccountId(treasuryId)
// .setFallbackFee(new CustomFixedFee().setHbarAmount(new Hbar(200)));
// IPFS CONTENT IDENTIFIERS FOR WHICH WE WILL CREATE NFTs
CID = [
"QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn",
"QmZ4dgAgt8owvnULxnKxNe8YqpavtVCXmc1Lt2XajFpJs9",
// "QmPzY5GxevjyfMUF5vEAjtyRoigzWp47MiKAtLBduLMC1T",
// "Qmd3kGgSrAwwSrhesYcY7K54f3qD7MDo38r7Po2dChtQx5",
// "QmWgkKz3ozgqtnvbCLeh7EaR1H8u5Sshx3ZJzxkcrT3jbw",
];
// CREATE NFT WITH CUSTOM FEE
let nftCreate = await new TokenCreateTransaction()
.setTokenName("Fall Collection")
.setTokenSymbol("LEAF")
.setTokenType(TokenType.NonFungibleUnique)
.setDecimals(0)
.setInitialSupply(0)
.setTreasuryAccountId(treasuryId)
// .setSupplyType(TokenSupplyType.Finite)
// .setMaxSupply(CID.length)
// .setCustomFees([nftCustomFee])
// .setAdminKey(adminKey)
.setSupplyKey(treasuryKey)
// .setPauseKey(pauseKey)
// .setFreezeKey(freezeKey)
// .setWipeKey(wipeKey)
.freezeWith(client);
let nftCreateTxSign = await nftCreate.sign(treasuryKey);
let nftCreateSubmit = await nftCreateTxSign.execute(client);
let nftCreateRx = await nftCreateSubmit.getReceipt(client);
let tokenId = nftCreateRx.tokenId;
console.log(`Created NFT with Token ID: ${tokenId} \n`);
// MINT NEW BATCH OF NFTs
nftLeaf = [];
for (var i = 0; i < CID.length; i++) {
nftLeaf[i] = await tokenMinterFcn(CID[i]);
console.log(`Created NFT ${tokenId} with serial: ${nftLeaf[i].serials[0].low}`);
}
async function tokenMinterFcn(CID) {
mintTx = await new TokenMintTransaction()
.setTokenId(tokenId)
.setMetadata([Buffer.from(CID)])
.freezeWith(client);
let mintTxSign = await mintTx.sign(treasuryKey);
let mintTxSubmit = await mintTxSign.execute(client);
let mintRx = await mintTxSubmit.getReceipt(client);
return mintRx;
}
var tokenInfo = await new TokenInfoQuery().setTokenId(tokenId).execute(client);
return [tokenId, tokenInfo];
}
async function accountCreateFcn(pvKey, iBal, client) {
const response = await new AccountCreateTransaction()
.setInitialBalance(iBal)
.setKey(pvKey.publicKey)
.setMaxAutomaticTokenAssociations(10)
.execute(client);
const receipt = await response.getReceipt(client);
return [receipt.status, receipt.accountId];
}
async function balanceCheckerFcn(acId, tkId, client) {
let balanceCheckTx = [];
try {
balanceCheckTx = await new AccountBalanceQuery().setAccountId(acId).execute(client);
console.log(
`- Balance of account ${acId}: ${balanceCheckTx.hbars.toString()} + ${balanceCheckTx.tokens._map.get(
tkId.toString()
)} units of token ${tkId}`
);
} catch {
balanceCheckTx = await new AccountBalanceQuery().setContractId(acId).execute(client);
console.log(
`- Balance of contract ${acId}: ${balanceCheckTx.hbars.toString()} + ${balanceCheckTx.tokens._map.get(
tkId.toString()
)} units of token ${tkId}`
);
}
}
}
main();