-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
259f745
commit a4a6c60
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
}; |