Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContractId not usable for allowance approval via AccountAllowanceApproveTransaction #1557

Closed
Burstall opened this issue Apr 12, 2023 · 1 comment · Fixed by #1575
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Burstall
Copy link

Description

Using an AccountAllowanceApproveTransaction() and passing in a ContractId object throws an error.

Forcing conversion via solidity Address -> AccountId.fromSolidityAddress(_contractId.toSolidityAddress())

(or fromString()/toString())

In the past, I never had to do this, but in recent SDK versions, I have to make this change; unsure which version it changed.

Possibly it was intentional

Steps to reproduce

  1. deploy a contract, _contractId returned as a ContractId object
  2. create a AccountAllowanceApproveTransaction() to grant an approval (NFT or FT) to _contractId
  3. fails on execution
  4. retry with hard translation to AccountId (e.g. AccountId.fromSolidityAddress(_contractId.toSolidityAddress()) )

Additional context

No response

Hedera network

mainnet, testnet

Version

@hashgraph/sdk@2.22.0

Operating system

None

@Burstall Burstall added the bug Something isn't working label Apr 12, 2023
@ochikov ochikov self-assigned this Apr 13, 2023
@ochikov
Copy link
Contributor

ochikov commented Apr 13, 2023

Hello @Burstall and than you for your issue,

I've created this example based on your explanations:

  1. I am creating a fungible token
  2. I am creating a file to store the bytecode
  3. I am deploying a contract
  4. Create AccountAllowanceApproveTransaction

I am getting the error INVALID_ALLOWANCE_SPENDER_ID
When I cast it to the solidity address and then again back to the account Id (as you described), the transaction is successful.

I am going to dig it.

  //Grab your Hedera testnet account ID and private key from your .env file
  const myAccountId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
  const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);

  // If we weren't able to grab it, we should throw a new error
  if (myAccountId == null || myPrivateKey == null) {
    throw new Error(
      "Environment variables myAccountId and myPrivateKey must be present"
    );
  }

  // Create Hedera Testnet Client

  //The client has a default max transaction fee of 100,000,000 tinybars (1 hbar) and default max query payment of 100,000,000 tinybars (1 hbar).
  // If you need to change these values, you can use.setMaxDefaultTransactionFee() for a transaction and .setDefaultMaxQueryPayment() for queries.
  // So the max transaction fee is 1 hbar and the max query fee is 1 hbar, but those values can be changed
  const client = Client.forTestnet();

  // The operator is the account that will pay for the transaction query fees in HBAR
  client.setOperator(myAccountId, myPrivateKey);

  //Create Fungible Token
  const tokenCreateTransaction = await new TokenCreateTransaction()
    .setTokenName("Black Sea LimeChain Token")
    .setTokenSymbol("BSL")
    .setTreasuryAccountId(myAccountId)
    .setInitialSupply(10000) // Total supply = 10000 / 10 ^ 2
    .setDecimals(2)
    .setAutoRenewAccountId(myAccountId)
    .freezeWith(client);

  const txResponse = await tokenCreateTransaction.execute(client);
  const tokenCreateReceipt = await txResponse.getReceipt(client);
  console.log("TokenID: ", tokenCreateReceipt.tokenId.toString());
  console.log(
    "Solidity Token Address",
    tokenCreateReceipt.tokenId.toSolidityAddress()
  );
  const tokenId = tokenCreateReceipt.tokenId;

  // Create a file on Hedera and store the bytecode
  const fileCreateTx = new FileCreateTransaction()
    .setKeys([myPrivateKey])
    .freezeWith(client);
  const fileCreateSign = await fileCreateTx.sign(myPrivateKey);
  const fileCreateSubmit = await fileCreateSign.execute(client);
  const fileCreateRx = await fileCreateSubmit.getReceipt(client);
  const bytecodeFileId = fileCreateRx.fileId;
  console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`);

  //Append contents to the file
  const fileAppendTx = new FileAppendTransaction()
    .setFileId(bytecodeFileId)
    .setContents(contract.bytecode)
    .setMaxChunks(10)
    .freezeWith(client);
  const fileAppendSign = await fileAppendTx.sign(myPrivateKey);
  const fileAppendSubmit = await fileAppendSign.execute(client);
  const fileAppendRx = await fileAppendSubmit.getReceipt(client);
  console.log("Status of file append is", fileAppendRx.status.toString());

  // Instantiate the contract instance
  const contractTx = await new ContractCreateTransaction()
    //Set the file ID of the Hedera file storing the bytecode
    .setAdminKey(myPrivateKey)
    .setBytecodeFileId(bytecodeFileId)
    //Set the gas to instantiate the contract
    .setGas(100000)
    //Provide the constructor parameters for the contract
    .setConstructorParameters();

  //Submit the transaction to the Hedera test network
  const contractResponse = await contractTx.execute(client);

  //Get the receipt of the file create transaction
  const contractReceipt = await contractResponse.getReceipt(client);

  //Get the smart contract ID
  const newContractId = contractReceipt.contractId;

  //Log the smart contract ID
  console.log("The smart contract ID is " + newContractId);

  // Give `spender` allowance for fungible token
  const receiverApproveTx =
    new AccountAllowanceApproveTransaction().approveTokenAllowance(
      tokenId,
      myAccountId,
      newContractId
    );

  const approveRx = await receiverApproveTx.execute(client);

  const approveReceipt = await approveRx.getReceipt(client);
  console.log(
    // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
    `Approve spender allowance - status: ${approveReceipt.status}`
  );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants