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

CU-86a6647tx-BS Lib - Swap - Round up the minimum with decimals and t… #142

Merged
merged 1 commit into from
Jan 14, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-swap",
"comment": "Add round up to min and apply decimals correctly",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-swap"
}
2 changes: 1 addition & 1 deletion packages/bs-swap/src/__tests__/SimpleSwapApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('SimpleSwapApi', () => {
name: 'Gas',
imageUrl: 'https://static.simpleswap.io/images/currencies-logo/gasn3.svg',
hash: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
decimals: undefined,
decimals: 8,
hasExtraId: false,
validationExtra: null,
validationAddress: '^(N)[A-Za-z0-9]{33}$',
Expand Down
43 changes: 43 additions & 0 deletions packages/bs-swap/src/__tests__/SimpleSwapService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,49 @@ describe('SimpleSwapService', () => {
expect(amountToUseMinMax).toEqual({ loading: false, value: expect.objectContaining({ min: expect.any(String) }) })
}, 10000)

it('Should be able to set the correct min and max amount with Gas (8 decimals)', async () => {
await simpleSwapService.init()

const gasToken = availableTokensToUse.value!.find(({ id }) => id === 'gasn3:neo3')!

await simpleSwapService.setTokenToUse(gasToken)

const account = blockchainServicesByName.neo3.generateAccountFromKey(process.env.TEST_PRIVATE_KEY)

await simpleSwapService.setAccountToUse(account)

await simpleSwapService.setTokenToReceive(availableTokensToReceive.value![0])

const min = amountToUseMinMax.value!.min

expect(amountToUseMinMax).toEqual({
loading: false,
value: expect.objectContaining({ min: expect.any(String), max: null }),
})
expect(min).toContain('.')
expect(min.split('.').at(1)!.length).toBe(8)
}, 10000)

it('Should be able to set the correct min and max amount with Neo (0 decimals)', async () => {
await simpleSwapService.init()

const neoToken = availableTokensToUse.value!.find(({ id }) => id === 'neo3:neo3')!

await simpleSwapService.setTokenToUse(neoToken)

const account = blockchainServicesByName.neo3.generateAccountFromKey(process.env.TEST_PRIVATE_KEY)

await simpleSwapService.setAccountToUse(account)

await simpleSwapService.setTokenToReceive(availableTokensToReceive.value![0])

expect(amountToUseMinMax).toEqual({
loading: false,
value: expect.objectContaining({ min: expect.any(String), max: null }),
})
expect(amountToUseMinMax.value!.min).not.toContain('.')
}, 10000)

it('Should be able to set an invalid address', async () => {
await simpleSwapService.init()
const tokenUse = availableTokensToUse.value![0]
Expand Down
24 changes: 11 additions & 13 deletions packages/bs-swap/src/services/SimpleSwapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,27 +240,26 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe
let range: SwapServiceMinMaxAmount | null = this.#amountToUseMinMax.value
try {
if ((shouldRecalculateAmountToUseMinMax || range === null) && this.#tokenToReceive.value) {
const apiRange = await this.#api.getRange(this.#tokenToUse.value, this.#tokenToReceive.value)
const { decimals } = this.#tokenToUse.value
const rangeResponse = await this.#api.getRange(this.#tokenToUse.value, this.#tokenToReceive.value)

// Add 1% because the SimpleSwap sends us a smaller minimum
const rangeMin = (+apiRange.min * 1.01).toString()
// Add 1% because the SimpleSwap sends us a smaller minimum than the required
const minWithOnePercent = formatNumber((Number(rangeResponse.min) * 1.01).toString(), decimals)

// Add the smallest number to round up because the SimpleSwap doesn't have the decimals, and we need to apply the decimals here
const smallestNumberToRoundUp = decimals ? `0.${'1'.padStart(decimals, '0')}` : '1'

range = {
min: this.#tokenToUse.value.decimals ? formatNumber(rangeMin, this.#tokenToUse.value.decimals) : rangeMin,
max:
this.#tokenToUse.value.decimals && apiRange.max
? formatNumber(apiRange.max, this.#tokenToUse.value.decimals)
: apiRange.max,
min: (Number(minWithOnePercent) + Number(smallestNumberToRoundUp)).toString(),
max: rangeResponse.max ? formatNumber(rangeResponse.max, decimals) : rangeResponse.max,
}
}

this.#amountToUseMinMax = { value: range }

if (shouldRecalculateAmountToUse && range) {
this.#amountToUse = {
value: this.#tokenToUse.value.decimals
? formatNumber(range.min, this.#tokenToUse.value.decimals)
: range.min,
value: range.min ? formatNumber(range.min, this.#tokenToUse.value.decimals) : range.min,
}
}
} catch (error: any) {
Expand Down Expand Up @@ -361,8 +360,7 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe

async setAmountToUse(amount: string | null): Promise<void> {
this.#amountToUse = {
value:
this.#tokenToUse.value?.decimals && amount ? formatNumber(amount, this.#tokenToUse.value.decimals) : amount,
value: this.#tokenToUse.value && amount ? formatNumber(amount, this.#tokenToUse.value.decimals) : amount,
}

debounce(this.#recalculateValues.bind(this), 1000)(['amountToReceive'])
Expand Down
Loading