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

Add balanceAtBlock XRP method #3042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 16 additions & 6 deletions packages/bitcore-client/bin/wallet-balance
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ try {
.version(require('../package.json').version)
.option('--name <name>', 'REQUIRED - Wallet name')
.option('--time [time]', 'optional - Get balance at specific time')
.option('--block [block]', 'optional - Get balance at specific block height')
.option('--path [path]', 'optional - Custom wallet storage path')
.option('--storageType [storageType]', 'optional - name of the database to use (default level)')
.option('--token [token]', 'optional - Get balance of an ERC20 token')
Expand All @@ -19,12 +20,21 @@ try {

async function main() {
const { name, path, time, storageType, token } = program;
const wallet = await Wallet.loadWallet({ name, path, storageType });
const balance = await wallet.getBalance(time, token);
return Object.assign(balance, {
currency: token || wallet.chain,
network: wallet.network
});
if (program.block) {
const wallet = await Wallet.loadWallet({ name, path, storageType });
const balance = await wallet.getBalanceAtBlock(program.block, token);
return Object.assign(balance, {
currency: token || wallet.chain,
network: wallet.network
});
} else {
const wallet = await Wallet.loadWallet({ name, path, storageType });
const balance = await wallet.getBalance(time, token);
return Object.assign(balance, {
currency: token || wallet.chain,
network: wallet.network
});
}
}

main()
Expand Down
20 changes: 20 additions & 0 deletions packages/bitcore-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ export class Client {
url += `?tokenAddress=${payload.tokenContractAddress}`;
}
const signature = this.sign({ method: 'GET', url });
console.log(url);
console.log(signature);
return request.get(url, {
headers: { 'x-signature': signature },
json: true
});
}

async getBalanceAtBlock(params: { payload?: any; pubKey: string; block?: string }) {
const { payload, pubKey, block } = params;
let url = `${this.apiUrl}/wallet/${pubKey}/balanceAtBlock`;
if (block) {
url += `/${block}`;
}
if (payload && payload.tokenContractAddress) {
url += `?tokenAddress=${payload.tokenContractAddress}`;
}
const signature = this.sign({ method: 'GET', url });
console.log(url);
console.log(signature);
return request.get(url, {
headers: { 'x-signature': signature },
json: true
Expand Down
14 changes: 14 additions & 0 deletions packages/bitcore-client/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ export class Wallet {
return this.client.getBalance({ payload, pubKey: this.authPubKey, time });
}

getBalanceAtBlock(block?: string, token?: string) {
let payload;
if (token) {
let tokenContractAddress;
const tokenObj = this.tokens.find(tok => tok.symbol === token);
if (!tokenObj) {
throw new Error(`${token} not found on wallet ${this.name}`);
}
tokenContractAddress = tokenObj.address;
payload = { tokenContractAddress };
}
return this.client.getBalanceAtBlock({ payload, pubKey: this.authPubKey, block });
}

getNetworkFee(params: { target?: number } = {}) {
const target = params.target || 2;
return this.client.getFee({ target });
Expand Down
94 changes: 68 additions & 26 deletions packages/bitcore-node/src/modules/ripple/api/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
GetBalanceForAddressParams,
GetBlockBeforeTimeParams,
GetEstimateSmartFeeParams,
GetWalletBalanceAtBlockParams,
GetWalletBalanceParams,
IChainStateService,
StreamAddressUtxosParams,
Expand Down Expand Up @@ -71,33 +72,57 @@ export class RippleStateProvider extends InternalStateProvider implements IChain

async getBalanceForAddress(params: GetBalanceForAddressParams) {
const { chain, network, address } = params;
const lowerAddress = address.toLowerCase();
const cacheKey = `getBalanceForAddress-${chain}-${network}-${lowerAddress}`;
return CacheStorage.getGlobalOrRefresh(
cacheKey,
async () => {
const client = await this.getClient(network);
try {
const info = await client.getAccountInfo(address);
const confirmed = Math.round(Number(info.xrpBalance) * 1e6);
const balance = confirmed;
const unconfirmed = 0;
return { confirmed, unconfirmed, balance };
} catch (e) {
if (e && e.data && e.data.error_code === 19) {
// Error code for when we have derived an address,
// but the account has not yet been funded
return {
confirmed: 0,
unconfirmed: 0,
balance: 0
};
}
throw e;
if (params.args && params.args.block) {
const block = params.args.block;
const client = await this.getClient(network);
try {
const blockNum = parseInt(block);
const info = await client.getAccountInfo(address, { ledgerVersion: blockNum });
const confirmed = Math.round(Number(info.xrpBalance) * 1e6);
const balance = confirmed;
const unconfirmed = 0;
return { confirmed, unconfirmed, balance };
} catch (e) {
if (e && e.data && e.data.error_code === 19) {
// Error code for when we have derived an address,
// but the account has not yet been funded
return {
confirmed: 0,
unconfirmed: 0,
balance: 0
};
}
},
CacheStorage.Times.Hour / 2
);
throw e;
}
} else {
const lowerAddress = address.toLowerCase();
const cacheKey = `getBalanceForAddress-${chain}-${network}-${lowerAddress}`;
return CacheStorage.getGlobalOrRefresh(
cacheKey,
async () => {
const client = await this.getClient(network);
try {
const info = await client.getAccountInfo(address);
const confirmed = Math.round(Number(info.xrpBalance) * 1e6);
const balance = confirmed;
const unconfirmed = 0;
return { confirmed, unconfirmed, balance };
} catch (e) {
if (e && e.data && e.data.error_code === 19) {
// Error code for when we have derived an address,
// but the account has not yet been funded
return {
confirmed: 0,
unconfirmed: 0,
balance: 0
};
}
throw e;
}
},
CacheStorage.Times.Hour / 2
);
}
}

async getBlock(params: GetBlockParams) {
Expand Down Expand Up @@ -175,6 +200,23 @@ export class RippleStateProvider extends InternalStateProvider implements IChain
);
}

async getWalletBalanceAtBlock(params: GetWalletBalanceAtBlockParams) {
const { chain, block, network } = params;
const addresses = await this.getWalletAddresses(params.wallet._id!);
const balances = await Promise.all(
addresses.map(a => this.getBalanceForAddress({ address: a.address, chain, network, args: { block } }))
);
return balances.reduce(
(total, current) => {
total.balance += current.balance;
total.confirmed += current.confirmed;
total.unconfirmed += current.unconfirmed;
return total;
},
{ confirmed: 0, unconfirmed: 0, balance: 0 }
);
}

streamTxs<T>(txs: Array<T>, stream: Readable) {
for (let tx of txs) {
stream.push(tx);
Expand Down
17 changes: 17 additions & 0 deletions packages/bitcore-node/src/modules/ripple/api/xrp-routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from 'express';
import { XRP } from './csp';
import { Auth, AuthenticatedRequest } from '../../../utils/auth';
export const XrpRoutes = Router();

XrpRoutes.get('/api/XRP/:network/address/:address/txs/count', async (req, res) => {
Expand All @@ -11,3 +12,19 @@ XrpRoutes.get('/api/XRP/:network/address/:address/txs/count', async (req, res) =
res.status(500).send(err);
}
});

XrpRoutes.get('/api/:chain/:network/wallet/:pubKey/balanceAtBlock/:block', Auth.authenticateMiddleware, async (req: AuthenticatedRequest, res) => {
let { network, block } = req.params;
try {
const result = await XRP.getWalletBalanceAtBlock({
chain: 'XRP',
network,
wallet: req.wallet!,
block,
args: req.query
});
return res.send(result || { confirmed: 0, unconfirmed: 0, balance: 0 });
} catch (err) {
return res.status(500).json(err);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export type GetWalletBalanceAtTimeParams = ChainNetwork & {
args: any;
};

export type GetWalletBalanceAtBlockParams = ChainNetwork & {
wallet: MongoBound<IWallet>;
block: string
args: any;
};

export type StreamAddressUtxosParams = ChainNetwork & {
address: string;
req?: Request;
Expand Down