forked from hiero-ledger/hiero-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-contract-with-value.js
More file actions
145 lines (120 loc) · 5.26 KB
/
create-contract-with-value.js
File metadata and controls
145 lines (120 loc) · 5.26 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
import {
Wallet,
LocalProvider,
ContractCreateTransaction,
FileCreateTransaction,
ContractDeleteTransaction,
ContractCallQuery,
Hbar,
} from "@hiero-ledger/sdk";
import dotenv from "dotenv";
dotenv.config();
// Import the compiled contract
import payableContract from "./payable.json" with { type: "json" };
async function main() {
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 provider = new LocalProvider();
const wallet = new Wallet(
process.env.OPERATOR_ID,
process.env.OPERATOR_KEY,
provider,
);
// The contract bytecode is located on the `object` field
const contractByteCode = payableContract.object;
try {
// Create a file on Hedera which contains the contact bytecode.
// Note: The contract bytecode **must** be hex encoded, it should not
// be the actual data the hex represents
let transaction = await new FileCreateTransaction()
.setKeys([wallet.getAccountKey()])
.setContents(contractByteCode)
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
let fileTransactionResponse =
await transaction.executeWithSigner(wallet);
// Fetch the receipt for transaction that created the file
const fileReceipt =
await fileTransactionResponse.getReceiptWithSigner(wallet);
// The file ID is located on the transaction receipt
const fileId = fileReceipt.fileId;
console.log(`contract bytecode file: ${fileId.toString()}`);
// Create the contract
let createContractTransaction = await new ContractCreateTransaction()
// Set gas to create the contract
.setGas(300000)
// The contract bytecode must be set to the file ID containing the contract bytecode
.setBytecodeFileId(fileId)
// Set the admin key on the contract in case the contract should be deleted or
// updated in the future
.setAdminKey(wallet.getAccountKey())
// Set an initial amount in HBARs to be set to the contract when it is deployed (equivalent of `value`)
.setInitialBalance(50)
.freezeWithSigner(wallet);
createContractTransaction =
await createContractTransaction.signWithSigner(wallet);
const contractTransactionResponse =
await createContractTransaction.executeWithSigner(wallet);
// Fetch the receipt for the transaction that created the contract
const contractReceipt =
await contractTransactionResponse.getReceiptWithSigner(wallet);
// The contract ID is located on the transaction receipt
const contractId = contractReceipt.contractId;
console.log(`new contract ID: ${contractId.toString()}`);
// Call a method on a contract that exists on Hedera
// Note: `ContractCallQuery` cannot mutate a contract, it will only return the last state
// of the contract
const contractCallResult = await new ContractCallQuery()
// Set the gas to execute a contract call
.setGas(75000)
// Set which contract
.setContractId(contractId)
// Set the function to call on the contract
.setFunction("greet")
.setQueryPayment(new Hbar(1))
.executeWithSigner(wallet);
if (
contractCallResult.errorMessage != null &&
contractCallResult.errorMessage != ""
) {
console.log(
`error calling contract: ${contractCallResult.errorMessage}`,
);
}
// Get the message from the result
// The `0` is the index to fetch a particular type from
//
// e.g.
// If the return type of `get_message` was `(string[], uint32, string)`
// then you'd need to get each field separately using:
// const stringArray = contractCallResult.getStringArray(0);
// const uint32 = contractCallResult.getUint32(1);
// const string = contractCallResult.getString(2);
const message = contractCallResult.getString(0);
console.log(`contract message: ${message}`);
let contractDeleteTransaction = await new ContractDeleteTransaction()
.setContractId(contractId)
.setTransferAccountId(wallet.accountId)
.freezeWithSigner(wallet);
await contractDeleteTransaction.signWithSigner(wallet);
const contractDeleteResult =
await contractDeleteTransaction.executeWithSigner(wallet);
// Delete the contract
// Note: The admin key of the contract needs to sign the transaction
// In this case the client operator is the same as the admin key so the
// automatic signing takes care of this for you
await contractDeleteResult.getReceiptWithSigner(wallet);
console.log("contract successfully deleted");
} catch (error) {
console.error(error);
}
provider.close();
}
void main();