Skip to content

Commit

Permalink
fix: fixing fee render logic (#147)
Browse files Browse the repository at this point in the history
* fix: fixing fee render logic

* fix: render fee logic using data from Fee object

* chore: ternary condition if decimals is 0
  • Loading branch information
wainola authored Feb 28, 2024
1 parent e6fa560 commit 723978c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/components/ExplorerTable/ExplorerTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const ExplorerTable: React.FC<ExplorerTable> = ({ state, sharedConfig }: Explore
const fromDomainInfo = getDomainData(fromDomainId, sharedConfig)
const toDomainInfo = getDomainData(toDomainId, sharedConfig)

const formatedFee = getFormatedFee(fee, fromDomainInfo!)
const formatedFee = getFormatedFee(fee)

const fromDomainType = fromDomainInfo?.type

Expand Down
2 changes: 1 addition & 1 deletion src/pages/DetailView/DetailView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export default function DetailView() {
</div>
<div className={classes.detailsContainer}>
<span className={classes.detailsInnerContentTitle}>Fees:</span>
<span className={classes.detailsInnerContent}>{getFormatedFee(transfer?.fee!, fromDomainInfo!)}</span>
<span className={classes.detailsInnerContent}>{getFormatedFee(transfer?.fee!)}</span>
</div>
{Array.isArray(state.transferDetails) && <br />}
</Container>
Expand Down
12 changes: 11 additions & 1 deletion src/types/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export type Execution = {
timestamp: string
}

export type Fee = {
amount: string
id: string
resource: { decimals: number; id: string; type: string }
resourceID: string
tokenAddress: string
tokenSymbol: string
transferId: string
}

export type Transfer = {
id: string
depositNonce: number
Expand All @@ -101,7 +111,7 @@ export type Transfer = {
status: TransferStatus
deposit?: Deposit
execution?: Execution
fee: { amount: string; tokenAddress: string; tokenSymbol: string }
fee: Fee
resourceID: string
usdValue: number
accountId: string
Expand Down
41 changes: 18 additions & 23 deletions src/utils/Helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import { describe, it, expect } from "vitest"
import { formatDistanceStrict, sub } from "date-fns"
import { SharedConfigDomain } from "../types"
import { formatDistanceDate, getFormatedFee } from "./Helpers"

describe("Helpers", () => {
describe("getFormatedFee", () => {
it("parses ERC20 fee and returns fee with native token", () => {
const formatedFee = getFormatedFee(
{
amount: "1000000000000000",
tokenAddress: "0x0000000000000000000000000000000000000000",
tokenSymbol: "eth",
},
{
nativeTokenDecimals: 18,
nativeTokenSymbol: "eth",
} as SharedConfigDomain,
)
const formatedFee = getFormatedFee({
amount: "1000000000000000",
tokenAddress: "0x0000000000000000000000000000000000000000",
tokenSymbol: "eth",
transferId: "0x",
resource: { decimals: 18, id: "0x", type: "fungible" },
resourceID: "0x",
id: "0x",
})

expect(formatedFee).toEqual("0.001 ETH")
})
it("parses PHA transfer fee and return fee with native token", () => {
const formatedFee = getFormatedFee(
{
amount: "100000000000",
tokenAddress: '{"Concrete":{"parents":"0","interior":"Here"}}',
tokenSymbol: "PHA",
},
{
nativeTokenDecimals: 12,
nativeTokenSymbol: "PHA",
} as SharedConfigDomain,
)
const formatedFee = getFormatedFee({
amount: "100000000000",
tokenAddress: '{"Concrete":{"parents":"0","interior":"Here"}}',
tokenSymbol: "PHA",
transferId: "0x",
resource: { decimals: 6, id: "0x", type: "fungible" },
resourceID: "0x",
id: "0x",
})

expect(formatedFee).toEqual("0.1 PHA")
})
Expand Down
16 changes: 7 additions & 9 deletions src/utils/Helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,15 @@ export const formatDistanceDate = (timestamp: string): string => {
}
}

export const getFormatedFee = (fee: Transfer["fee"] | string, domain: SharedConfigDomain): string => {
export const getFormatedFee = (fee: Transfer["fee"]): string => {
let formatedFee = "No fee"
const { type } = domain

if (type === DomainTypes.SUBSTRATE) {
formatedFee = "50 PHA"
}

if (typeof fee !== "string" && domain) {
const { nativeTokenDecimals, nativeTokenSymbol } = domain
formatedFee = `${ethers.formatUnits(fee.amount, nativeTokenDecimals).toString()} ${nativeTokenSymbol.toUpperCase()}`
if (fee && Object.keys(fee).length) {
const {
resource: { decimals },
tokenSymbol,
} = fee
formatedFee = `${ethers.formatUnits(fee.amount, decimals !== 0 ? decimals : 18).toString()} ${tokenSymbol.toUpperCase()}`
}

return formatedFee
Expand Down

0 comments on commit 723978c

Please sign in to comment.