#### Wallet system
Imagine we have to design a system to manage the wallet's balance of users:
1. Deposit funds into system
a. $0:
b. $100: transfer funds from DBS bank (any bank) to CIMB (any bank) Xfers bank account
3. Transfer funds to each other
4. See their balance changes in the system
5. Show transaction histories
Goals:
- Design the system: Database / endpoints
Database: SQL
Each Row will represent a transaction record
== User to Bank Ref No. Table
User | Bank | Reference No.
User1 | CIMB | ref001
== User bank deposit table
Bank | Reference No. | Amount | Timestamp
CIMB | ref001 | 100 | 2021-09-27
== Transfer Table
From | To | Amount (unit: dollar) | Timestamp
User1 | User2 | 100 | 2021-08-27
== User Balance Table
User | Balance
User1 | 100
def deposit(amt):
# insert deposit record to user bank deposit table
success = insert()
# update balance in user balance table
if success:
updateSuccess = update()
if not updateSuccess:
rollbackInsert()
1. sum up all the deposit for User1 as s1
2. sum up all the transfer from other users to user1 as s2
3. sum up all the transfer from User1 to other users as s3
4. balance = s1 + s2 - s3
If we want running balance of user1 at the end of 2021 Aug
Do the following but with filter of timestamp < 2021-09-01
1. sum up all the deposit for User1 as s1
2. sum up all the transfer from other users to user1 as s2
3. sum up all the transfer from User1 to other users as s3
4. balance = s1 + s2 - s3
API endpoints:
1. Deposit funds into system from bank
POST, /v1/deposit
input:
{
bank: "CIMB",
User: "User1", (identify it is to User1's account)
amount: 100.20,
}
output:
{
instructions: [
"Go to your bank website",
"Transfer money to account number {...}",
"with reference number {...}"
],
}
2. Transfer fund between user accounts
POST, /v2/transfer
input:
{
from: "User1",
to: "User2",
amout: 100.20,
}
output:
response code: 200 or 5xx
{
error: ""
}
3. check user balance