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

feat: Adds Tests + Decouples parseAmount fn + fix BigNumber inital parse #20

Merged
merged 8 commits into from
Aug 8, 2024
Merged
18 changes: 18 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test
on:
push:
branches-ignore:
- main
jobs:
run_tests:
runs-on: ubuntu-20.04
container: node:20.15-alpine
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install
run: npm install
- name: Run tests
id: run_node_tests
run: |
npm run test --verbose
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "node --loader ts-node/esm src/tests/*.ts",
"build": "tsup",
"start": "node --experimental-import-meta-resolve dist/index.js",
"lint": "eslint src/**/*.ts",
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ethers } from "ethers";

export default function parseAmount(numberString: string): string {
try {
const number = BigInt(numberString);
return ethers.formatEther(number).toString();
} catch (e) {
return "";
}
}
12 changes: 2 additions & 10 deletions src/services/transaction-cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { decodeParameter, decodeParameters } from "web3-eth-abi";
import {ethers} from "ethers";

import logger from "../helpers/logger.js";
import parseAmount from "../helpers/parser.js";
import {
IAvailEvent,
IAvailExtrinsic,
Expand Down Expand Up @@ -654,7 +655,7 @@ export default class TransactionCron {
{
...sourceObj(),
amountPrettified: parseAmount(
new BigNumber(value.message.fungibleToken.amount, 16).toString()
new BigNumber(value.message.fungibleToken.amount, 16).toFixed()
),
},
"MessageExecuted"
Expand All @@ -681,12 +682,3 @@ export default class TransactionCron {
}
}
}

function parseAmount(numberString: string): string {
try {
const number = BigInt(numberString);
return ethers.formatEther(number).toString();
} catch (e) {
return "";
}
}
22 changes: 22 additions & 0 deletions src/tests/parse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from "assert/strict";
import { describe, it } from "node:test";
import { BigNumber } from "bignumber.js";
import parseAmount from "../helpers/parser.js";

describe("Test tx amount parse", () => {
it("Should parse rawFungibleTokenAmount on 18 digit BigNumber value", () => {
const rawFungibleTokenAmount = "0x00000000000000000de0b6b3a7640000";
const bigNumberString = BigNumber(rawFungibleTokenAmount, 16).toFixed();
const amountPrettified = parseAmount(bigNumberString);
const expectedParsedAmount = "1.0";
assert.deepStrictEqual(amountPrettified, expectedParsedAmount);
});

it("Should parse rawFungibleTokenAmount on 24 digit scientific BigNumber value", () => {
const rawFungibleTokenAmount = "0x000000000000182a697403a1b0cf0ad2";
const bigNumberString = BigNumber(rawFungibleTokenAmount, 16).toFixed();
const amountPrettified = parseAmount(bigNumberString);
const expectedParsedAmount = "114119.157542431558142674";
assert.deepStrictEqual(amountPrettified, expectedParsedAmount);
});
});