Skip to content

Commit a4a6c60

Browse files
chore(TCK): Added account allowance transaction endpoint (#2774)
* 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]>
1 parent 259f745 commit a4a6c60

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

tck/methods/account.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
AccountUpdateTransaction,
66
AccountDeleteTransaction,
77
Timestamp,
8+
AccountAllowanceApproveTransaction,
9+
TokenId,
810
} from "@hashgraph/sdk";
911
import Long from "long";
1012

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

1416
import { getKeyFromString } from "../utils/key";
1517
import { DEFAULT_GRPC_DEADLINE } from "../utils/constants/config";
18+
import { handleNftAllowances } from "../utils/helpers/account";
1619

1720
import {
21+
AccountAllowanceApproveParams,
1822
CreateAccountParams,
1923
DeleteAccountParams,
2024
UpdateAccountParams,
@@ -208,3 +212,54 @@ export const deleteAccount = async ({
208212
status: receipt.status.toString(),
209213
};
210214
};
215+
216+
export const approveAllowance = async ({
217+
allowances,
218+
commonTransactionParams,
219+
}: AccountAllowanceApproveParams): Promise<AccountResponse> => {
220+
const transaction = new AccountAllowanceApproveTransaction();
221+
transaction.setGrpcDeadline(DEFAULT_GRPC_DEADLINE);
222+
223+
for (const allowance of allowances) {
224+
const { ownerAccountId, spenderAccountId, hbar, token, nft } =
225+
allowance;
226+
const owner = AccountId.fromString(ownerAccountId);
227+
const spender = AccountId.fromString(spenderAccountId);
228+
229+
if (hbar) {
230+
transaction.approveHbarAllowance(
231+
owner,
232+
spender,
233+
Hbar.fromTinybars(hbar.amount),
234+
);
235+
} else if (token) {
236+
transaction.approveTokenAllowance(
237+
TokenId.fromString(token.tokenId),
238+
owner,
239+
spender,
240+
Long.fromString(token.amount),
241+
);
242+
} else if (nft) {
243+
handleNftAllowances(transaction, nft, owner, spender);
244+
} else {
245+
throw new Error("No valid allowance type provided.");
246+
}
247+
}
248+
249+
transaction.freezeWith(sdk.getClient());
250+
251+
if (commonTransactionParams) {
252+
applyCommonTransactionParams(
253+
commonTransactionParams,
254+
transaction,
255+
sdk.getClient(),
256+
);
257+
}
258+
259+
const txResponse = await transaction.execute(sdk.getClient());
260+
const receipt = await txResponse.getReceipt(sdk.getClient());
261+
262+
return {
263+
status: receipt.status.toString(),
264+
};
265+
};

tck/params/account.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AllowanceParams } from "./allowance";
2+
13
export interface CreateAccountParams {
24
readonly key?: string;
35
readonly initialBalance?: string;
@@ -31,3 +33,8 @@ export interface DeleteAccountParams {
3133
readonly transferAccountId?: string;
3234
readonly commonTransactionParams?: Record<string, any>;
3335
}
36+
37+
export interface AccountAllowanceApproveParams {
38+
readonly allowances: AllowanceParams[];
39+
readonly commonTransactionParams?: Record<string, any>;
40+
}

tck/params/allowance.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export interface AllowanceParams {
2+
readonly ownerAccountId: string;
3+
readonly spenderAccountId: string;
4+
readonly hbar: HbarAllowanceParams;
5+
readonly token?: TokenAllowanceParams;
6+
readonly nft?: NftAllowanceParams;
7+
}
8+
9+
export interface HbarAllowanceParams {
10+
readonly amount: string;
11+
}
12+
13+
export interface TokenAllowanceParams {
14+
readonly tokenId: string;
15+
readonly amount: string;
16+
}
17+
18+
export interface NftAllowanceParams {
19+
readonly tokenId: string;
20+
readonly serialNumbers?: string[];
21+
readonly approvedForAll?: boolean;
22+
readonly delegateSpenderAccountId?: string;
23+
}

tck/utils/helpers/account.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
AccountId,
3+
AccountAllowanceApproveTransaction,
4+
TokenId,
5+
NftId,
6+
} from "@hashgraph/sdk";
7+
import Long from "long";
8+
9+
import { NftAllowanceParams } from "../../params/allowance";
10+
11+
export const handleNftAllowances = (
12+
transaction: AccountAllowanceApproveTransaction,
13+
nft: NftAllowanceParams,
14+
owner: AccountId,
15+
spender: AccountId,
16+
) => {
17+
const { tokenId, serialNumbers, delegateSpenderAccountId, approvedForAll } =
18+
nft;
19+
20+
if (delegateSpenderAccountId === "") {
21+
throw new Error("delegateSpenderAccountId cannot be an empty string!");
22+
}
23+
24+
if (serialNumbers) {
25+
for (const serialNumber of serialNumbers) {
26+
const nftId = new NftId(
27+
TokenId.fromString(tokenId),
28+
Long.fromString(serialNumber),
29+
);
30+
31+
if (delegateSpenderAccountId) {
32+
transaction.approveTokenNftAllowanceWithDelegatingSpender(
33+
nftId,
34+
owner,
35+
spender,
36+
AccountId.fromString(delegateSpenderAccountId),
37+
);
38+
} else {
39+
transaction.approveTokenNftAllowance(nftId, owner, spender);
40+
}
41+
}
42+
} else if (approvedForAll) {
43+
transaction.approveTokenNftAllowanceAllSerials(
44+
TokenId.fromString(tokenId),
45+
owner,
46+
spender,
47+
);
48+
} else {
49+
transaction.deleteTokenNftAllowanceAllSerials(
50+
TokenId.fromString(tokenId),
51+
owner,
52+
spender,
53+
);
54+
}
55+
};

0 commit comments

Comments
 (0)