Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions ui/cypress/e2e/liquidity.cy.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
describe('Liquidity', () => {
beforeEach(() => {
cy.visit('/liquidity')

cy.intercept("GET", "https://api.coingecko.com/api/v3/simple/price?ids=nation3,ethereum&vs_currencies=usd", {
body: {
"ethereum": {
"usd": 2672.05
cy.intercept(
'GET',
'https://api.coingecko.com/api/v3/simple/price?ids=nation3,ethereum&vs_currencies=usd',
{
body: {
ethereum: {
usd: 2672.05,
},
nation3: {
usd: 30.75,
},
},
"nation3": {
"usd": 30.75
}
},
});
});
)

it('stake: type a deposit amount', () => {
cy.visit('/liquidity')
})

it('stake: type a deposit amount', () => {
// Expect default value to be '0'
cy.get('#depositValue')
.invoke('val')
.then(val => {
.then((val) => {
expect(val).to.equal('0')
})

// Clear the default '0' value before typing a number
cy.get('#depositValue').clear()

// Type a number
cy.get('#depositValue').type('3.3333')

// Expect the value to no longer be '0'
cy.get('#depositValue')
.invoke('val')
.then(val => {
.then((val) => {
expect(val).to.equal('3.3333')
})
})

it('unstake: type a withdrawal amount', () => {

// Click the "Unstake" tab
cy.get("#unstakeTab").click()
cy.get('#unstakeTab').click()

// Expect default value to be '0'
cy.get('#withdrawalValue')
.invoke('val')
.then(val => {
.then((val) => {
expect(val).to.equal('0')
})

// Clear the default '0' value before typing a number
cy.get('#withdrawalValue').clear()

// Type a number
cy.get('#withdrawalValue').type('0.3333')

// Expect the value to no longer be '0'
cy.get('#withdrawalValue')
.invoke('val')
.then(val => {
.then((val) => {
expect(val).to.equal('0.3333')
})
})
Expand Down
33 changes: 19 additions & 14 deletions ui/lib/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,45 @@ import { BigNumber } from 'ethers'
import { NumberType, transformNumber, isFixedDecimalsNumber } from './numbers'

describe('transformNumber', () => {
test("transformNumber to Number", () => {
test('transformNumber to Number', () => {
const actual = transformNumber(333, NumberType.number)
expect(actual).toBe(Number(333))
})
test("transformNumber to BigNumber", () => {

test('transformNumber to BigNumber', () => {
const actual = transformNumber(333, NumberType.bignumber)
expect(actual).toBeInstanceOf(BigNumber)
})

test("transformNumber to String", () => {

test('transformNumber to BigNumber handles long floating point decimals', () => {
const actual = transformNumber(1 / 3, NumberType.bignumber)
expect(actual).toBeInstanceOf(BigNumber)
})

test('transformNumber to String', () => {
const actual = transformNumber(333, NumberType.string)
expect(actual).toBe("333.000000000000000000")
expect(actual).toBe('333.000000000000000000')
})
test("transformNumber to String (2 decimals)", () => {

test('transformNumber to String (2 decimals)', () => {
const actual = transformNumber(333, NumberType.string, 2)
expect(actual).toBe("333.00")
expect(actual).toBe('333.00')
})
})

describe('isFixedDecimalsNumber', () => {
test('Is working for integers', () => {
const isValid = isFixedDecimalsNumber(12345);
const isValid = isFixedDecimalsNumber(12345)
expect(isValid).toBe(true)
})

test('Fails for too long decimals', () => {
const isValid = isFixedDecimalsNumber('1.111111111111111111111111111111');
const isValid = isFixedDecimalsNumber('1.111111111111111111111111111111')
expect(isValid).toBe(false)
})

test('Check "decimals" param is working properly', ()=> {
const isValid = isFixedDecimalsNumber(1.12345, 2);
test('Check "decimals" param is working properly', () => {
const isValid = isFixedDecimalsNumber(1.12345, 2)
expect(isValid).toBe(false)
})
})
})
12 changes: 6 additions & 6 deletions ui/lib/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export enum NumberType {
export function transformNumber(
num: number | BigNumber | string,
to: NumberType,
decimals = 18
decimals = 18,
): BigNumber | string | number {
if (!num) {
return to === NumberType.bignumber ? ethers.BigNumber.from('0') : 0
Expand All @@ -23,8 +23,8 @@ export function transformNumber(
if (num instanceof ethers.BigNumber) return num

return ethers.utils.parseUnits(
typeof num === 'string' ? num : num.toString(),
decimals
typeof num === 'string' ? num : num.toFixed(decimals),
decimals,
)
} else if (to === NumberType.number) {
if (typeof num === 'number') return num
Expand All @@ -40,7 +40,7 @@ export function transformNumber(
if (num instanceof ethers.BigNumber) {
return stringToNumber(
ethers.utils.formatUnits(num, 18),
decimals
decimals,
).toString()
} else if (typeof num === 'number') {
return num.toFixed(decimals).toString()
Expand All @@ -52,6 +52,6 @@ export function transformNumber(
export function isFixedDecimalsNumber(value: any, decimals = 18) {
const NUMBER_REGEX = RegExp(`^(\\d*\\.{0,1}\\d{0,${decimals}}$)`)
const isValid = value.toString().match(NUMBER_REGEX)
return Boolean(isValid);

return Boolean(isValid)
}
Loading
Loading