feat(sdk-token): Add token allowance management methods to SDK (#204) - #260
Open
joshuaolabodebello2020-cyber wants to merge 1 commit into
Conversation
…737866#204) Implements SEP-41 compliant allowance management in the TypeScript SDK: - approve(owner, spender, amount, options?): Grant a spending allowance with optional expiration_ledger support. Allows amount='0' for revocation. - allowance(owner, spender): Query current approved amount - transferFrom(spender, from, to, amount): Delegated transfer with pre-flight allowance check; throws INSUFFICIENT_ALLOWANCE if too low - increaseAllowance(owner, spender, addedAmount): Convenience helper that reads current allowance and calls approve(current + addedAmount) - decreaseAllowance(owner, spender, subtractedAmount): Convenience helper that reads current allowance and calls approve(current - subtractedAmount); throws INVALID_ALLOWANCE_AMOUNT if subtraction would go negative - parseAllowanceEvent(rawEvent): Parse raw contract events for 'approve' and 'transfer_from' topics into structured objects Types added (sdk/src/types.ts): - AllowanceInfo interface - ApproveOptions interface (extends TransactionOptions with expirationLedger) - Updated AssetEvent type to include 'transfer_from' and 'approve' event types - New ErrorCode values: INSUFFICIENT_ALLOWANCE, ALLOWANCE_NOT_FOUND, INVALID_ALLOWANCE_AMOUNT Bug fixes: - errors.ts: Fixed TX_BAD_SEQ syntax error (missing ']' in computed key) - tokenClient.ts: Fixed Server import for stellar-sdk v12 (rpc.Server) - tokenClient.ts: Added missing Keypair, scValToNative, InvalidParametersError imports Testing: - jest.config.js: Added ts-jest configuration for TypeScript test support - sdk/src/__tests__/tokenAllowance.test.ts: 26 passing tests covering approve, allowance query, transferFrom, increaseAllowance, decreaseAllowance, parseAllowanceEvent, INSUFFICIENT_ALLOWANCE error path Closes Kevin737866#204
|
@joshuaolabodebello2020-cyber Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Owner
|
@joshuaolabodebello2020-cyber conflict |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements full SEP-41 compliant token allowance management in the TypeScript SDK, closing issue #204.
Changes
New Methods on
TokenClientapprove(owner, spender, amount, options?)amount='0'revokes. SupportsexpirationLedgeroption.allowance(owner, spender)transferFrom(spender, from, to, amount)INSUFFICIENT_ALLOWANCEbefore submitting a doomed transaction.increaseAllowance(owner, spender, addedAmount)current + addedAmount. Avoids the approve(0)/approve(new) race condition.decreaseAllowance(owner, spender, subtractedAmount)current - subtractedAmount. ThrowsINVALID_ALLOWANCE_AMOUNTif subtraction would go negative.parseAllowanceEvent(rawEvent)approveandtransfer_fromtopics into typed objects.Types (
sdk/src/types.ts)AllowanceInfo— owner, spender, amountApproveOptions— extendsTransactionOptionswith optionalexpirationLedgerAssetEvent— extended withtransfer_fromandapproveevent typesErrorCode— new values:INSUFFICIENT_ALLOWANCE,ALLOWANCE_NOT_FOUND,INVALID_ALLOWANCE_AMOUNTBug Fixes
errors.ts: Fixed pre-existing syntax error onTX_BAD_SEQcomputed key (missing])tokenClient.ts: UpdatedServerimport for stellar-sdk v12 (rpc.Server)tokenClient.ts: Added missingKeypair,scValToNative,InvalidParametersErrorimportsTest Infrastructure
jest.config.js: Added ts-jest configuration enabling TypeScript test compilationsdk/src/__tests__/tokenAllowance.test.ts: 26 passing unit testsTests Passing
Acceptance Criteria
approve(spender, amount)/allowance(owner, spender)transferFrom(spender, from, to, amount)increaseAllowance/decreaseAllowanceconvenience methodsparseAllowanceEvent)Closes #204