Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: WM on Test wallet #1427

Closed
wants to merge 66 commits into from
Closed
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
01d9084
First iteration - still WIP
sanducb Jun 27, 2024
ee0bb17
Format
sanducb Jun 27, 2024
da749f5
Add Rapyd threshold as env var
sanducb Jun 27, 2024
261ce75
Frontend fixes for WM PPs
Tymmmy Jun 27, 2024
a06af49
Update [accountId].tsx
Tymmmy Jun 27, 2024
2078190
Update [accountId].tsx
Tymmmy Jun 27, 2024
c14b686
fix: conflicts of build has been resolved (#1428)
rico191013 Jun 27, 2024
fb4986d
Merge branch 'main' into 1358-be-asset-scale-2-to-asset-scale-9
Tymmmy Jun 28, 2024
bb35ecf
Remove unused function
sanducb Jul 9, 2024
eea0694
Merge branch 'main' of https://github.com/interledger/testnet into 13…
sanducb Jul 10, 2024
c01d1a6
Remove unused import
sanducb Jul 10, 2024
b3429ac
Fix wallet backend tests
sanducb Jul 11, 2024
07a1ea3
Run prettier
sanducb Jul 11, 2024
9eeeec9
Fix frontend formatting of asset scale
sanducb Jul 16, 2024
0cbdf04
Merge branch 'main' into 1358-be-asset-scale-2-to-asset-scale-9
sanducb Jul 16, 2024
22c77a5
Run prettier
sanducb Jul 16, 2024
ac3be50
Merge branch '1358-be-asset-scale-2-to-asset-scale-9' of https://gith…
sanducb Jul 16, 2024
4876690
Run prettier
sanducb Jul 16, 2024
4caaaa4
Final fix for displaying in diffrerent asset scales
sanducb Jul 16, 2024
83ed0c5
Remove unused import
sanducb Jul 16, 2024
b2b14ce
Run prettier
sanducb Jul 16, 2024
2626bfc
Add balance to useMemo deps
sanducb Jul 16, 2024
e52bd16
Fix display send and request amounts
sanducb Jul 17, 2024
6c5d9a3
Fix quote values
sanducb Aug 1, 2024
4c0de74
Remove comment
sanducb Aug 1, 2024
1768d9e
Prettier
sanducb Aug 1, 2024
3aa2ce8
Add simple migration - WIP
sanducb Aug 1, 2024
70fb990
Prettier
sanducb Aug 1, 2024
e5f6cc8
Fix display of fee
sanducb Aug 2, 2024
ac844d1
Merge branch 'main' of https://github.com/interledger/testnet into 13…
sanducb Aug 6, 2024
7c5eee5
Update migrations + partial asset display fix
sanducb Aug 7, 2024
7cd87d4
Prettier
sanducb Aug 7, 2024
3733fa6
Fix formatting?
sanducb Aug 7, 2024
3c410d8
Merge branch 'main' of https://github.com/interledger/testnet into 13…
sanducb Aug 9, 2024
ff4b64e
Try fix migration
sanducb Aug 9, 2024
2bad249
Try fix migration - amend
sanducb Aug 9, 2024
6e6b7f4
Prettier
sanducb Aug 9, 2024
7e6c73b
Update knex config
sanducb Aug 9, 2024
3d2189d
Update backend and migration
sanducb Aug 13, 2024
317d2a3
Update Rafiki service interaction with Rapyd
sanducb Aug 19, 2024
e450145
Prettier
sanducb Aug 19, 2024
7fdb4fe
Increase Rapyd threshold
sanducb Aug 19, 2024
b78f24d
remove asset scale imbalance transaction from list
Tymmmy Aug 19, 2024
308306e
next version update for error fixes
Tymmmy Aug 19, 2024
dfbf7f3
Update Rapyd threshold
sanducb Aug 20, 2024
021c9b8
Use truncate for amounts to prevent ceiling
sanducb Aug 20, 2024
b920316
Skip imbalance transactions when syncing balances
sanducb Aug 20, 2024
4601b42
Prevent ceiling when formatting amounts in frontend
sanducb Aug 20, 2024
1593a26
Use scale 2 in quote dialog and notifications
sanducb Aug 20, 2024
ed44242
Prettier
sanducb Aug 20, 2024
ba0ff7b
Remove unused file
sanducb Aug 21, 2024
29cc5e9
Cache all WAs
sanducb Aug 21, 2024
8b1f95f
Fix balance inconsistency on AccountCard
sanducb Aug 21, 2024
1f81cc6
Fix inconsistency in send and request screens, show scale 9
sanducb Aug 21, 2024
db4de9c
Remove console log
sanducb Aug 21, 2024
df198f3
Fix amounts sent to Rapyd
sanducb Aug 21, 2024
9723429
Prettier
sanducb Aug 21, 2024
27f32c3
Fix type mismatch
sanducb Aug 21, 2024
3925b62
Replace asset scale hardcoded values with constants
sanducb Aug 21, 2024
e81c0f5
Prettier
sanducb Aug 21, 2024
650a72f
Fix balance calculation
sanducb Aug 21, 2024
119e4da
Prettier
sanducb Aug 21, 2024
b78cf23
Update index.tsx
Tymmmy Aug 22, 2024
79fd71a
Revert onboarding to EUR
sanducb Aug 22, 2024
d9e4d9a
Merge branch '1358-be-asset-scale-2-to-asset-scale-9' of https://gith…
sanducb Aug 22, 2024
939b7f4
Merge branch 'main' into 1358-be-asset-scale-2-to-asset-scale-9
Tymmmy Aug 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The `RAPYD_ACCESS_KEY` and `RAPYD_SECRET_KEY` variables values can be found in y
To create a new Interledger Test Wallet account, a verification email will be sent to the provided email address. If you want to send emails within the development environment, you will need to have a personal Sendgrid account and update the following environment variables: `SEND_EMAIL` to `true`, `SENDGRID_API_KEY` and `FROM_EMAIL`. If you prefer not to send emails in the development environment, simply set `SEND_EMAIL` to `false` and use the verification link found in the Docker `wallet-backend` container logs to finalize the registration process for a new user.

Cross-currency transactions are supported. To enable this functionality, you will need to register at [freecurrencyapi.com/](https://freecurrencyapi.com/) and update the `RATE_API_KEY` environment variable with your own API key.
Currencies can be added in the `admin` environment. For example `assetCode` is `EUR`, `assetScale` is `2`, and you will need to add an amount to `liquidity`.
Currencies can be added in the `admin` environment. For example `assetCode` is `USD`, `assetScale` is `2`, and you will need to add an amount to `liquidity`.
sanducb marked this conversation as resolved.
Show resolved Hide resolved

To have everything ready for `DEV` environment, we already set up some default values for Interledger Test Wallet, this way developers are ready to login without validation, and test e-commerce application without any additional setup:

Expand Down
2 changes: 1 addition & 1 deletion docker/dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ services:
RATE_API_KEY: ${RATE_API_KEY}
BASE_ASSET_SCALE: 2
MAX_ASSET_SCALE: 9
WM_THRESHOLD: 100000000
RAPYD_THRESHOLD: 10000000 # 0.01
Copy link
Contributor Author

@sanducb sanducb Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified this to 0.01 (which is the lowest non-zero amount representable in asset scale 2) instead of 0.1. Any reason to change it back?

DEBT_THRESHOLD: 5
REDIS_URL: redis://redis:6379/0
restart: always
Expand Down
2 changes: 1 addition & 1 deletion docker/prod/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ WALLET_BACKEND_AUTH_DOMAIN=
WALLET_BACKEND_RATE_API_KEY=
WALLET_BACKEND_BASE_ASSET_SCALE=
WALLET_BACKEND_MAX_ASSET_SCALE=
WALLET_BACKEND_WM_THRESHOLD=
WALLET_BACKEND_RAPYD_THRESHOLD=
WALLET_BACKEND_DEBT_THRESHOLD=

# BOUTIQUE
Expand Down
2 changes: 1 addition & 1 deletion docker/prod/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ services:
RATE_API_KEY: ${WALLET_BACKEND_RATE_API_KEY}
BASE_ASSET_SCALE: ${WALLET_BACKEND_BASE_ASSET_SCALE}
MAX_ASSET_SCALE: ${WALLET_BACKEND_MAX_ASSET_SCALE}
WM_THRESHOLD: ${WALLET_BACKEND_WM_THRESHOLD}
RAPYD_THRESHOLD: ${WALLET_BACKEND_RAPYD_THRESHOLD}
DEBT_THRESHOLD: ${WALLET_BACKEND_DEBT_THRESHOLD}
REDIS_URL: ${WALLET_BACKEND_REDIS_URL}
networks:
Expand Down
14 changes: 14 additions & 0 deletions packages/wallet/backend/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ module.exports = {
}
},

rafiki_backend: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this added?

Copy link
Contributor Author

@sanducb sanducb Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this so that I can migrate assets to scale 9 in the assets table which is in rafiki_backend db. That requires a separate connection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an env variable instead with the connection string

client: 'postgresql',
connection: {
host: 'postgres',
user: 'postgres',
password: 'password',
database: 'rafiki_backend'
},
pool: {
min: 2,
max: 10
}
},

testing: {
client: 'postgresql',
connection: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const Knex = require('knex')
const knexConfig = require('../knexfile')

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
if (process.env.NODE_ENV !== 'test') {
const rafikiKnex = Knex(knexConfig.rafiki_backend)

try {
const assets = await rafikiKnex('assets').distinct('code')

for (const asset of assets) {
const { code } = asset

const existingAssetWithScale9 = await rafikiKnex('assets')
.where({ code, scale: 9 })
.first()

if (existingAssetWithScale9) {
await knex('accounts')
.where({ assetCode: code, assetScale: 2 })
.update({
assetId: existingAssetWithScale9.id,
assetScale: 9
})
} else {
await rafikiKnex('assets')
.where({ code, scale: 2 })
.update({ scale: 9 })
}
}
} finally {
await rafikiKnex.destroy()
}
}

const walletAddresses = await knex('walletAddresses').select(
'id',
'accountId'
)
for (const walletAddress of walletAddresses) {
const account = await knex('accounts')
.where('id', walletAddress.accountId)
.first()
if (account) {
await knex('walletAddresses')
.where('id', walletAddress.id)
.update({ assetCode: account.assetCode })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would asset code change?

Copy link
Contributor Author

@sanducb sanducb Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens because assetCode was null for every WA for some reason and that was causing keepBalancesSynced to fail when checking that asset code exists for every WA. Until now, we only populated assetCode column on WAs table for WM payment pointers. Therefore, I changed the asset code from null to whatever asset code the account has when migrating to prevent issues.

}
}

await knex('walletAddresses').update({ assetScale: 9 })
await knex('accounts').update({ assetScale: 9 })
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
if (process.env.NODE_ENV !== 'test') {
const rafikiKnex = Knex(knexConfig.rafiki_backend)

try {
const assets = await rafikiKnex('assets').distinct('code')

for (const asset of assets) {
const { code } = asset

const existingAssetWithScale2 = await rafikiKnex('assets')
.where({ code, scale: 2 })
.first()

if (existingAssetWithScale2) {
await knex('accounts')
.where({ assetCode: code, assetScale: 9 })
.update({
assetId: existingAssetWithScale2.id,
assetScale: 2
})
} else {
await rafikiKnex('assets')
.where({ code, scale: 9 })
.update({ scale: 2 })
}
}
} finally {
await rafikiKnex.destroy()
}
}

const walletAddresses = await knex('walletAddresses').select(
'id',
'accountId'
)
for (const walletAddress of walletAddresses) {
const account = await knex('accounts')
.where('id', walletAddress.accountId)
.first()
if (account) {
await knex('walletAddresses')
.where('id', walletAddress.id)
.update({ assetCode: account.assetCode })
}
}

await knex('walletAddresses').update({ assetScale: 2 })
await knex('accounts').update({ assetScale: 2 })
}
12 changes: 9 additions & 3 deletions packages/wallet/backend/src/account/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { transformBalance } from '@/utils/helpers'
import { Transaction } from '@/transaction/model'
import { Amount } from '@/rafiki/service'
import { Conflict, NotFound } from '@shared/backend'
import { Env } from '@/config/env'

type CreateAccountArgs = {
userId: string
Expand Down Expand Up @@ -38,7 +39,8 @@ interface IAccountService {
export class AccountService implements IAccountService {
constructor(
private rapydClient: RapydClient,
private rafikiClient: RafikiClient
private rafikiClient: RafikiClient,
private env: Env
) {}

public async createAccount(args: CreateAccountArgs): Promise<Account> {
Expand Down Expand Up @@ -244,7 +246,10 @@ export class AccountService implements IAccountService {
accountId: existingAccount.id,
paymentId: transactions[transactions.length - 1].id,
assetCode: existingAccount.assetCode,
value: transformBalance(args.amount, asset.scale),
value: transformBalance(
args.amount,
this.env.MAX_ASSET_SCALE || asset.scale
),
type: 'INCOMING',
status: 'COMPLETED',
description: 'Fund account'
Expand Down Expand Up @@ -304,7 +309,8 @@ export class AccountService implements IAccountService {
name = 'USD Account'
): Promise<Account | undefined> {
const asset = (await this.rafikiClient.listAssets({ first: 100 })).find(
(asset) => asset.code === 'USD' && asset.scale === 2
(asset) =>
asset.code === 'USD' && asset.scale === this.env.MAX_ASSET_SCALE
)
if (!asset) {
return
Expand Down
13 changes: 6 additions & 7 deletions packages/wallet/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { UserController } from './user/controller'
import type { UserService } from './user/service'
import { SocketService } from './socket/service'
import { GrantService } from '@/grant/service'
import { WMTransactionService } from '@/webMonetization/transaction/service'
import { AwilixContainer } from 'awilix'
import { Cradle } from '@/createContainer'
import { initErrorHandler, RedisClient } from '@shared/backend'
Expand Down Expand Up @@ -78,7 +77,6 @@ export interface Bindings {
grantService: GrantService
emailService: EmailService
socketService: SocketService
wmTransactionService: WMTransactionService
}

export class App {
Expand Down Expand Up @@ -336,24 +334,25 @@ export class App {
})
}

private async processWMWalletAddresses() {
private async keepBalancesSynced(lastProcessedTimestamp: Date) {
const logger = await this.container.resolve('logger')
const walletAddressService = await this.container.resolve(
'walletAddressService'
)

return walletAddressService
.processWMWalletAddresses()
.keepBalancesSynced(lastProcessedTimestamp)
.catch((e) => {
logger.error(e)
return false
})
.then((trx) => {
const newTimestamp = new Date()
if (trx) {
process.nextTick(() => this.processWMWalletAddresses())
process.nextTick(() => this.keepBalancesSynced(newTimestamp))
} else {
setTimeout(
() => this.processWMWalletAddresses(),
() => this.keepBalancesSynced(lastProcessedTimestamp),
1000 * 60 * 5
).unref()
}
Expand All @@ -362,7 +361,7 @@ export class App {

async processResources() {
process.nextTick(() => this.processPendingTransactions())
process.nextTick(() => this.processWMWalletAddresses())
process.nextTick(() => this.keepBalancesSynced(new Date()))
}

async createDefaultUsers() {
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/backend/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const envSchema = z.object({
.transform((value) => value === 'true'),
BASE_ASSET_SCALE: z.coerce.number().nonnegative().default(2),
MAX_ASSET_SCALE: z.coerce.number().nonnegative().default(9),
WM_THRESHOLD: z.coerce.bigint().nonnegative().default(100_000_000n), // $0.1 in asset scale 9
RAPYD_THRESHOLD: z.coerce.bigint().nonnegative().default(100_000_00n), // $0.01 in asset scale 9
DEBT_THRESHOLD: z.coerce.number().multipleOf(0.01).nonnegative().default(5.0), // $5.00
DEFAULT_WALLET_ACCOUNT: z
.object({
Expand Down
6 changes: 0 additions & 6 deletions packages/wallet/backend/src/createContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { type Knex } from 'knex'
import { SocketService } from './socket/service'
import { GrantService } from './grant/service'
import { RatesService } from './rates/service'
import { WMTransactionService } from '@/webMonetization/transaction/service'
import { Logger } from 'winston'
import {
asClass,
Expand Down Expand Up @@ -69,7 +68,6 @@ export interface Cradle {
accountService: AccountService
ratesService: RatesService
redisClient: RedisClient
wmTransactionService: WMTransactionService
walletAddressService: WalletAddressService
walletAddressKeyService: WalletAddressKeyService
transactionService: TransactionService
Expand Down Expand Up @@ -119,10 +117,6 @@ export async function createContainer(
accountService: asClass(AccountService).singleton(),
ratesService: asClass(RatesService).singleton(),
redisClient: asFunction(createRedis).singleton(),
wmTransactionService: asClassSingletonWithLogger(
WMTransactionService,
logger
),
transactionService: asClassSingletonWithLogger(TransactionService, logger),
walletAddressService: asClassSingletonWithLogger(
WalletAddressService,
Expand Down
Loading
Loading