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-86a65zwt2-BS Lib - Swap - Extra ID implementation #141

Merged
merged 1 commit into from
Jan 13, 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
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish all Blockchain Services Libs (Blockchain-Service, BS-Asteroid-sdk, BS-Ethereum, BS-Neo-Legacy, BS-Neo3)
name: Publish all Blockchain Services Libs (Blockchain-Service, BS-Asteroid-sdk, BS-Ethereum, BS-Neo-Legacy, BS-Neo3, BS-Swap)

on: workflow_dispatch

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/blockchain-service",
"comment": "Add types for extraIdToReceive and setExtraIdToReceive",
"type": "minor"
}
],
"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 extraId feature",
"type": "minor"
}
],
"packageName": "@cityofzion/bs-swap"
}
4 changes: 3 additions & 1 deletion packages/blockchain-service/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export type SwapServiceToken<BSName extends string = string> = {
addressTemplateUrl?: string
txTemplateUrl?: string
network?: string
hasExtraId: boolean
}

export type SwapServiceLoadableValue<T> = { loading: boolean; value: T | null }
Expand All @@ -277,8 +278,8 @@ export type SwapServiceEvents<BSName extends string = string> = {
amountToUseMinMax: (minMax: SwapServiceLoadableValue<SwapServiceMinMaxAmount>) => void | Promise<void>
tokenToUse: (token: SwapServiceLoadableValue<SwapServiceToken<BSName>>) => void | Promise<void>
availableTokensToUse: (tokens: SwapServiceLoadableValue<SwapServiceToken<BSName>[]>) => void | Promise<void>

addressToReceive: (account: SwapServiceValidateValue<string>) => void | Promise<void>
extraIdToReceive: (extraIdToReceive: SwapServiceValidateValue<string>) => void
amountToReceive: (amount: SwapServiceLoadableValue<string>) => void | Promise<void>
tokenToReceive: (token: SwapServiceLoadableValue<SwapServiceToken<BSName>>) => void | Promise<void>
availableTokensToReceive: (tokens: SwapServiceLoadableValue<SwapServiceToken<BSName>[]>) => void | Promise<void>
Expand Down Expand Up @@ -310,6 +311,7 @@ export interface SwapService<BSName extends string = string> {
setAmountToUse(amount: string | null): Promise<void>
setTokenToReceive(token: SwapServiceToken<BSName> | null): Promise<void>
setAddressToReceive(address: string | null): Promise<void>
setExtraIdToReceive(extraId: string | null): Promise<void>
swap(): Promise<SwapServiceSwapResult>
calculateFee(): Promise<string>
}
90 changes: 87 additions & 3 deletions packages/bs-swap/src/__tests__/SimpleSwapApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe('SimpleSwapApi', () => {
imageUrl: 'https://static.simpleswap.io/images/currencies-logo/gasn3.svg',
hash: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
decimals: undefined,
hasExtraId: false,
validationExtra: null,
validationAddress: '^(N)[A-Za-z0-9]{33}$',
addressTemplateUrl: 'https://dora.coz.io/address/neo3/mainnet/{address}',
txTemplateUrl: 'https://dora.coz.io/transaction/neo3/mainnet/{txId}',
Expand All @@ -28,15 +30,97 @@ describe('SimpleSwapApi', () => {
imageUrl: 'https://static.simpleswap.io/images/currencies-logo/neo3.svg',
hash: 'ef4073a0f2b305a38ec4050e4d3d28bc40ea63f5',
decimals: 0,
hasExtraId: false,
validationExtra: null,
validationAddress: '^(N)[A-Za-z0-9]{33}$',
addressTemplateUrl: 'https://dora.coz.io/address/neo3/mainnet/{address}',
txTemplateUrl: 'https://dora.coz.io/transaction/neo3/mainnet/{txId}',
blockchain: 'neo3',
}

const xrpCurrency: SimpleSwapApiCurrency = {
id: 'xrp:xrp',
ticker: 'xrp',
symbol: 'XRP',
network: 'xrp',
name: 'XRP',
imageUrl: 'https://static.simpleswap.io/images/currencies-logo/xrp.svg',
hasExtraId: true,
validationExtra: '^r[1-9A-HJ-NP-Za-km-z]{25,34}$',
validationAddress: '^((?!0)[0-9]{1,10})$',
addressTemplateUrl: 'https://xrpscan.com/account/{address}',
txTemplateUrl: 'https://xrpscan.com/tx/{txId}',
}

const notcoinCurrency: SimpleSwapApiCurrency = {
id: 'not:ton',
ticker: 'not',
symbol: 'NOT',
network: 'ton',
name: 'Notcoin',
imageUrl: 'https://static.simpleswap.io/images/currencies-logo/not.svg',
hash: 'EQAvlWFDxGF2lXm67y4yzC17wYKD9A0guwPkMs1gOsM__NOT',
hasExtraId: true,
validationExtra: '^[0-9A-Za-z\\-_]{1,120}$',
validationAddress: '^[UE][Qf][0-9a-z-A-Z\\-\\_]{46}$',
addressTemplateUrl: 'https://tonscan.org/address/{address}',
txTemplateUrl: 'https://tonscan.org/tx/{txId}',
}

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)
const address = process.env.TEST_ADDRESS_TO_SWAP_TOKEN
const result = await simpleSwapApi.createExchange({
currencyFrom: gasCurrency,
currencyTo: neoCurrency,
amount: '89',
refundAddress: address,
address,
extraIdToReceive: null,
})

expect(result).toEqual(
expect.objectContaining({
id: expect.any(String),
depositAddress: expect.any(String),
log: expect.any(String),
})
)
}, 10000)

it.skip('Should create the exchange to XRP with extraIdToReceive', async () => {
const addressFrom = process.env.TEST_ADDRESS_TO_SWAP_TOKEN
const addressTo = process.env.TEST_XRP_ADDRESS_TO_SWAP_TOKEN
const extraIdToReceive = process.env.TEST_XRP_EXTRA_ID_TO_SWAP_TOKEN
const result = await simpleSwapApi.createExchange({
currencyFrom: gasCurrency,
currencyTo: xrpCurrency,
amount: '89',
refundAddress: addressFrom,
address: addressTo,
extraIdToReceive,
})

expect(result).toEqual(
expect.objectContaining({
id: expect.any(String),
depositAddress: expect.any(String),
log: expect.any(String),
})
)
}, 10000)

it.skip('Should create the exchange to Notcoin with extraIdToReceive', async () => {
const addressFrom = process.env.TEST_ADDRESS_TO_SWAP_TOKEN
const addressTo = process.env.TEST_NOTCOIN_ADDRESS_TO_SWAP_TOKEN
const extraIdToReceive = process.env.TEST_NOTCOIN_EXTRA_ID_TO_SWAP_TOKEN
const result = await simpleSwapApi.createExchange({
currencyFrom: gasCurrency,
currencyTo: notcoinCurrency,
amount: '89',
refundAddress: addressFrom,
address: addressTo,
extraIdToReceive,
})

expect(result).toEqual(
expect.objectContaining({
Expand All @@ -48,7 +132,7 @@ describe('SimpleSwapApi', () => {
}, 10000)

it('Should get the exchange by swap id', async () => {
const result = await simpleSwapApi.getExchange(process.env.TEST_SWAP_ID as string)
const result = await simpleSwapApi.getExchange(process.env.TEST_SWAP_ID)

expect(result).toEqual(
expect.objectContaining({
Expand Down
Loading
Loading