-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (112 loc) · 4.82 KB
/
index.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
console.clear();
const {
Client,
PrivateKey,
AccountCreateTransaction,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
AccountId,
AccountBalanceQuery,
Hbar,
TransferTransaction,
} = require("@hashgraph/sdk");
require("dotenv").config();
const fs = require("fs");
const myAccountId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY) ;
const client = Client.forTestnet().setOperator(myAccountId, myPrivateKey);
async function main() {
const contractBytecode = fs.readFileSync("DonerContract_sol_DonerContract.bin")
const fileCreateTransaction = new FileCreateTransaction()
.setContents(contractBytecode)
.setKeys([myPrivateKey])
.freezeWith(client)
const fileCreateSign = await fileCreateTransaction.sign(myPrivateKey);
const fileCreateSubmit = await fileCreateSign.execute(client);
const fileCreateReceipt = fileCreateSubmit.getReceipt(client);
const bytecodeFileId = (await fileCreateReceipt).fileId;
console.log("bytecodeFileId : " +bytecodeFileId.toString() + "\n" );
const contractInstantiateTransaction = new ContractCreateTransaction()
.setBytecodeFileId(bytecodeFileId)
.setGas(100000)
.setConstructorParameters(new ContractFunctionParameters().addString("mahmoud").addUint256(1000))
const contractInstantiateSubmit = await contractInstantiateTransaction.execute(client)
const contractInstantiateReceipt = await contractInstantiateSubmit.getReceipt(client)
const contractId = contractInstantiateReceipt.contractId
const contractAddress = contractId.toSolidityAddress();
console.log("smart contract id : " +contractId)
console.log("smart contract id in solidity format : " +contractAddress)
async function getAmountByName(donerName){
const contractQueryTransaction = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("getAmount",new ContractFunctionParameters().addString(donerName));
const contractQuerySubmit = await contractQueryTransaction.execute(client);
const contractQueryResult = contractQuerySubmit.getUint256(0)
console.log("query result : " +contractQueryResult)
return contractQueryResult
}
async function setAmount(donerName,amount){
const contractExecuteTransaction = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(100000)
.setFunction("setAmount",new ContractFunctionParameters().addString(donerName).addUint256(amount))
const contractExecuteSubmit = await contractExecuteTransaction.execute(client)
const contractExecuteReceipt = await contractExecuteSubmit.getReceipt(client)
console.log(" contract status : " + contractExecuteReceipt.status.toString())
}
await setAmount("redApple",163)
console.log(getAmountByName("redApple"))
}
main();
// if (myAccountId == null || myPrivateKey == null) {
// throw new Error(
// "Environment variables myAccountId and myPrivateKey must be present"
// );
// }
// console.log(myAccountId);
// const client = Client.forTestnet();
// client.setOperator(myAccountId, myPrivateKey);
// const newAccountPrivateKey = await PrivateKey.generateED25519();
// const newAccountPublicKey = newAccountPrivateKey.publicKey;
// // created a new account
// // ! important : the client is just a way or key to interact with the API
// const newAccount = await new AccountCreateTransaction()
// .setKey(newAccountPublicKey)
// .setInitialBalance(Hbar.fromTinybars(1000))
// .execute(client);
// // ! we created an account so we can get our receipt
// // ! which contains all "transaction" info
// const getReceipt = await newAccount.getReceipt(client);
// const newAccountId = getReceipt.accountId;
// console.log("The new account ID is: " + newAccountId);
// console.log("The new account ID is: " + myAccountId);
// const accountBalance = await new AccountBalanceQuery()
// .setAccountId(newAccountId)
// .execute(client);
// console.log(
// "The new account balance is: " +
// accountBalance.hbars.toTinybars() +
// " tinybar."
// );
// const sendHbar = await new TransferTransaction()
// .addHbarTransfer(myAccountId, Hbar.fromTinybars(-10))
// .addHbarTransfer(newAccountId, Hbar.fromTinybars(10))
// .execute(client);
// const transactionReceipt = await sendHbar.getReceipt(client);
// console.log(transactionReceipt.status.toString());
// const queryCost = await new AccountBalanceQuery()
// .setAccountId(newAccountId)
// .getCost(client);
// const getNewBalance = await new AccountBalanceQuery()
// .setAccountId(newAccountId)
// .execute(client);
// console.log(
// "The account balance after the transfer is: " +
// getNewBalance.hbars.toTinybars() +
// " tinybar."
// );
// console.log("The cost of query is: " + transactionReceipt.tokenId);