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

feat(accountAllowanceApproveTransaction): Implement AccountAllowanceApproveTransaction E2E tests: TCK #314

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

rwalworth
Copy link
Contributor

@rwalworth rwalworth commented Dec 20, 2024

Description:
This PR implements the AccountAllowanceApproveTransaction tests documented in docs/test-specifications/crypto-service/AccountAllowanceApproveTransaction.md.

Related issue(s):
#313

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

@rwalworth rwalworth added the enhancement New feature or request label Dec 20, 2024
@rwalworth rwalworth self-assigned this Dec 20, 2024
@rwalworth rwalworth linked an issue Dec 20, 2024 that may be closed by this pull request

// TODO: Get mirror node interface with OpenAPI
async getNftAllowances(accountId: string): Promise<any> {
const url = `${this.mirrorNodeRestJavaUrl}/api/v1/accounts/${accountId}/allowances/nfts`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add the mirrorNodeRestJavaUrl value inside the .env examples.

Comment on lines +66 to +76
let foundAllowance = false;
for (let i = 0; i < mirrorNodeInfo.allowances.length; i++) {
if (
mirrorNodeInfo.allowances[i].owner === ownerAccountId &&
mirrorNodeInfo.allowances[i].spender === spenderAccountId &&
mirrorNodeInfo.allowances[i].amount.toString() === amount
) {
foundAllowance = true;
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let foundAllowance = false;
for (let i = 0; i < mirrorNodeInfo.allowances.length; i++) {
if (
mirrorNodeInfo.allowances[i].owner === ownerAccountId &&
mirrorNodeInfo.allowances[i].spender === spenderAccountId &&
mirrorNodeInfo.allowances[i].amount.toString() === amount
) {
foundAllowance = true;
break;
}
}
const foundAllowance = mirrorNodeInfo.allowances.some(
allowance =>
allowance.owner === ownerAccountId &&
allowance.spender === spenderAccountId &&
allowance.amount.toString() === amount
);

await JSONRPCRequest(this, "reset");
});

async function verifyHbarAllowance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please put this function into the helper directory?

Comment on lines +90 to +101
let foundAllowance = false;
for (let i = 0; i < mirrorNodeInfo.allowances.length; i++) {
if (
mirrorNodeInfo.allowances[i].owner === ownerAccountId &&
mirrorNodeInfo.allowances[i].spender === spenderAccountId &&
mirrorNodeInfo.allowances[i].token_id === tokenId &&
mirrorNodeInfo.allowances[i].amount.toString() === amount
) {
foundAllowance = true;
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let foundAllowance = false;
for (let i = 0; i < mirrorNodeInfo.allowances.length; i++) {
if (
mirrorNodeInfo.allowances[i].owner === ownerAccountId &&
mirrorNodeInfo.allowances[i].spender === spenderAccountId &&
mirrorNodeInfo.allowances[i].token_id === tokenId &&
mirrorNodeInfo.allowances[i].amount.toString() === amount
) {
foundAllowance = true;
break;
}
}
const foundAllowance = mirrorNodeInfo.allowances.some(
allowance =>
allowance.owner === ownerAccountId &&
allowance.spender === spenderAccountId &&
allowance.tokenId === tokenId &&
allowance.amount.toString() === amount
);

expect(foundAllowance).to.be.true;
}

async function verifyTokenAllowance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please put this function into the helper directory?

expect(foundAllowance).to.be.true;
}

async function verifyNftAllowance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please put this function into the helper directory?

expect(foundAllowance).to.equal(allowanceExists);
}

async function verifyApprovedForAllAllowance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Could you please put this function into the helper directory?
  2. Do the same as the previous examples in the loop.
  3. I think those functions could be arrow.

Comment on lines +1953 to +2014
await JSONRPCRequest(this, "approveAllowance", {
allowances: [
{
ownerAccountId,
spenderAccountId,
nft: {
tokenId,
serialNumbers: ["1", "2", "3"],
},
},
],
commonTransactionParams: {
signers: [ownerPrivateKey],
},
});

const serialNumbers = ["1", "2", "3"];
await JSONRPCRequest(this, "approveAllowance", {
allowances: [
{
ownerAccountId,
spenderAccountId: accountId,
nft: {
tokenId,
serialNumbers,
},
},
],
commonTransactionParams: {
signers: [ownerPrivateKey],
},
});

await retryOnError(async () => {
await verifyNftAllowance(
true,
ownerAccountId,
accountId,
tokenId,
serialNumbers[0],
);
});

await retryOnError(async () => {
await verifyNftAllowance(
true,
ownerAccountId,
accountId,
tokenId,
serialNumbers[1],
);
});

await retryOnError(async () => {
await verifyNftAllowance(
true,
ownerAccountId,
accountId,
tokenId,
serialNumbers[2],
);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also move some of those helper functions to the helper directory and reuse them throughout the file wherever the same logic appears.

Repeat this logic for also:
(#1) Approves an NFT allowance to a spender account from an owner account
(#11) Approves an NFT allowance to a delegate spender account from a spender account with approved for all privileges from an owner account

Suggested change
await JSONRPCRequest(this, "approveAllowance", {
allowances: [
{
ownerAccountId,
spenderAccountId,
nft: {
tokenId,
serialNumbers: ["1", "2", "3"],
},
},
],
commonTransactionParams: {
signers: [ownerPrivateKey],
},
});
const serialNumbers = ["1", "2", "3"];
await JSONRPCRequest(this, "approveAllowance", {
allowances: [
{
ownerAccountId,
spenderAccountId: accountId,
nft: {
tokenId,
serialNumbers,
},
},
],
commonTransactionParams: {
signers: [ownerPrivateKey],
},
});
await retryOnError(async () => {
await verifyNftAllowance(
true,
ownerAccountId,
accountId,
tokenId,
serialNumbers[0],
);
});
await retryOnError(async () => {
await verifyNftAllowance(
true,
ownerAccountId,
accountId,
tokenId,
serialNumbers[1],
);
});
await retryOnError(async () => {
await verifyNftAllowance(
true,
ownerAccountId,
accountId,
tokenId,
serialNumbers[2],
);
});
const allowances = [
// Owner account allowing spender account.
{
ownerAccountId,
spenderAccountId,
nft: { tokenId, serialNumbers: ["1", "2", "3"] },
},
// Owner account allowing another spender account.
{
ownerAccountId,
spenderAccountId: accountId,
nft: { tokenId, serialNumbers: ["1", "2", "3"] },
},
];
// Approve allowances in bulk
for (const allowance of allowances) {
await JSONRPCRequest(this, "approveAllowance", {
allowances: [allowance],
commonTransactionParams: {
signers: [ownerPrivateKey],
},
});
}
// Consolidate verification for serial numbers
const verifyAllowance = async (accountId, serialNumbers) => {
for (const serialNumber of serialNumbers) {
await retryOnError(async () => {
await verifyNftAllowance(true, ownerAccountId, accountId, tokenId, serialNumber);
});
}
};
// Verify NFT allowances for the new account
await verifyAllowance(accountId, ["1", "2", "3"]);

Comment on lines +232 to +238
await JSONRPCRequest(this, "deleteAccount", {
deleteAccountId: ownerAccountId,
transferAccountId: process.env.OPERATOR_ACCOUNT_ID as string,
commonTransactionParams: {
signers: [ownerPrivateKey],
},
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add this part of logic to a helper function and re-use it, currently its repeated 5 times.

Comment on lines +349 to +362
await JSONRPCRequest(this, "approveAllowance", {
allowances: [
{
ownerAccountId,
spenderAccountId,
hbar: {
amount,
},
},
],
commonTransactionParams: {
signers: [ownerPrivateKey],
},
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add this part of logic to a helper function and re-use it, currently its repeated 4 times.

@ivaylogarnev-limechain
Copy link
Contributor

ivaylogarnev-limechain commented Jan 16, 2025

Tests are passing in JS! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TCK: Implement tests for AccountAllowanceApproveTransaction
2 participants