Skip to content

Commit

Permalink
feat: add wallet route
Browse files Browse the repository at this point in the history
add wallet route with balance and address, may be consider deprecating balance route
  • Loading branch information
gridcat committed Nov 25, 2021
1 parent 2057e60 commit 91a2832
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/grc-stamp/src/controllers/WalletController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import HttpStatus from 'http-status-codes';
import { Request, Response } from 'express';
import { BalancePresenter } from '../presenters/balance.presenter';
import { WalletPresenter } from '../presenters/wallet.presenter';
import { WalletRepository } from '../repositories/WalletRepository';
import { Controller } from './BaseController';
import { ErrorModel } from '../models/Error';
Expand All @@ -10,18 +11,38 @@ export class WalletController extends Controller {
req: Request,
res: Response,
private repository = WalletRepository,
private balancePresenter = BalancePresenter,
private walletPresenter = WalletPresenter,
) {
super(req, res);
this.presenter = BalancePresenter;
this.init();
}

public async getWalletInfo(): Promise<void> {
try {
const wallet = await this.repository.getWalletInfo();
this.res
.status(HttpStatus.OK)
.send(this.render(wallet, this.walletPresenter));
} catch (e) {
console.error(e);
this.res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({
errors: [
new ErrorModel(
HttpStatus.INTERNAL_SERVER_ERROR,
HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR),
),
],
});
}
}

public async getBalance(): Promise<void> {
try {
const balance = await this.repository.getBalance();
this.res
.status(HttpStatus.OK)
.send(this.render(balance));
.send(this.render(balance, this.balancePresenter));
} catch (e) {
console.error(e);
this.res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({
Expand Down
7 changes: 7 additions & 0 deletions packages/grc-stamp/src/models/Wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Address } from 'gridcoin-rpc/dist/types';

export class Wallet {
public balance: number;

public address: Address;
}
24 changes: 24 additions & 0 deletions packages/grc-stamp/src/presenters/wallet.presenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import yayson from 'yayson';
import { Wallet } from '../models/Wallet';
import { Attributes } from './types';

const { Presenter } = yayson();

export class WalletPresenter extends Presenter {
public selfLinks(): string {
return '/wallet/';
}

public attributes(instanse: Wallet): Attributes {
return {
address: instanse.address,
balance: instanse.balance,
};
}

public id(instance: Wallet): string {
return instance.address;
}
}

WalletPresenter.prototype.type = 'wallet';
16 changes: 16 additions & 0 deletions packages/grc-stamp/src/repositories/WalletRepository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { rpc } from '../lib/gridcoin';
import { Wallet } from '../models/Wallet';

export class WalletRepositoryClass {
constructor(private grcRpc = rpc) {}

public async getWalletInfo(): Promise<Wallet> {
const [address, balance] = await Promise.all([
this.getAddress(),
this.getBalance(),
]);
const wallet = new Wallet();
wallet.address = address;
wallet.balance = balance;
return wallet;
}

public async getAddress(): Promise<string> {
return this.grcRpc.getAccountAddress('');
}

public async getBalance(): Promise<number> {
return this.grcRpc.getBalance();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/grc-stamp/src/routes/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { WalletController } from '../controllers/WalletController';

export const walletRouter = Router();

walletRouter.get('/', async (req: Request, res: Response) => {
const controller = new WalletController(req, res);
return controller.getWalletInfo();
});

walletRouter.get('/balance', async (req: Request, res: Response) => {
const controller = new WalletController(req, res);
return controller.getBalance();
Expand Down

0 comments on commit 91a2832

Please sign in to comment.