solution/sudhanshukroy#97
Open
SkroyCodes wants to merge 4 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds initial planning documentation for the wallet transfer assignment, outlining the intended transactional flow (success/failure), proposed schema direction, idempotency/concurrency considerations, a testing plan, and an AI usage disclosure.
Changes:
- Added an initial end-to-end transfer workflow write-up (including failure handling) and proposed DB tables/indexes.
- Documented idempotency and concurrency concerns to guide the upcoming implementation.
- Added an AI usage note describing the tool and general usage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
APPROACH.md |
New design/approach document covering transfer flows, schema direction, idempotency, concurrency, and testing. |
AI_USAGE.md |
New AI disclosure note describing the tool used and how it contributed to planning. |
|
|
||
| ## Problem: | ||
|
|
||
| We have to ensure wallet to wallet transfering of funds without repetition, handling and reporting failure and with balance correctness. |
|
|
||
| ## Approach: | ||
|
|
||
| ### The Point of concerns: |
Comment on lines
+22
to
+32
| 1. Validate incoming API Request | ||
| 2. Check an idempotency record using provided idempotencyKey. | ||
| 3. Start a database transaction. | ||
| 4. Create a transfer Record with Pending Status | ||
| 5. Lock or safely update the wallet rows to prevent concurrent attempts of debit. | ||
| 6. Validate the amount of debit availability in wallet. | ||
| 7. Debit from source wallet and Credit to Destination wallet. | ||
| 8. insert debit and credit ledger entries. | ||
| 9. Mark transfer Record as Processed status. | ||
| 10. Store final idempotency result. | ||
| 11. Commit Transaction |
Comment on lines
+38
to
+47
| 3. Check an idempotency record using provided idempotencyKey. | ||
| 4. Start a database transaction. | ||
| 5. Create a transfer record with status PENDING. | ||
| 6. Lock the wallet rows to prevent concurrent attempts of debit. | ||
| 7. Validate wallet existence and source wallet balance inside the transaction. | ||
| 8. If the transfer cannot be completed, mark the transfer as FAILED. | ||
| 9. Do not debit/credit wallet balances. | ||
| 10. Do not create successful debit/credit ledger entries. | ||
| 11. Business Logic error: Commit Transaction for FAILED records (low balance, wallet not found....). | ||
| 12. For Technical errors: rollback the transaction so partial wallet updates or ledger entries are not persisted. |
Comment on lines
+60
to
+62
| 1. wallet_id as primary key | ||
| 2. wallet.balance always non-negative. | ||
|
|
Comment on lines
+1
to
+11
| # AI Usage Note | ||
|
|
||
| ## Tool Used | ||
|
|
||
| ChatGPT | ||
|
|
||
| ## How I Used AI | ||
|
|
||
| I used ChatGPT as a reviewing assistant to structure my initial approach around idempotency, transaction boundaries, database design, and testing scenarios. | ||
|
|
||
| I did not use AI to blindly generate the full solution. I used it to reason about edge cases and organize my implementation approach. No newline at end of file |
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
Implemented the core wallet-to-wallet transfer workflow.
The solution focuses on:
AI disclosure
Tool used: ChatGPT
How I used it:
I used ChatGPT as a planning, review, and implementation-support assistant.
I worked design-first. I first prepared the initial approach and decided the main direction of the solution myself: wallet-to-wallet transfer flow, idempotency handling, transaction boundaries, ledger entries, duplicate request handling, and failed transfer handling.
I used AI to improve clarity, validate edge cases, review clean service flow, identify error scenarios, generate/refine test cases based on my approach document, and improve documentation wording.
I did not use AI to blindly generate the full solution. I reviewed, corrected, implemented, ran, and verified the final solution locally.
Representative prompts and AI usage details are documented in
AI_USAGE.md.Schema Design
Introduced the following main entities/tables:
walletStores wallet balance.
Important fields:
wallet_idbalancetransferStores each transfer attempt and its lifecycle.
Important fields:
transfer_idsource_wallet_iddestination_wallet_idamountstatusSupported statuses:
PENDINGPROCESSEDFAILEDIndexes are added on source and destination wallet references.
ledger_entryStores accounting entries for successful transfers.
Important fields:
entry_idtransfer_idwallet_idtransaction_typeamountSupported transaction types:
DEBITCREDITFor every successful transfer, two ledger entries are created: one debit entry for the source wallet and one credit entry for the destination wallet.
idempotency_recordStores retry-safe request processing state.
Important fields:
idempotency_record_ididempotency_keystatustransfer_idresult_statusresult_messageThe
idempotency_keyis unique, so the same request key cannot trigger the transfer logic more than once.Idempotency Strategy
The service uses a separate
idempotency_recordtable.Flow:
idempotencyKey.This prevents duplicate debit/credit operations during retries or duplicate delivery.
Concurrency Strategy
The implementation uses pessimistic row locking for involved wallets.
Both source and destination wallets are locked before balance validation and balance updates. Wallets are locked in wallet ID order to reduce deadlock risk.
This prevents race conditions where concurrent transfers from the same wallet could otherwise overdraw the source balance.
How to Run
Using Maven wrapper on Windows:
.\mvnw.cmd spring-boot:runUsing Maven wrapper on Linux/Mac:
If Maven is installed globally:
How to Test
Using Maven wrapper on Windows:
Using Maven wrapper on Linux/Mac:
./mvnw testIf Maven is installed globally:
mvn testTest coverage includes:
Tradeoffs / Assumptions
Checklist