Skip to content

Commit

Permalink
fix: token creation tx was not sending token_name and token_symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed May 6, 2021
1 parent 07b9c1e commit 11ab509
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 16 deletions.
31 changes: 17 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export interface Output {

export interface Token {
uid: string;
name: string;
symbol: string;
// Hathor will return name: null and symbol: null
name: string | null;
symbol: string | null;
}

export interface FullTx {
Expand All @@ -50,23 +51,29 @@ export interface FullTx {
version: number;
weight: number;
parents: string[];
tokenName?: string | null;
tokenSymbol?: string | null;
inputs: Input[];
outputs: Output[];
tokens?: Token[];
height?: number;
raw?: string;
}

export interface FullBlock {
txId: string;
nonce: string;
timestamp: number;
version: number;
weight: number;
timestamp: number;
isVoided: boolean;
parents: string[];
tokenName?: string | null;
tokenSymbol?: string | null;
inputs: Input[];
outputs: Output[];
parents: string[];
tokens?: Token[];
height: number;
raw?: string;
}

export interface ApiResponse {
Expand Down Expand Up @@ -170,12 +177,6 @@ export interface PreparedOutput {
decoded: PreparedDecodedScript;
}

export interface PreparedToken {
uid: string;
name: string;
symbol: string;
}

export interface PreparedTx {
tx_id: string;
inputs: PreparedInput[];
Expand All @@ -186,13 +187,15 @@ export interface PreparedTx {
parents: string[];
nonce?: string;
height?: number;
tokens?: PreparedToken[];
token_name?: string;
token_symbol?: string;
tokens?: Token[];
token_name?: string | null;
token_symbol?: string | null;
raw?: string;
}

export interface RawTxResponse {
tx: any;
meta: any;
success?: boolean;
spent_outputs?: any;
};
14 changes: 12 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ export const recursivelyDownloadTx = async (blockId: string, txIds: string[] = [
*/
export const prepareTx = (tx: FullTx | FullBlock): PreparedTx => {
const prepared = {
...tx,
tx_id: tx.txId,
raw: '',
nonce: tx.nonce,
timestamp: tx.timestamp,
version: tx.version,
weight: tx.weight,
parents: tx.parents,
token_name: tx.tokenName,
token_symbol: tx.tokenSymbol,
height: tx.height,
inputs: tx.inputs.map((input) => {
const baseInput: PreparedInput = {
value: input.value,
Expand Down Expand Up @@ -142,6 +148,8 @@ export const prepareTx = (tx: FullTx | FullBlock): PreparedTx => {
token: uid,
};
}),
tokens: tx.tokens,
raw: tx.raw,
};

return prepared;
Expand All @@ -159,6 +167,8 @@ export const parseTx = (tx: any): FullTx => {
version: tx.version as number,
weight: tx.weight as number,
timestamp: tx.timestamp as number,
tokenName: tx.token_name ? tx.token_name as string : null,
tokenSymbol: tx.token_symbol ? tx.token_symbol as string : null,
inputs: tx.inputs.map((input) => {
const typedDecodedScript: DecodedScript = {
type: input.decoded.type as string,
Expand Down
16 changes: 16 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import {
BLOCK_BY_HEIGHT,
MOCK_TXS,
MOCK_FULL_TXS,
MOCK_CREATE_TOKEN_TX,
generateBlock,
} from './utils';
import {
prepareTx,
parseTx,
} from '../src/utils';
import * as Utils from '../src/utils';
import * as FullNode from '../src/api/fullnode';
import * as Lambda from '../src/api/lambda';
Expand Down Expand Up @@ -238,3 +243,14 @@ test('LRU cache', async () => {
expect(cache.get('tx3')).toStrictEqual(undefined);
expect(cache.first()).toStrictEqual('tx4');
}, 500);

test('prepareTx on a CREATE_TOKEN tx should have token_name and token_symbol', async () => {
expect.hasAssertions();

const { tx } = MOCK_CREATE_TOKEN_TX;
const parsedTx = parseTx(tx);
const preparedTx = prepareTx(parsedTx);

expect(preparedTx.token_name).toStrictEqual('XCoin');
expect(preparedTx.token_symbol).toStrictEqual('XCN');
}, 500);
89 changes: 89 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
FullBlock,
FullTx,
Block,
RawTxResponse,
} from '../src/types';

export const MOCK_TXS = [
Expand Down Expand Up @@ -183,3 +184,91 @@ export const BLOCK_BY_HEIGHT: FullBlock = {
height: 3
};

export const MOCK_CREATE_TOKEN_TX: RawTxResponse = {
success: true,
tx: {
hash: "0035db82f5993097515d5bcc9e869700d538332e017c7ff599c47f659ab63d42",
nonce: "180",
timestamp: 1620266110,
version: 2,
weight: 8.000001,
parents: [
"00504c97802cc199e2e418aefdafd1a627fdc4cf6fc9e4198b916c2456bbb203",
"0063b3ec31f8ffe0ebcb465e6c1111e1e9700926ac4d504c74b74b1af9cc6aad"
],
inputs: [{
value: 1,
token_data: 0,
script: "dqkURCVU2U54vCcmN8UVMeIBKQ+ldayIrA==",
decoded: {
type: "P2PKH",
address: "WUtMYoi96nNVgf6i3Rq3GuvJkYsbkx3KDi",
timelock: null,
value: 1,
token_data: 2
},
tx_id: "00504c97802cc199e2e418aefdafd1a627fdc4cf6fc9e4198b916c2456bbb203",
index: 1
}],
outputs: [{
value: 100,
token_data: 1,
script: "dqkU1vXqQItRBKC9TwophPs9I5reNnOIrA==",
decoded: {
type: "P2PKH",
address: "WiGe5TRjhAsrYP2dxp1zsgvYZqcBjXdWmy",
timelock: null,
value: 100,
token_data: 1
}
}, {
value: 1,
token_data: 129,
script: "dqkUvKVTGtZCXV/Wmwxsdc47FUnf8f6IrA==",
decoded: {
type: "P2PKH",
address: "WfsVxwxZhrfKHSYCeqPubQkWaeBcWZJ1ox",
timelock: null,
value: 1,
token_data: 129
}
}, {
value: 2,
token_data: 129,
script: "dqkU6v6yo/94Z55pVSHPv+gTJWLln22IrA==",
decoded: {
type: "P2PKH",
address: "Wk6a7Xif6qYsprSzFmFhVXYrgQdqg7h1K6",
timelock: null,
value: 2,
token_data: 129
}
}],
tokens: [{
uid: "0035db82f5993097515d5bcc9e869700d538332e017c7ff599c47f659ab63d42",
name: "XCoin",
symbol: "XCN"
}, {
uid: "00",
name: null,
symbol: null
}],
token_name: "XCoin",
token_symbol: "XCN",
raw: ""
},
meta: {
hash: "0035db82f5993097515d5bcc9e869700d538332e017c7ff599c47f659ab63d42",
spent_outputs: [ [ 0, [] ], [ 1, [] ], [ 2, [] ] ],
received_by: [],
children: [],
conflict_with: [],
voided_by: [],
twins: [],
accumulated_weight: 25.78875940418488,
score: 0.0,
height: 0,
first_block: "000000bd45ecc5119963cc3fa03e894f574e69811eef266ed7c6a0d4c1e1806c"
},
spent_outputs: {}
}

0 comments on commit 11ab509

Please sign in to comment.