Skip to content

Commit

Permalink
CU-86a5wajzg-BS Lib - Implement Swap Log
Browse files Browse the repository at this point in the history
  • Loading branch information
hotequil committed Dec 12, 2024
1 parent 74c0d16 commit f37cba0
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/blockchain-service",
"comment": "Add log in swap response",
"type": "patch"
}
],
"packageName": "@cityofzion/blockchain-service"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-swap",
"comment": "Add log in swap response",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-swap"
}
2 changes: 2 additions & 0 deletions packages/blockchain-service/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,12 @@ export type SwapServiceSwapResult = {
transactionHash: string
numberOfTransactions: number
id: string
log: string
}

export type SwapServiceStatusResponse = {
status: 'finished' | 'confirming' | 'exchanging' | 'failed' | 'refunded'
log: string
txFrom?: string
txTo?: string
}
Expand Down
58 changes: 58 additions & 0 deletions packages/bs-swap/src/__tests__/SimpleSwapApi.spec.ts
Original file line number Diff line number Diff line change
@@ -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('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),
})
)
})
})
18 changes: 18 additions & 0 deletions packages/bs-swap/src/__tests__/SimpleSwapServiceHelper.spec.ts
Original file line number Diff line number Diff line change
@@ -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),
})
)
})
})
20 changes: 13 additions & 7 deletions packages/bs-swap/src/apis/SimpleSwapApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ export class SimpleSwapApi<BSName extends string = string> {
address: string,
refundAddress: string
) {
const response = await this.#axios.post<SimpleSwapApiCreateExchangeResponse>('/exchanges', {
const {
data: { result },
} = await this.#axios.post<SimpleSwapApiCreateExchangeResponse>('/exchanges', {
tickerFrom: currencyFrom.ticker,
tickerTo: currencyTo.ticker,
networkFrom: currencyFrom.network,
Expand All @@ -151,18 +153,22 @@ export class SimpleSwapApi<BSName extends string = string> {
})

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<SimpleSwapApiGetExchangeResponse>(`/exchanges/${id}`)
const {
data: { result },
} = await this.#axios.get<SimpleSwapApiGetExchangeResponse>(`/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),
}
}
}
1 change: 1 addition & 0 deletions packages/bs-swap/src/helpers/SimpleSwapServiceHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class SimpleSwapServiceHelper<BSName extends string = string> implements
status,
txFrom: response.txFrom,
txTo: response.txTo,
log: response.log,
}
}
}
3 changes: 2 additions & 1 deletion packages/bs-swap/src/services/SimpleSwapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe
throw new Error('Not all required fields are set')
}

const { depositAddress, id } = await this.#api.createExchange(
const { depositAddress, id, log } = await this.#api.createExchange(
this.#tokenToReceive.value,
this.#tokenToUse.value,
this.#amountToUse.value,
Expand All @@ -297,6 +297,7 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe
// SimpleSwap always make 2 transactions
numberOfTransactions: 2,
transactionHash: transactionHash,
log,
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/bs-swap/src/types/simpleSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ export type SimpleSwapApiCreateExchangeResponse = {
result: {
id: string
addressFrom: string
log: string
}
}

export type SimpleSwapApiGetExchangeResponse = {
result: {
log: string
status: string
txFrom?: string
txTo?: string
Expand Down

0 comments on commit f37cba0

Please sign in to comment.