Skip to content

Commit

Permalink
Portfolio cards and Balance break down (#358)
Browse files Browse the repository at this point in the history
* Functions for calculating balance and cards info

* Balance and cards UI

* Add coin height check

* Split bids and names stats

* Handle finalize button

* Check isExpired

* Renew Many

* Refactor and move to background

* Remove card buttons

* Remove console logs
  • Loading branch information
rithvikvibhu authored Jun 13, 2021
1 parent 17c08db commit 70580a3
Show file tree
Hide file tree
Showing 7 changed files with 911 additions and 79 deletions.
82 changes: 82 additions & 0 deletions app/background/wallet/bulk-renewal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const rules = require("hsd/lib/covenants/rules");
const { types } = rules;
const { states } = require("hsd/lib/covenants/namestate");
const MTX = require("hsd/lib/primitives/mtx");
const Output = require("hsd/lib/primitives/output");

export const renewMany = async (wallet, names) => {
if (!Array.isArray(names)) {
throw new Error("names must be an array");
}

const mtx = new MTX();

for (const name of names) {
if (!rules.verifyName(name)) {
throw new Error("Invalid name.");
}

const rawName = Buffer.from(name, "ascii");
const nameHash = rules.hashName(rawName);
const ns = await wallet.getNameState(nameHash);
const height = wallet.wdb.height + 1;
const network = wallet.network;

if (!ns) {
throw new Error("Auction not found.");
}

const { hash, index } = ns.owner;
const coin = await wallet.getCoin(hash, index);

if (!coin) {
throw new Error(`Wallet does not own: "${name}".`);
}

if (ns.isExpired(height, network)) throw new Error("Name has expired!");

// Is local?
if (coin.height < ns.height) {
throw new Error(`Wallet does not own: "${name}".`);
}

const state = ns.state(height, network);

if (state !== states.CLOSED) {
throw new Error("Auction is not yet closed.");
}

if (
!coin.covenant.isRegister() &&
!coin.covenant.isUpdate() &&
!coin.covenant.isRenew() &&
!coin.covenant.isFinalize()
) {
throw new Error("Name must be registered.");
}

if (height < ns.renewal + network.names.treeInterval) {
throw new Error("Must wait to renew.");
}

const output = new Output();
output.address = coin.address;
output.value = coin.value;
output.covenant.type = types.RENEW;
output.covenant.pushHash(nameHash);
output.covenant.pushU32(ns.height);
output.covenant.pushHash(await wallet.wdb.getRenewalBlock());

mtx.addOutpoint(ns.owner);
mtx.outputs.push(output);
}

const unlock = await wallet.fundLock.lock();
try {
await wallet.fill(mtx);
const finalizedTX = await wallet.finalize(mtx);
await wallet.sendMTX(finalizedTX, null);
} finally {
unlock();
}
};
4 changes: 3 additions & 1 deletion app/background/wallet/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const clientStub = ipcRendererInjector => makeClient(ipcRendererInjector,
'sendRegisterAll',
'transferMany',
'finalizeMany',
'renewMany',
'sendTransfer',
'cancelTransfer',
'finalizeTransfer',
Expand All @@ -55,5 +56,6 @@ export const clientStub = ipcRendererInjector => makeClient(ipcRendererInjector,
'zap',
'importName',
'rpcGetWalletInfo',
'listWallets'
'listWallets',
'getStats',
]);
16 changes: 16 additions & 0 deletions app/background/wallet/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
import {SET_FEE_INFO, SET_NODE_INFO} from "../../ducks/nodeReducer";
import createRegisterAll from "./create-register-all";
import {finalizeMany, transferMany} from "./bulk-transfer";
import {renewMany} from "./bulk-renewal";
import {getStats} from "./stats";
import {get, put} from "../db/service";
import hsdLedger from 'hsd-ledger';

Expand Down Expand Up @@ -446,6 +448,12 @@ class WalletService {
await finalizeMany(wallet, names);
};

renewMany = async (names) => {
const {wdb} = this.node;
const wallet = await wdb.get(this.name);
await renewMany(wallet, names);
};

sendRevealAll = () => this._ledgerProxy(
() => this._executeRPC('createreveal', ['']),
() => this._executeRPC('sendreveal', [''], this.lock),
Expand Down Expand Up @@ -748,6 +756,12 @@ class WalletService {
return ret;
};

getStats = async () => {
const {wdb} = this.node;
const wallet = await wdb.get(this.name);
return getStats(wallet);
};

_onNodeStart = async (networkName, network, apiKey) => {
const conn = await getConnection();

Expand Down Expand Up @@ -1225,6 +1239,7 @@ const methods = {
sendRenewal: service.sendRenewal,
transferMany: service.transferMany,
finalizeMany: service.finalizeMany,
renewMany: service.renewMany,
sendTransfer: service.sendTransfer,
cancelTransfer: service.cancelTransfer,
finalizeTransfer: service.finalizeTransfer,
Expand All @@ -1241,6 +1256,7 @@ const methods = {
importName: service.importName,
rpcGetWalletInfo: service.rpcGetWalletInfo,
listWallets: service.listWallets,
getStats: service.getStats,
};

export async function start(server) {
Expand Down
Loading

0 comments on commit 70580a3

Please sign in to comment.