github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/precompile_example/main.go (about) 1 package main 2 3 import ( 4 _ "embed" 5 "encoding/hex" 6 "encoding/json" 7 "fmt" 8 "os" 9 10 "github.com/hashgraph/hedera-sdk-go/v2" 11 "github.com/hashgraph/hedera-sdk-go/v2/examples/contract_helper" 12 ) 13 14 //go:embed PrecompileExample.json 15 var precompileExample []byte 16 17 type AbiObject struct { 18 ByteCode string `json:"bytecode"` 19 } 20 21 func additionalLogic(privateKey hedera.PrivateKey, keyList *hedera.KeyList, address string, client *hedera.Client) { 22 id, err := hedera.TokenIDFromSolidityAddress(address) 23 if err != nil { 24 panic(err) 25 } 26 asd, err := hedera.NewTokenUpdateTransaction().SetTokenID(id).SetAdminKey(keyList).SetSupplyKey(keyList).Sign(privateKey).Execute(client) 27 if err != nil { 28 panic(err) 29 } 30 rec, err := asd.GetReceipt(client) 31 if err != nil { 32 panic(err) 33 } 34 fmt.Printf("asd: %v\n", rec) 35 } 36 func main() { 37 var client *hedera.Client 38 var err error 39 40 // Retrieving network type from environment variable HEDERA_NETWORK 41 client, err = hedera.ClientForName(os.Getenv("HEDERA_NETWORK")) 42 if err != nil { 43 panic(fmt.Sprintf("%v : error creating client", err)) 44 } 45 46 // Retrieving operator ID from environment variable OPERATOR_ID 47 operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID")) 48 if err != nil { 49 panic(fmt.Sprintf("%v : error converting string to AccountID", err)) 50 } 51 52 // Retrieving operator key from environment variable OPERATOR_KEY 53 operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY")) 54 if err != nil { 55 panic(fmt.Sprintf("%v : error converting string to PrivateKey", err)) 56 } 57 58 // Setting the client operator ID and key 59 client.SetOperator(operatorAccountID, operatorKey) 60 61 alicePrivateKey, err := hedera.PrivateKeyGenerateEd25519() 62 if err != nil { 63 panic(fmt.Sprintf("%v : error generating Alice's private key", err)) 64 } 65 alicePublicKey := alicePrivateKey.PublicKey() 66 accountCreateResponse, err := hedera.NewAccountCreateTransaction(). 67 SetKey(alicePublicKey). 68 SetInitialBalance(hedera.NewHbar(1)). 69 Execute(client) 70 if err != nil { 71 panic(fmt.Sprintf("%v : error creating Alice's account", err)) 72 } 73 74 accountCreateReceipt, err := accountCreateResponse.GetReceipt(client) 75 if err != nil { 76 panic(fmt.Sprintf("%v : error retrieving account create receipt", err)) 77 } 78 79 var aliceAccountID hedera.AccountID 80 if accountCreateReceipt.AccountID != nil { 81 aliceAccountID = *accountCreateReceipt.AccountID 82 } else { 83 panic("Alice's account id from receipt is nil") 84 } 85 86 var abiObject AbiObject 87 err = json.Unmarshal(precompileExample, &abiObject) 88 if err != nil { 89 panic("error reading from json") 90 } 91 92 contractFunctionParameters, err := hedera.NewContractFunctionParameters(). 93 AddAddress(client.GetOperatorAccountID().ToSolidityAddress()) 94 if err != nil { 95 panic(fmt.Sprintf("%v : error making contract function parameters", err)) 96 } 97 98 contractFunctionParameters, err = contractFunctionParameters. 99 AddAddress(aliceAccountID.ToSolidityAddress()) 100 if err != nil { 101 panic(fmt.Sprintf("%v : error adding alice's address to contract function parameters", err)) 102 } 103 104 contractHelper := contract_helper.NewContractHelper([]byte(abiObject.ByteCode), *contractFunctionParameters, client) 105 106 keyList := hedera.KeyListWithThreshold(1).Add(operatorKey.PublicKey()).Add(contractHelper.ContractID) 107 108 tx, err := hedera.NewAccountUpdateTransaction().SetAccountID(operatorAccountID).SetKey(keyList).Execute(client) 109 if err != nil { 110 panic(fmt.Sprintf("%v : error updating alice's account", err)) 111 } 112 _, err = tx.GetReceipt(client) 113 if err != nil { 114 panic(err) 115 } 116 keyList = hedera.KeyListWithThreshold(1).Add(alicePublicKey).Add(contractHelper.ContractID) 117 118 frozenTxn, err := hedera.NewAccountUpdateTransaction().SetAccountID(aliceAccountID).SetKey(keyList).FreezeWith(client) 119 if err != nil { 120 panic(fmt.Sprintf("%v : error updating alice's account", err)) 121 } 122 tx, err = frozenTxn.Sign(alicePrivateKey).Execute(client) 123 if err != nil { 124 panic(fmt.Sprintf("%v : error updating alice's account", err)) 125 } 126 _, err = tx.GetReceipt(client) 127 if err != nil { 128 panic(err) 129 } 130 131 tokenUpdateFunction := func(address string) { 132 id, err := hedera.TokenIDFromSolidityAddress(address) 133 if err != nil { 134 panic(err) 135 } 136 frozenTxn, err := hedera.NewTokenUpdateTransaction().SetTokenID(id).SetAdminKey(keyList).SetSupplyKey(keyList).FreezeWith(client) 137 if err != nil { 138 panic(err) 139 } 140 tx, err := frozenTxn.Sign(alicePrivateKey).Execute(client) 141 if err != nil { 142 panic(err) 143 } 144 _, err = tx.GetReceipt(client) 145 if err != nil { 146 panic(err) 147 } 148 } 149 150 contractHelper. 151 SetResultValidatorForStep(0, func(contractFunctionResult hedera.ContractFunctionResult) bool { 152 println("getPseudoRandomSeed() returned " + hex.EncodeToString(contractFunctionResult.GetBytes32(0))) 153 return true 154 }). 155 SetPayableAmountForStep(1, hedera.NewHbar(20)). 156 // step 3 associates Alice with the token, which requires Alice's signature 157 AddSignerForStep(3, alicePrivateKey). 158 AddSignerForStep(5, alicePrivateKey). 159 SetParameterSupplierForStep(11, func() *hedera.ContractFunctionParameters { 160 return hedera.NewContractFunctionParameters(). 161 // when contracts work with a public key, they handle the raw bytes of the public key 162 AddBytes(alicePublicKey.BytesRaw()) 163 }). 164 SetPayableAmountForStep(11, hedera.NewHbar(40)). 165 // Because we're setting the adminKey for the created NFT token to Alice's key, 166 // Alice must sign the ContractExecuteTransaction. 167 AddSignerForStep(11, alicePrivateKey). 168 SetStepLogic(11, tokenUpdateFunction). 169 // and Alice must sign for minting because her key is the supply key. 170 AddSignerForStep(12, alicePrivateKey). 171 SetParameterSupplierForStep(12, func() *hedera.ContractFunctionParameters { 172 return hedera.NewContractFunctionParameters(). 173 // add three metadatas 174 AddBytesArray([][]byte{{0x01b}, {0x02b}, {0x03b}}) 175 }). // and alice must sign to become associated with the token. 176 AddSignerForStep(13, alicePrivateKey). 177 // Alice must sign to burn the token because her key is the supply key 178 AddSignerForStep(16, alicePrivateKey) 179 180 // step 0 tests pseudo random number generator (PRNG) 181 // step 1 creates a fungible token 182 // step 2 mints it 183 // step 3 associates Alice with it 184 // step 4 transfers it to Alice. 185 // step 5 approves an allowance of the fungible token with operator as the owner and Alice as the spender 186 // steps 6 - 10 test misc functions on the fungible token (see PrecompileExample.sol for details). 187 // step 11 creates an NFT token with a custom fee, and with the admin and supply set to Alice's key 188 // step 12 mints some NFTs 189 // step 13 associates Alice with the NFT token 190 // step 14 transfers some NFTs to Alice 191 // step 15 approves an NFT allowance with operator as the owner and Alice as the spender 192 // step 16 burn some NFTs 193 194 // TODO there is currently possible bug in services causing this operation to fail, should be investigated 195 // _, err = contractHelper. 196 // ExecuteSteps( /* from step */ 0 /* to step */, 16, client) 197 // if err != nil { 198 // panic(fmt.Sprintf("%v : error executing steps", err)) 199 // } 200 }