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-86a5wajzg-BS Lib - Implement Swap Log #119

Merged
merged 1 commit into from
Dec 13, 2024
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/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"
}
5 changes: 3 additions & 2 deletions packages/blockchain-service/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,16 @@ export type SwapServiceEvents<BSName extends string = string> = {
}

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 {
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.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),
})
)
})
})
29 changes: 29 additions & 0 deletions packages/bs-swap/src/__tests__/SimpleSwapService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
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,
}
}
}
62 changes: 36 additions & 26 deletions packages/bs-swap/src/services/SimpleSwapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,34 +270,44 @@ 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(
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<string> {
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,6 +45,7 @@ export type SimpleSwapApiCreateExchangeResponse = {
result: {
id: string
addressFrom: string
log?: string
}
}

Expand All @@ -53,5 +54,6 @@ export type SimpleSwapApiGetExchangeResponse = {
status: string
txFrom?: string
txTo?: string
log?: string
}
}
Loading