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

chore(TCK): Added account allowance transaction endpoint #2774

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
}
};