forked from hashgraph/hedera-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-simple-contract.js
120 lines (99 loc) · 4.04 KB
/
create-simple-contract.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
import {
Client,
PrivateKey,
ContractCreateTransaction,
FileCreateTransaction,
ContractDeleteTransaction,
ContractCallQuery,
Hbar,
AccountId,
} from "@hashgraph/sdk";
import * as dotenv from "dotenv";
dotenv.config();
// Import the compiled contract
import helloWorld from "./hello_world.json";
async function main() {
let client;
try {
client = Client.forName(process.env.HEDERA_NETWORK).setOperator(
AccountId.fromString(process.env.OPERATOR_ID),
PrivateKey.fromString(process.env.OPERATOR_KEY)
);
} catch (error) {
throw new Error(
"Environment variables HEDERA_NETWORK, OPERATOR_ID, and OPERATOR_KEY are required."
);
}
// The contract bytecode is located on the `object` field
const contractByteCode = helloWorld.object;
// 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
const fileTransactionResponse = await new FileCreateTransaction()
.setKeys([client.operatorPublicKey])
.setContents(contractByteCode)
.execute(client);
// Fetch the receipt for transaction that created the file
const fileReceipt = await fileTransactionResponse.getReceipt(client);
// The file ID is located on the transaction receipt
const fileId = fileReceipt.fileId;
console.log(`contract bytecode file: ${fileId.toString()}`);
// Create the contract
const contractTransactionResponse = await new ContractCreateTransaction()
// Set gas to create the contract
.setGas(75000)
// 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(client.operatorPublicKey)
.execute(client);
// Fetch the receipt for the transaction that created the contract
const contractReceipt = await contractTransactionResponse.getReceipt(
client
);
// The conract 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))
.execute(client);
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}`);
const contractDeleteResult = await new ContractDeleteTransaction()
.setContractId(contractId)
.execute(client);
// 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.getReceipt(client);
console.log("contract successfully deleted");
}
void main();