Skip to content

Commit

Permalink
chore(TCK): Added account allowance transaction endpoint (#2774)
Browse files Browse the repository at this point in the history
* feat: Draft account allowance transaction added

Signed-off-by: ivaylogarnev-limechain <[email protected]>

* fix: Some fixing in the approveAllowence method

Signed-off-by: ivaylogarnev-limechain <[email protected]>

* fix: Fixing conditions inside approveAllowance method

Signed-off-by: ivaylogarnev-limechain <[email protected]>

* fix: Added delegateSpenderAccountId check

Signed-off-by: ivaylogarnev-limechain <[email protected]>

* refactor: Refactored the accountAlowance method and put some of the logic in helper function

Signed-off-by: ivaylogarnev-limechain <[email protected]>

* fix: Multiple allowances logic adjusted

Signed-off-by: ivaylogarnev-limechain <[email protected]>

* fix: Removed check for no allowances

Signed-off-by: ivaylogarnev-limechain <[email protected]>

---------

Signed-off-by: ivaylogarnev-limechain <[email protected]>
Co-authored-by: Ivan Ivanov <[email protected]>
  • Loading branch information
ivaylogarnev-limechain and 0xivanov authored Feb 11, 2025
1 parent 259f745 commit a4a6c60
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tck/methods/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
AccountUpdateTransaction,
AccountDeleteTransaction,
Timestamp,
AccountAllowanceApproveTransaction,
TokenId,
} from "@hashgraph/sdk";
import Long from "long";

Expand All @@ -13,8 +15,10 @@ import { AccountResponse } from "../response/account";

import { getKeyFromString } from "../utils/key";
import { DEFAULT_GRPC_DEADLINE } from "../utils/constants/config";
import { handleNftAllowances } from "../utils/helpers/account";

import {
AccountAllowanceApproveParams,
CreateAccountParams,
DeleteAccountParams,
UpdateAccountParams,
Expand Down Expand Up @@ -208,3 +212,54 @@ export const deleteAccount = async ({
status: receipt.status.toString(),
};
};

export const approveAllowance = async ({
allowances,
commonTransactionParams,
}: AccountAllowanceApproveParams): Promise<AccountResponse> => {
const transaction = new AccountAllowanceApproveTransaction();
transaction.setGrpcDeadline(DEFAULT_GRPC_DEADLINE);

for (const allowance of allowances) {
const { ownerAccountId, spenderAccountId, hbar, token, nft } =
allowance;
const owner = AccountId.fromString(ownerAccountId);
const spender = AccountId.fromString(spenderAccountId);

if (hbar) {
transaction.approveHbarAllowance(
owner,
spender,
Hbar.fromTinybars(hbar.amount),
);
} else if (token) {
transaction.approveTokenAllowance(
TokenId.fromString(token.tokenId),
owner,
spender,
Long.fromString(token.amount),
);
} else if (nft) {
handleNftAllowances(transaction, nft, owner, spender);
} else {
throw new Error("No valid allowance type provided.");
}
}

transaction.freezeWith(sdk.getClient());

if (commonTransactionParams) {
applyCommonTransactionParams(
commonTransactionParams,
transaction,
sdk.getClient(),
);
}

const txResponse = await transaction.execute(sdk.getClient());
const receipt = await txResponse.getReceipt(sdk.getClient());

return {
status: receipt.status.toString(),
};
};
7 changes: 7 additions & 0 deletions tck/params/account.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AllowanceParams } from "./allowance";

export interface CreateAccountParams {
readonly key?: string;
readonly initialBalance?: string;
Expand Down Expand Up @@ -31,3 +33,8 @@ export interface DeleteAccountParams {
readonly transferAccountId?: string;
readonly commonTransactionParams?: Record<string, any>;
}

export interface AccountAllowanceApproveParams {
readonly allowances: AllowanceParams[];
readonly commonTransactionParams?: Record<string, any>;
}
23 changes: 23 additions & 0 deletions tck/params/allowance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface AllowanceParams {
readonly ownerAccountId: string;
readonly spenderAccountId: string;
readonly hbar: HbarAllowanceParams;
readonly token?: TokenAllowanceParams;
readonly nft?: NftAllowanceParams;
}

export interface HbarAllowanceParams {
readonly amount: string;
}

export interface TokenAllowanceParams {
readonly tokenId: string;
readonly amount: string;
}

export interface NftAllowanceParams {
readonly tokenId: string;
readonly serialNumbers?: string[];
readonly approvedForAll?: boolean;
readonly delegateSpenderAccountId?: string;
}
55 changes: 55 additions & 0 deletions tck/utils/helpers/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
AccountId,
AccountAllowanceApproveTransaction,
TokenId,
NftId,
} from "@hashgraph/sdk";
import Long from "long";

import { NftAllowanceParams } from "../../params/allowance";

export const handleNftAllowances = (
transaction: AccountAllowanceApproveTransaction,
nft: NftAllowanceParams,
owner: AccountId,
spender: AccountId,
) => {
const { tokenId, serialNumbers, delegateSpenderAccountId, approvedForAll } =
nft;

if (delegateSpenderAccountId === "") {
throw new Error("delegateSpenderAccountId cannot be an empty string!");
}

if (serialNumbers) {
for (const serialNumber of serialNumbers) {
const nftId = new NftId(
TokenId.fromString(tokenId),
Long.fromString(serialNumber),
);

if (delegateSpenderAccountId) {
transaction.approveTokenNftAllowanceWithDelegatingSpender(
nftId,
owner,
spender,
AccountId.fromString(delegateSpenderAccountId),
);
} else {
transaction.approveTokenNftAllowance(nftId, owner, spender);
}
}
} else if (approvedForAll) {
transaction.approveTokenNftAllowanceAllSerials(
TokenId.fromString(tokenId),
owner,
spender,
);
} else {
transaction.deleteTokenNftAllowanceAllSerials(
TokenId.fromString(tokenId),
owner,
spender,
);
}
};

0 comments on commit a4a6c60

Please sign in to comment.