Skip to content

Commit

Permalink
Merge pull request #107 from blockfrost/feat/asset-fingerprint
Browse files Browse the repository at this point in the history
feat: add fingerprint and decimals fields to each asset
  • Loading branch information
vladimirvolek committed Oct 22, 2021
2 parents 744e202 + 488b498 commit 6ba74bc
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/methods/getAccountInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { txIdsToTransactions } from '../utils/transaction';
import { prepareMessage, prepareErrorMessage } from '../utils/message';
import { paginate } from '../utils/common';
import { transformToken } from '../utils/asset';
import { transformAsset } from '../utils/asset';

export default async (
id: number,
Expand Down Expand Up @@ -93,7 +93,7 @@ export default async (
};

if (details !== 'basic') {
accountInfo.tokens = tokensBalances.map(t => transformToken(t));
accountInfo.tokens = tokensBalances.map(t => transformAsset(t));
}

if (details === 'txs' || details === 'txids') {
Expand Down
2 changes: 1 addition & 1 deletion src/methods/getServerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { blockfrostAPI } from '../utils/blockfrostAPI';

export default async (id: number): Promise<string> => {
try {
const isTestnet = process.env.NETWORK.includes('testnet');
const isTestnet = !!blockfrostAPI.options.isTestnet;
const info = await blockfrostAPI.root();
const latestBlock = await blockfrostAPI.blocksLatest();
const serverInfo = {
Expand Down
6 changes: 5 additions & 1 deletion src/methods/getTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { prepareMessage, prepareErrorMessage } from '../utils/message';
import { blockfrostAPI } from '../utils/blockfrostAPI';
import { transformAsset } from '../utils/asset';

export default async (id: number, txId: string): Promise<string> => {
try {
const tx = await blockfrostAPI.txs(txId);
const message = prepareMessage(id, tx);
const message = prepareMessage(id, {
...tx,
output_amount: tx.output_amount.map(asset => transformAsset(asset)),
});

return message;
} catch (err) {
Expand Down
7 changes: 6 additions & 1 deletion src/types/address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Responses } from '@blockfrost/blockfrost-js';
import { AssetBalance } from './response';

export type Result = {
address: string;
Expand Down Expand Up @@ -45,6 +46,10 @@ export interface Utxo {
block: string;
}

export interface TransformedUtxo extends Omit<Utxo, 'amount'> {
amount: AssetBalance[];
}

export interface UtxosWithBlockResponse {
address: string;
utxoData: Utxo;
Expand All @@ -62,7 +67,7 @@ export interface UtxosWithBlocksBundle {
export type UtxosWithBlocksParams = {
address: string;
path: string;
data: Responses['address_utxo_content'] | 'empty';
data: TransformedUtxo[] | 'empty';
}[];

export type GetAddressDataBundle = {
Expand Down
5 changes: 3 additions & 2 deletions src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export interface ServerInfo {
export type BlockHash = string;

type Transactions = TxIdsToTransactionsResponse[];
export interface TokenBalance extends Balance {
export interface AssetBalance extends Balance {
fingerprint?: string; // lovelace has no fingerprint
decimals: number;
}

Expand All @@ -28,7 +29,7 @@ export interface AccountInfo {
empty: boolean;
availableBalance: string;
descriptor: string;
tokens?: TokenBalance[];
tokens?: AssetBalance[];
history: {
total: number; // total transactions
tokens?: number; // tokens transactions
Expand Down
17 changes: 11 additions & 6 deletions src/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
RewardAddress,
StakeCredential,
} from '@emurgo/cardano-serialization-lib-nodejs';
import { transformAsset } from './asset';

export const deriveAddress = (
publicKey: string,
Expand Down Expand Up @@ -124,12 +125,9 @@ export const addressesToBalances = (

return balances;
};

export const addressesToUtxos = async (
addresses: { address: string; path: string; data: Responses['address_content'] | 'empty' }[],
): Promise<
{ address: string; path: string; data: Responses['address_utxo_content'] | 'empty' }[]
> => {
): Promise<{ address: string; path: string; data: Addresses.TransformedUtxo[] | 'empty' }[]> => {
const promisesBundle: {
address: string;
path: string;
Expand All @@ -139,7 +137,7 @@ export const addressesToUtxos = async (
const result: {
address: string;
path: string;
data: Responses['address_utxo_content'] | 'empty';
data: Addresses.TransformedUtxo[] | 'empty';
}[] = [];

addresses.map(item => {
Expand All @@ -153,7 +151,14 @@ export const addressesToUtxos = async (
promisesBundle.map(p =>
p.promise
.then(data => {
result.push({ address: p.address, data, path: p.path });
result.push({
address: p.address,
data: data.map(utxo => ({
...utxo,
amount: utxo.amount.map(asset => transformAsset(asset)),
})),
path: p.path,
});
})
.catch(error => {
console.log(error);
Expand Down
10 changes: 8 additions & 2 deletions src/utils/asset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { parseAsset } from '@blockfrost/blockfrost-js';
import { Balance } from 'types/address';
import { TokenBalance } from 'types/response';
import { AssetBalance } from 'types/response';

export const transformAsset = (token: Balance): AssetBalance => {
if (token.unit === 'lovelace') {
return { ...token, decimals: 6 };
}

export const transformToken = (token: Balance): TokenBalance => {
return {
...token,
fingerprint: parseAsset(token.unit).fingerprint,
decimals: 0,
};
};
2 changes: 1 addition & 1 deletion src/utils/blockfrostAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import packageJson from '../../package.json';
const blockfrostAPI = new BlockFrostAPI({
projectId: process.env.PROJECT_ID,
customBackend: process.env.BACKEND_URL || '',
isTestnet: process.env.NETWORK.includes('testnet'),
isTestnet: process.env.NETWORK === 'testnet',
userAgent: `${packageJson.name}@${packageJson.version}`,
});

Expand Down
18 changes: 16 additions & 2 deletions src/utils/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Responses } from '@blockfrost/blockfrost-js';
import * as Types from '../types/transactions';
import { blockfrostAPI } from '../utils/blockfrostAPI';
import { transformAsset } from './asset';

export const txIdsToTransactions = async (
addresses: {
Expand Down Expand Up @@ -45,8 +46,21 @@ export const txIdsToTransactions = async (
.then(data => {
result.push({
address: p.address,
txData: data.txData,
txUtxos: data.txUtxos,
txData: {
...data.txData,
output_amount: data.txData.output_amount.map(asset => transformAsset(asset)),
},
txUtxos: {
...data.txUtxos,
inputs: data.txUtxos.inputs.map(input => ({
...input,
amount: input.amount.map(asset => transformAsset(asset)),
})),
outputs: data.txUtxos.outputs.map(output => ({
...output,
amount: output.amount.map(asset => transformAsset(asset)),
})),
},
blockInfo: data.blockInfo,
txHash: p.txHash,
});
Expand Down

0 comments on commit 6ba74bc

Please sign in to comment.