Skip to content

Commit

Permalink
fix: remove WM accounting (#1526)
Browse files Browse the repository at this point in the history
* Remove WM accounting

* Lint fix

* Fix frontend errors

* PR review fixes

* Delete 20240903125600_update_wallet_addresses.js

* remove Max asset scale env variable

* fix: remove unecessary migrations and fields

* fix: remove debt column

* fix: remove base asset scale env var

* test: remove WA asset scale/code

---------

Co-authored-by: Tymmmy <[email protected]>
Co-authored-by: dragosp1011 <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2024
1 parent 60eb193 commit f80db89
Show file tree
Hide file tree
Showing 36 changed files with 189 additions and 878 deletions.
4 changes: 0 additions & 4 deletions docker/dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ services:
FROM_EMAIL: ${FROM_EMAIL}
SEND_EMAIL: ${SEND_EMAIL:-false}
RATE_API_KEY: ${RATE_API_KEY}
BASE_ASSET_SCALE: 2
MAX_ASSET_SCALE: 9
WM_THRESHOLD: 100000000
DEBT_THRESHOLD: 5
REDIS_URL: redis://redis:6379/0
KRATOS_ADMIN_URL: 'http://kratos:4434/admin'
restart: always
Expand Down
4 changes: 0 additions & 4 deletions docker/prod/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ WALLET_BACKEND_FROM_EMAIL=
WALLET_BACKEND_SEND_EMAIL=
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_DEBT_THRESHOLD=

# BOUTIQUE
BOUTIQUE_BACKEND_PORT=
Expand Down
4 changes: 0 additions & 4 deletions docker/prod/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ services:
AUTH_IDENTITY_SERVER_SECRET: ${RAFIKI_AUTH_IDENTITY_SERVER_SECRET}
AUTH_DOMAIN: ${WALLET_BACKEND_AUTH_DOMAIN}
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}
DEBT_THRESHOLD: ${WALLET_BACKEND_DEBT_THRESHOLD}
REDIS_URL: ${WALLET_BACKEND_REDIS_URL}
networks:
- testnet
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ exports.up = async function (knex) {
table.dropForeign(['paymentPointerId'])
table.renameColumn('paymentPointerId', 'walletAddressId')
table.foreign('walletAddressId').references('walletAddresses.id')
}),
knex.schema.alterTable('wmTransactions', function (table) {
table.dropForeign(['paymentPointerId'])
table.renameColumn('paymentPointerId', 'walletAddressId')
table.foreign('walletAddressId').references('walletAddresses.id')
})
])
}
Expand All @@ -35,11 +30,6 @@ exports.down = async function (knex) {
table.dropForeign(['walletAddressId'])
table.renameColumn('walletAddressId', 'paymentPointerId')
table.foreign('paymentPointerId').references('paymentPointers.id')
}),
knex.schema.alterTable('wmTransactions', function (table) {
table.dropForeign(['walletAddressId'])
table.renameColumn('walletAddressId', 'paymentPointerId')
table.foreign('paymentPointerId').references('paymentPointers.id')
})
])
}
1 change: 0 additions & 1 deletion packages/wallet/backend/src/account/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class Account extends BaseModel {
public readonly userId!: string
public user!: User
public walletAddresses!: Array<WalletAddress>
public debt!: number

static relationMappings = () => ({
user: {
Expand Down
3 changes: 2 additions & 1 deletion 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 { DEFAULT_ASSET_SCALE } from '@/utils/consts'

type CreateAccountArgs = {
userId: string
Expand Down Expand Up @@ -304,7 +305,7 @@ 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 === DEFAULT_ASSET_SCALE
)
if (!asset) {
return
Expand Down
27 changes: 0 additions & 27 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,33 +334,8 @@ export class App {
})
}

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

return walletAddressService
.processWMWalletAddresses()
.catch((e) => {
logger.error(e)
return false
})
.then((trx) => {
if (trx) {
process.nextTick(() => this.processWMWalletAddresses())
} else {
setTimeout(
() => this.processWMWalletAddresses(),
1000 * 60 * 5
).unref()
}
})
}

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

async createDefaultUsers() {
Expand Down
4 changes: 0 additions & 4 deletions packages/wallet/backend/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ const envSchema = z.object({
.enum(['true', 'false'])
.default('false')
.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
DEBT_THRESHOLD: z.coerce.number().multipleOf(0.01).nonnegative().default(5.0), // $5.00
DEFAULT_WALLET_ACCOUNT: z
.object({
email: z.string().default('[email protected]'),
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 @@ -70,7 +69,6 @@ export interface Cradle {
accountService: AccountService
ratesService: RatesService
redisClient: RedisClient
wmTransactionService: WMTransactionService
walletAddressService: WalletAddressService
walletAddressKeyService: WalletAddressKeyService
transactionService: TransactionService
Expand Down Expand Up @@ -121,10 +119,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
65 changes: 9 additions & 56 deletions packages/wallet/backend/src/rafiki/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { UserService } from '@/user/service'
import { SocketService } from '@/socket/service'
import { NodeCacheInstance } from '@/utils/helpers'
import { WalletAddressService } from '@/walletAddress/service'
import { WMTransactionService } from '@/webMonetization/transaction/service'
import { Account } from '@/account/model'
import { WMTransaction } from '@/webMonetization/transaction/model'
import MessageType from '@/socket/messageType'
import { BadRequest } from '@shared/backend'

Expand Down Expand Up @@ -84,8 +82,7 @@ export class RafikiService implements IRafikiService {
private logger: Logger,
private rafikiClient: RafikiClient,
private transactionService: TransactionService,
private walletAddressService: WalletAddressService,
private wmTransactionService: WMTransactionService
private walletAddressService: WalletAddressService
) {}

public async onWebHook(wh: WebHook): Promise<void> {
Expand Down Expand Up @@ -167,27 +164,20 @@ export class RafikiService implements IRafikiService {
return amount
}

private amountToNumber(amount: Amount): number {
return +(Number(amount.value) * 10 ** -amount.assetScale).toFixed(
amount.assetScale
)
private amountToNumber(
amount: Amount,
toAssetScale: number = amount.assetScale
): number {
const factor = 10 ** toAssetScale
const scaledValue = Number(amount.value) * 10 ** -amount.assetScale
const truncatedValue = Math.floor(scaledValue * factor) / factor
return truncatedValue
}

private async handleIncomingPaymentCompleted(wh: WebHook) {
const walletAddress = await this.getWalletAddress(wh)
const amount = this.getAmountFromWebHook(wh)

if (walletAddress.isWM) {
await this.rafikiClient.withdrawLiqudity(wh.id)

await this.wmTransactionService.updateTransaction(
{ paymentId: wh.data.id },
{ status: 'COMPLETED', value: amount.value }
)

return
}

const receiverWalletId = await this.getRapydWalletId(walletAddress)

if (!this.validateAmount(amount, wh.type)) {
Expand Down Expand Up @@ -246,12 +236,6 @@ export class RafikiService implements IRafikiService {
private async handleIncomingPaymentCreated(wh: WebHook) {
const walletAddress = await this.getWalletAddress(wh)

if (walletAddress.isWM) {
await this.wmTransactionService.createIncomingTransaction(wh.data)

return
}

await this.transactionService.createIncomingTransaction(
wh.data,
walletAddress
Expand All @@ -262,12 +246,6 @@ export class RafikiService implements IRafikiService {
const walletAddress = await this.getWalletAddress(wh)
const amount = this.getAmountFromWebHook(wh)

if (walletAddress.isWM) {
await this.rafikiClient.depositLiquidity(wh.id)
await this.wmTransactionService.createOutgoingTransaction(wh.data)
return
}

const rapydWalletId = await this.getRapydWalletId(walletAddress)

if (!this.validateAmount(amount, wh.type)) {
Expand Down Expand Up @@ -304,17 +282,6 @@ export class RafikiService implements IRafikiService {
const walletAddress = await this.getWalletAddress(wh)
const debitAmount = this.getAmountFromWebHook(wh)

if (walletAddress.isWM) {
await this.rafikiClient.withdrawLiqudity(wh.id)

await this.wmTransactionService.updateTransaction(
{ paymentId: wh.data.id },
{ status: 'COMPLETED', value: debitAmount.value }
)

return
}

const source_ewallet = await this.getRapydWalletId(walletAddress)

if (!this.validateAmount(debitAmount, wh.type)) {
Expand Down Expand Up @@ -376,20 +343,6 @@ export class RafikiService implements IRafikiService {

const sentAmount = this.parseAmount(wh.data.sentAmount as AmountJSON)

if (walletAddress.isWM) {
await this.rafikiClient.withdrawLiqudity(wh.id)

const update: Partial<WMTransaction> = sentAmount.value
? { status: 'COMPLETED', value: sentAmount.value }
: { status: 'FAILED', value: 0n }
await this.wmTransactionService.updateTransaction(
{ paymentId: wh.data.id },
update
)

return
}

const source_ewallet = await this.getRapydWalletId(walletAddress)

const releaseResult = await this.rapydClient.releaseLiquidity({
Expand Down
3 changes: 1 addition & 2 deletions packages/wallet/backend/src/rapyd/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export class RapydController implements IRapydController {
accountId: defaultAccount.id,
walletAddressName,
publicName: 'Default Payment Pointer',
userId: id,
isWM: false
userId: id
})
}

Expand Down
Loading

0 comments on commit f80db89

Please sign in to comment.