Skip to content

Commit

Permalink
chore: jsonMode for tx utxos
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed Oct 24, 2023
1 parent 8090df7 commit ae0af89
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 109 deletions.
8 changes: 3 additions & 5 deletions src/commands/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ export const registerTxCommand = (app: App<StringIndexed>) => {
return;
}

// TODO: jsonMode
const { txHash, network } = JSON.parse(action.value);
const { txHash, network, jsonMode } = JSON.parse(action.value);
const bClient = await initializeBlockfrostClient(body, { network });

if (!bClient) {
Expand All @@ -87,11 +86,10 @@ export const registerTxCommand = (app: App<StringIndexed>) => {

try {
const utxo = await bClient.getTxUtxo(txHash);
const formattedInputs = formatInputs(utxo.inputs);
const formattedOutputs = formatOutputs(utxo.outputs);
const formattedInputs = formatInputs(utxo.inputs, jsonMode);
const formattedOutputs = formatOutputs(utxo.outputs, jsonMode);
// Slack limits the number of blocks that can be send at once to 50.
// For now we split the inputs and outputs to separate messages.
// TODO: Handle large transaction with more than 50 inputs or outputs

await say({
blocks: [
Expand Down
250 changes: 146 additions & 104 deletions src/utils/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,129 +24,171 @@ export const lovelaceToAda = (lovelace: number | string) => {
return formatAssetDecimals(lovelace, 6);
};

export const formatInputs = (inputs: Responses['tx_content_utxo']['inputs']) => {
return inputs
.map((input, index) => {
const amount =
BigInt(input.amount.find(a => a.unit === 'lovelace')?.quantity ?? '0') / 100000000n;
const tokens = input.amount.filter(amount => amount.unit !== 'lovelace');

const response: SayArguments['blocks'] = [
{
type: 'header',
text: {
type: 'plain_text',
text: `Input #${index + 1}`,
},
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Tx Hash*\n\`${input.tx_hash}\``,
},
{
type: 'mrkdwn',
text: `*Index*\n${input.output_index}`,
export const formatInputs = (inputs: Responses['tx_content_utxo']['inputs'], jsonMode: boolean) => {
if (!jsonMode) {
return inputs
.map((input, index) => {
const amount =
BigInt(input.amount.find(a => a.unit === 'lovelace')?.quantity ?? '0') / 100000000n;
const tokens = input.amount.filter(amount => amount.unit !== 'lovelace');

const response: SayArguments['blocks'] = [
{
type: 'header',
text: {
type: 'plain_text',
text: `Input #${index + 1}`,
},
],
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `📘 *Address*\n\`${input.address}\``,
},
},
{
type: 'section',
fields: [
{
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Tx Hash*\n\`${input.tx_hash}\``,
},
{
type: 'mrkdwn',
text: `*Index*\n${input.output_index}`,
},
],
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Amount*\n${amount} ADA`,
text: `📘 *Address*\n\`${input.address}\``,
},
{
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Amount*\n${amount} ADA`,
},
{
type: 'mrkdwn',
text: `*Collateral*\n${input.collateral ? 'Yes' : 'No'}`,
},
],
},
];

if (tokens.length > 0) {
// Append tokens block
response.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `*Collateral*\n${input.collateral ? 'Yes' : 'No'}`,
text: `*Assets*\n\`\`\`${JSON.stringify(tokens, undefined, 2)}\`\`\``,
},
],
},
];

if (tokens.length > 0) {
// Append tokens block
});
}
response.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `*Assets*\n\`\`\`${JSON.stringify(tokens, undefined, 2)}\`\`\``,
},
type: 'divider',
});
}
response.push({
type: 'divider',
});

return response;
})
.flat();
return response;
})
.flat();
} else {
// JSON mode
return [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Inputs*\n\`\`\`${JSON.stringify(
inputs.map(input => truncateLongStrings(input)),
undefined,
2,
)}\`\`\``,
},
},
{
type: 'divider',
},
];
}
};

export const formatOutputs = (outputs: Responses['tx_content_utxo']['outputs']) => {
return outputs
.map((output, index) => {
const amount =
BigInt(output.amount.find(a => a.unit === 'lovelace')?.quantity ?? '0') / 100000000n;
const tokens = output.amount.filter(amount => amount.unit !== 'lovelace');

const response: SayArguments['blocks'] = [
{
type: 'header',
text: {
type: 'plain_text',
text: `Output #${index + 1}`,
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `📘 *Address*\n\`${output.address}\``,
export const formatOutputs = (
outputs: Responses['tx_content_utxo']['outputs'],
jsonMode: boolean,
) => {
if (!jsonMode) {
return outputs
.map((output, index) => {
const amount =
BigInt(output.amount.find(a => a.unit === 'lovelace')?.quantity ?? '0') / 100000000n;
const tokens = output.amount.filter(amount => amount.unit !== 'lovelace');

const response: SayArguments['blocks'] = [
{
type: 'header',
text: {
type: 'plain_text',
text: `Output #${index + 1}`,
},
},
},
{
type: 'section',
fields: [
{
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Amount*\n${amount} ADA`,
text: `📘 *Address*\n\`${output.address}\``,
},
{
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Amount*\n${amount} ADA`,
},
{
type: 'mrkdwn',
text: `*Collateral*\n${output.collateral ? 'Yes' : 'No'}`,
},
],
},
];

if (tokens.length > 0) {
// Append tokens block
response.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `*Collateral*\n${output.collateral ? 'Yes' : 'No'}`,
text: `*Assets*\n\`\`\`${JSON.stringify(tokens, undefined, 2)}\`\`\``,
},
],
},
];

if (tokens.length > 0) {
// Append tokens block
});
}
response.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `*Assets*\n\`\`\`${JSON.stringify(tokens, undefined, 2)}\`\`\``,
},
type: 'divider',
});
}
response.push({
type: 'divider',
});

return response;
})
.flat();
return response;
})
.flat();
} else {
return [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Outputs*\n\`\`\`${JSON.stringify(
outputs.map(output => truncateLongStrings(output)),
undefined,
2,
)}\`\`\``,
},
},
{
type: 'divider',
},
];
}
};

export const formatUnixTimestamp = (ts: number) => {
Expand Down

0 comments on commit ae0af89

Please sign in to comment.