Skip to content

feat(sdk-token): Add token allowance management methods to SDK (#204) - #260

Open
joshuaolabodebello2020-cyber wants to merge 1 commit into
Kevin737866:mainfrom
joshuaolabodebello2020-cyber:feature/204-sdk-token-allowance
Open

feat(sdk-token): Add token allowance management methods to SDK (#204)#260
joshuaolabodebello2020-cyber wants to merge 1 commit into
Kevin737866:mainfrom
joshuaolabodebello2020-cyber:feature/204-sdk-token-allowance

Conversation

@joshuaolabodebello2020-cyber

Copy link
Copy Markdown
Contributor

Summary

Implements full SEP-41 compliant token allowance management in the TypeScript SDK, closing issue #204.

Changes

New Methods on TokenClient

Method Description
approve(owner, spender, amount, options?) Grant a spending allowance; amount='0' revokes. Supports expirationLedger option.
allowance(owner, spender) Query the current approved amount as a string.
transferFrom(spender, from, to, amount) Delegated transfer using an existing allowance. Pre-flight check throws INSUFFICIENT_ALLOWANCE before submitting a doomed transaction.
increaseAllowance(owner, spender, addedAmount) Atomic read-then-approve helper: current + addedAmount. Avoids the approve(0)/approve(new) race condition.
decreaseAllowance(owner, spender, subtractedAmount) Atomic read-then-approve helper: current - subtractedAmount. Throws INVALID_ALLOWANCE_AMOUNT if subtraction would go negative.
parseAllowanceEvent(rawEvent) Parse raw Stellar contract events for approve and transfer_from topics into typed objects.

Types (sdk/src/types.ts)

  • AllowanceInfo — owner, spender, amount
  • ApproveOptions — extends TransactionOptions with optional expirationLedger
  • AssetEvent — extended with transfer_from and approve event types
  • ErrorCode — new values: INSUFFICIENT_ALLOWANCE, ALLOWANCE_NOT_FOUND, INVALID_ALLOWANCE_AMOUNT

Bug Fixes

  • errors.ts: Fixed pre-existing syntax error on TX_BAD_SEQ computed key (missing ])
  • tokenClient.ts: Updated Server import for stellar-sdk v12 (rpc.Server)
  • tokenClient.ts: Added missing Keypair, scValToNative, InvalidParametersError imports

Test Infrastructure

  • jest.config.js: Added ts-jest configuration enabling TypeScript test compilation
  • sdk/src/__tests__/tokenAllowance.test.ts: 26 passing unit tests

Tests Passing

PASS sdk/src/__tests__/tokenAllowance.test.ts
  TokenClient.approve (6 tests)
  TokenClient.allowance (4 tests)
  TokenClient.transferFrom (5 tests)
  TokenClient.increaseAllowance (3 tests)
  TokenClient.decreaseAllowance (3 tests)
  TokenClient.parseAllowanceEvent (5 tests)

Tests: 26 passed, 26 total

Acceptance Criteria

  • approve(spender, amount) / allowance(owner, spender)
  • transferFrom(spender, from, to, amount)
  • increaseAllowance / decreaseAllowance convenience methods
  • Allowance event parsing (parseAllowanceEvent)
  • Tests: approve, transferFrom, allowance query, insufficient allowance

Closes #204

…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
@drips-wave

drips-wave Bot commented Aug 27, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@Kevin737866

Copy link
Copy Markdown
Owner

@joshuaolabodebello2020-cyber conflict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(sdk-token): Add token allowance management methods to SDK

2 participants