import { TransactionType } from '../faces/transaction' ; import { Knex } from 'knex' ; import { Utils } from '../utils/utils' ; export interface Wallet { address : string ; balance : number ; } export class WalletDB { private connection : Knex ; constructor ( connection ) { this . connection = connection ; } async addWallet ( wallet ) { return await this . connection . insert ( { id : Utils . randomID ( 64 ) , address : wallet . address , balance : wallet . balance , } ) . into ( 'wallets' ) ; } async getWallet ( address ) { return ( await this . connection . queryBuilder ( ) . select ( '*' ) . from ( 'wallets' ) . where ( 'address' , '=' , address ) . limit ( 1 ) ) [ 0 ] ; } async getWalletBalance ( address ) { const wallet = await this . getWallet ( address ) ; if ( ! wallet ) return 0 ; return wallet . balance ; } async getLastTx ( address ) { return ( await this . connection . queryBuilder ( ) . select ( '*' ) . from ( 'transactions' ) . where ( 'owner_address' , '=' , address ) . orderBy ( 'created_at' , 'desc' ) . limit ( 1 ) ) [ 0 ] ; } async updateBalance ( address , balance ) { return await this . connection ( 'wallets' ) . update ( { balance } ) . where ( { address } ) ; } async incrementBalance ( address , balance ) { try { return await this . connection ( 'wallets' ) . increment ( 'balance' , balance ) . where ( { address } ) ; } catch ( error ) { console . log ( { error } ) ; } } }