From cfab9e801b169f0ac7fefb3d44a5403de62941a1 Mon Sep 17 00:00:00 2001 From: hotequil Date: Thu, 12 Dec 2024 14:45:01 -0300 Subject: [PATCH] CU-86a5wajzg-BS Lib - Implement Swap Log --- .../CU-86a5wajzg_2024-12-12-17-38.json | 10 +++ .../CU-86a5wajzg_2024-12-12-17-38.json | 10 +++ packages/blockchain-service/src/interfaces.ts | 5 +- .../src/__tests__/SimpleSwapApi.spec.ts | 58 +++++++++++++++++ .../src/__tests__/SimpleSwapService.spec.ts | 29 +++++++++ .../__tests__/SimpleSwapServiceHelper.spec.ts | 18 ++++++ packages/bs-swap/src/apis/SimpleSwapApi.ts | 20 +++--- .../src/helpers/SimpleSwapServiceHelper.ts | 1 + .../bs-swap/src/services/SimpleSwapService.ts | 62 +++++++++++-------- packages/bs-swap/src/types/simpleSwap.ts | 2 + 10 files changed, 180 insertions(+), 35 deletions(-) create mode 100644 common/changes/@cityofzion/blockchain-service/CU-86a5wajzg_2024-12-12-17-38.json create mode 100644 common/changes/@cityofzion/bs-swap/CU-86a5wajzg_2024-12-12-17-38.json create mode 100644 packages/bs-swap/src/__tests__/SimpleSwapApi.spec.ts create mode 100644 packages/bs-swap/src/__tests__/SimpleSwapServiceHelper.spec.ts diff --git a/common/changes/@cityofzion/blockchain-service/CU-86a5wajzg_2024-12-12-17-38.json b/common/changes/@cityofzion/blockchain-service/CU-86a5wajzg_2024-12-12-17-38.json new file mode 100644 index 0000000..640da60 --- /dev/null +++ b/common/changes/@cityofzion/blockchain-service/CU-86a5wajzg_2024-12-12-17-38.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/blockchain-service", + "comment": "Add log in swap response", + "type": "patch" + } + ], + "packageName": "@cityofzion/blockchain-service" +} \ No newline at end of file diff --git a/common/changes/@cityofzion/bs-swap/CU-86a5wajzg_2024-12-12-17-38.json b/common/changes/@cityofzion/bs-swap/CU-86a5wajzg_2024-12-12-17-38.json new file mode 100644 index 0000000..cf4e4c6 --- /dev/null +++ b/common/changes/@cityofzion/bs-swap/CU-86a5wajzg_2024-12-12-17-38.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-swap", + "comment": "Add log in swap response", + "type": "patch" + } + ], + "packageName": "@cityofzion/bs-swap" +} \ No newline at end of file diff --git a/packages/blockchain-service/src/interfaces.ts b/packages/blockchain-service/src/interfaces.ts index b9ecb61..571123b 100644 --- a/packages/blockchain-service/src/interfaces.ts +++ b/packages/blockchain-service/src/interfaces.ts @@ -280,15 +280,16 @@ export type SwapServiceEvents = { } export type SwapServiceSwapResult = { - transactionHash: string - numberOfTransactions: number id: string + txFrom?: string + log?: string } export type SwapServiceStatusResponse = { status: 'finished' | 'confirming' | 'exchanging' | 'failed' | 'refunded' txFrom?: string txTo?: string + log?: string } export interface SwapServiceHelper { diff --git a/packages/bs-swap/src/__tests__/SimpleSwapApi.spec.ts b/packages/bs-swap/src/__tests__/SimpleSwapApi.spec.ts new file mode 100644 index 0000000..2031169 --- /dev/null +++ b/packages/bs-swap/src/__tests__/SimpleSwapApi.spec.ts @@ -0,0 +1,58 @@ +import { SimpleSwapApi } from '../apis/SimpleSwapApi' +import { SimpleSwapApiCurrency } from '../types/simpleSwap' + +describe('SimpleSwapApi', () => { + const simpleSwapApi = new SimpleSwapApi() + + const gasCurrency: SimpleSwapApiCurrency<'neo3'> = { + id: 'gasn3:neo3', + ticker: 'gasn3', + symbol: 'gasn3', + network: 'neo3', + name: 'Gas', + imageUrl: 'https://static.simpleswap.io/images/currencies-logo/gasn3.svg', + hash: '0xd2a4cff31913016155e38e474a2c06d08be276cf', + decimals: undefined, + validationAddress: '^(N)[A-Za-z0-9]{33}$', + blockchain: 'neo3', + } + + const neoCurrency: SimpleSwapApiCurrency<'neo3'> = { + id: 'neo3:neo3', + ticker: 'neo3', + symbol: 'NEO', + network: 'neo3', + name: 'NEO', + imageUrl: 'https://static.simpleswap.io/images/currencies-logo/neo3.svg', + hash: 'ef4073a0f2b305a38ec4050e4d3d28bc40ea63f5', + decimals: 0, + validationAddress: '^(N)[A-Za-z0-9]{33}$', + blockchain: 'neo3', + } + + it.skip('Should create the exchange with params', async () => { + const address = process.env.TEST_ADDRESS_TO_SWAP_TOKEN as string + const result = await simpleSwapApi.createExchange(gasCurrency, neoCurrency, '1000', address, address) + + expect(result).toEqual( + expect.objectContaining({ + id: expect.any(String), + depositAddress: expect.any(String), + log: expect.any(String), + }) + ) + }) + + it('Should get the exchange by swap id', async () => { + const result = await simpleSwapApi.getExchange(process.env.TEST_SWAP_ID as string) + + expect(result).toEqual( + expect.objectContaining({ + status: expect.any(String), + txFrom: null, + txTo: null, + log: expect.any(String), + }) + ) + }) +}) diff --git a/packages/bs-swap/src/__tests__/SimpleSwapService.spec.ts b/packages/bs-swap/src/__tests__/SimpleSwapService.spec.ts index cdc1445..1dd8612 100644 --- a/packages/bs-swap/src/__tests__/SimpleSwapService.spec.ts +++ b/packages/bs-swap/src/__tests__/SimpleSwapService.spec.ts @@ -328,4 +328,33 @@ describe('SimpleSwapService', () => { expect(addressToReceive).toEqual({ loading: false, value: account.address, valid: true }) expect(amountToUseMinMax).toEqual({ loading: false, value: expect.objectContaining({ min: expect.any(String) }) }) }, 20000) + + it.skip('Should create a swap when all fields are filled', async () => { + await simpleSwapService.init() + + const tokenUse = availableTokensToUse.value![1] + const tokenReceive = availableTokensToUse.value![0] + + await simpleSwapService.setTokenToUse(tokenUse) + + const account = blockchainServicesByName.neo3.generateAccountFromKey( + process.env.TEST_PRIVATE_KEY_TO_SWAP_TOKEN as string + ) + + await simpleSwapService.setAccountToUse(account) + await simpleSwapService.setAmountToUse('1000') + + await simpleSwapService.setTokenToReceive(tokenReceive) + await simpleSwapService.setAddressToReceive(account.address) + + const result = await simpleSwapService.swap() + + expect(result).toEqual( + expect.objectContaining({ + id: expect.any(String), + txFrom: undefined, + log: expect.any(String), + }) + ) + }, 20000) }) diff --git a/packages/bs-swap/src/__tests__/SimpleSwapServiceHelper.spec.ts b/packages/bs-swap/src/__tests__/SimpleSwapServiceHelper.spec.ts new file mode 100644 index 0000000..bbf986d --- /dev/null +++ b/packages/bs-swap/src/__tests__/SimpleSwapServiceHelper.spec.ts @@ -0,0 +1,18 @@ +import { SimpleSwapServiceHelper } from '../helpers/SimpleSwapServiceHelper' + +describe('SimpleSwapServiceHelper', () => { + const simpleSwapServiceHelper = new SimpleSwapServiceHelper() + + it('Should get the swap status by swap id', async () => { + const result = await simpleSwapServiceHelper.getStatus(process.env.TEST_SWAP_ID as string) + + expect(result).toEqual( + expect.objectContaining({ + status: expect.any(String), + txFrom: null, + txTo: null, + log: expect.any(String), + }) + ) + }) +}) diff --git a/packages/bs-swap/src/apis/SimpleSwapApi.ts b/packages/bs-swap/src/apis/SimpleSwapApi.ts index 3694364..be301e8 100644 --- a/packages/bs-swap/src/apis/SimpleSwapApi.ts +++ b/packages/bs-swap/src/apis/SimpleSwapApi.ts @@ -140,7 +140,9 @@ export class SimpleSwapApi { address: string, refundAddress: string ) { - const response = await this.#axios.post('/exchanges', { + const { + data: { result }, + } = await this.#axios.post('/exchanges', { tickerFrom: currencyFrom.ticker, tickerTo: currencyTo.ticker, networkFrom: currencyFrom.network, @@ -151,18 +153,22 @@ export class SimpleSwapApi { }) return { - id: response.data.result.id, - depositAddress: response.data.result.addressFrom, + id: result.id, + depositAddress: result.addressFrom, + log: JSON.stringify(result), } } async getExchange(id: string) { - const response = await this.#axios.get(`/exchanges/${id}`) + const { + data: { result }, + } = await this.#axios.get(`/exchanges/${id}`) return { - status: response.data.result.status, - txFrom: response.data.result.txFrom, - txTo: response.data.result.txTo, + status: result.status, + txFrom: result.txFrom, + txTo: result.txTo, + log: JSON.stringify(result), } } } diff --git a/packages/bs-swap/src/helpers/SimpleSwapServiceHelper.ts b/packages/bs-swap/src/helpers/SimpleSwapServiceHelper.ts index 235b97d..1d050fd 100644 --- a/packages/bs-swap/src/helpers/SimpleSwapServiceHelper.ts +++ b/packages/bs-swap/src/helpers/SimpleSwapServiceHelper.ts @@ -29,6 +29,7 @@ export class SimpleSwapServiceHelper implements status, txFrom: response.txFrom, txTo: response.txTo, + log: response.log, } } } diff --git a/packages/bs-swap/src/services/SimpleSwapService.ts b/packages/bs-swap/src/services/SimpleSwapService.ts index 76e3c10..5f74533 100644 --- a/packages/bs-swap/src/services/SimpleSwapService.ts +++ b/packages/bs-swap/src/services/SimpleSwapService.ts @@ -270,34 +270,44 @@ export class SimpleSwapService implements SwapSe throw new Error('Not all required fields are set') } - const { depositAddress, id } = await this.#api.createExchange( - this.#tokenToReceive.value, - this.#tokenToUse.value, - this.#amountToUse.value, - this.#addressToReceive.value, - this.#accountToUse.value.address - ) - - const service = this.#blockchainServicesByName[this.#accountToUse.value.blockchain] - - const [transactionHash] = await service.transfer({ - senderAccount: this.#accountToUse.value, - intents: [ - { - amount: this.#amountToUse.value, - receiverAddress: depositAddress, - tokenHash: this.#tokenToUse.value.hash, - tokenDecimals: this.#tokenToUse.value.decimals, - }, - ], - }) + const result: SwapServiceSwapResult = { + id: '', + txFrom: undefined, + log: undefined, + } - return { - id, - // SimpleSwap always make 2 transactions - numberOfTransactions: 2, - transactionHash: transactionHash, + try { + const { depositAddress, id, log } = await this.#api.createExchange( + this.#tokenToReceive.value, + this.#tokenToUse.value, + this.#amountToUse.value, + this.#addressToReceive.value, + this.#accountToUse.value.address + ) + + result.id = id + result.log = log + + const service = this.#blockchainServicesByName[this.#accountToUse.value.blockchain] + + const [transactionHash] = await service.transfer({ + senderAccount: this.#accountToUse.value, + intents: [ + { + amount: this.#amountToUse.value, + receiverAddress: depositAddress, + tokenHash: this.#tokenToUse.value.hash, + tokenDecimals: this.#tokenToUse.value.decimals, + }, + ], + }) + + result.txFrom = transactionHash + } catch { + // empty } + + return result } async calculateFee(): Promise { diff --git a/packages/bs-swap/src/types/simpleSwap.ts b/packages/bs-swap/src/types/simpleSwap.ts index f1f6361..0275ab6 100644 --- a/packages/bs-swap/src/types/simpleSwap.ts +++ b/packages/bs-swap/src/types/simpleSwap.ts @@ -45,6 +45,7 @@ export type SimpleSwapApiCreateExchangeResponse = { result: { id: string addressFrom: string + log?: string } } @@ -53,5 +54,6 @@ export type SimpleSwapApiGetExchangeResponse = { status: string txFrom?: string txTo?: string + log?: string } }