Skip to content

Latest commit

 

History

History
116 lines (85 loc) · 3.28 KB

delete-a-smart-contract.md

File metadata and controls

116 lines (85 loc) · 3.28 KB

Delete a smart contract

ContractDeleteTransaction() deletes a smart contract.

Constructor Description
ContractDeleteTransaction() ​Initializes the ContractDeleteTransaction object
new ContractDeleteTransaction()

Basic

Method Type Description
setContractId(<contractID>) ContractID The ID of the contract

Example

{% tabs %} {% tab title="Java" %}

ClassLoader cl = CreateSimpleContract.class.getClassLoader();

Gson gson = new Gson();

JsonObject jsonObject;

try (InputStream jsonStream = cl.getResourceAsStream("hello_world.json")) {
    if (jsonStream == null) {
        throw new RuntimeException("failed to get hello_world.json");
    }

    jsonObject = gson.fromJson(new InputStreamReader(jsonStream), JsonObject.class);
}

String byteCodeHex = jsonObject.getAsJsonPrimitive("object")
    .getAsString();

// `Client.forMainnet()` is provided for connecting to Hedera mainnet
Client client = Client.forTestnet();

// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);

// create the contract's bytecode file
TransactionId fileTxId = new FileCreateTransaction().setExpirationTime(
    Instant.now()
        .plus(Duration.ofSeconds(3600)))
    // Use the same key as the operator to "own" this file
    .addKey(OPERATOR_KEY.publicKey)
    .setContents(byteCodeHex.getBytes())
    .execute(client);

TransactionReceipt fileReceipt = fileTxId.getReceipt(client);
FileId newFileId = fileReceipt.getFileId();

System.out.println("contract bytecode file: " + newFileId);

// create the contract itself
TransactionId contractTxId = new ContractCreateTransaction().setAutoRenewPeriod(Duration.ofHours(1))
    .setGas(217000)
    .setBytecodeFileId(newFileId)
    // set an admin key so we can delete the contract later
    .setAdminKey(OPERATOR_KEY.publicKey)
    .execute(client);

TransactionReceipt contractReceipt = contractTxId.getReceipt(client);

System.out.println(contractReceipt.toProto());

ContractId newContractId = contractReceipt.getContractId();

System.out.println("new contract ID: " + newContractId);

ContractFunctionResult contractCallResult = new ContractCallQuery()
    .setGas(30000)
    .setContractId(newContractId)
    .setFunction("greet")
    .execute(client);

if (contractCallResult.errorMessage != null) {
    System.out.println("error calling contract: " + contractCallResult.errorMessage);
    return;
}

String message = contractCallResult.getString(0);
System.out.println("contract message: " + message);

// now delete the contract
TransactionId contractDeleteTxnId = new ContractDeleteTransaction()
    .setContractId(newContractId)
    .execute(client);

TransactionReceipt contractDeleteResult = contractDeleteTxnId.getReceipt(client);

if (contractDeleteResult.status != Status.Success) {
    System.out.println("error deleting contract: " + contractDeleteResult.status);
    return;
}
System.out.println("Contract successfully deleted");

{% endtab %}

{% tab title="JavaScript" %}

const deleteContractTxId = new ContractDeleteTransaction()
    .setContractId(newContractId)
    .execute(client);

{% endtab %} {% endtabs %}