Skip to content

Commit

Permalink
Merge pull request #91 from CityOfZion/CU-86drvz6jc-3
Browse files Browse the repository at this point in the history
CU-86drvz6jc Swap Multi Invoke - Implement routes definitions to Swap…
  • Loading branch information
raulduartep authored Aug 21, 2024
2 parents e26863d + b3abf72 commit a8f5630
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/blockchain-service",
"comment": "Update swap lib",
"type": "patch"
}
],
"packageName": "@cityofzion/blockchain-service"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-neo3",
"comment": "Update swap lib",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-neo3"
}
5 changes: 5 additions & 0 deletions packages/blockchain-service/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BSWithLedger,
BSWithNameService,
BSWithNft,
BSWithSwap,
} from './interfaces'

export function hasNameService(service: BlockchainService): service is BlockchainService & BSWithNameService {
Expand All @@ -32,6 +33,10 @@ export function hasLedger(service: BlockchainService): service is BlockchainServ
return 'ledgerService' in service
}

export function hasSwap(service: BlockchainService): service is BlockchainService & BSWithSwap {
return 'createSwapService' in service
}

function wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
Expand Down
6 changes: 4 additions & 2 deletions packages/blockchain-service/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export interface BSWithLedger {
generateAccountFromPublicKey(publicKey: string): Account
}

export interface BSWithSwap<BSAvailableNetworks extends string = string> {
createSwapService(): SwapService<BSAvailableNetworks>
}

export type TransactionNotifications = {
eventName: string
state: {
Expand Down Expand Up @@ -311,7 +315,5 @@ export interface SwapService<AvailableNetworkIds extends string> {
startListeningBlockGeneration(): void
stopListeningBlockGeneration(): void

listSwappableTokensSymbol(network: Network<AvailableNetworkIds>): string[]

swap(isLedger?: boolean): void
}
10 changes: 9 additions & 1 deletion packages/bs-neo3/src/BSNeo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
BSWithLedger,
BSWithNameService,
BSWithNft,
BSWithSwap,
ExchangeDataService,
ExplorerService,
Network,
NftDataService,
SwapService,
Token,
TransferParam,
} from '@cityofzion/blockchain-service'
Expand All @@ -29,6 +31,7 @@ import { FlamingoEDSNeo3 } from './services/exchange-data/FlamingoEDSNeo3'
import { DoraESNeo3 } from './services/explorer/DoraESNeo3'
import { NeonDappKitLedgerServiceNeo3 } from './services/ledger/NeonDappKitLedgerServiceNeo3'
import { GhostMarketNDSNeo3 } from './services/nft-data/GhostMarketNDSNeo3'
import { FlamingoSwapServiceNeo3 } from './services/swap/FlamingoSwapServiceNeo3'

export class BSNeo3<BSCustomName extends string = string>
implements
Expand All @@ -38,7 +41,8 @@ export class BSNeo3<BSCustomName extends string = string>
BSCalculableFee,
BSWithNft,
BSWithExplorerService,
BSWithLedger
BSWithLedger,
BSWithSwap<BSNeo3NetworkId>
{
blockchainName: BSCustomName
derivationPath: string
Expand Down Expand Up @@ -103,6 +107,10 @@ export class BSNeo3<BSCustomName extends string = string>
return invocations
}

createSwapService(): SwapService<BSNeo3NetworkId> {
return new FlamingoSwapServiceNeo3(this.network, this.ledgerService)
}

setNetwork(network: Network<BSNeo3NetworkId>) {
this.#setTokens(network)
this.network = network
Expand Down
5 changes: 5 additions & 0 deletions packages/bs-neo3/src/helpers/FlamingoSwapHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import {
FlamingoSwapScriptHashes,
FlamingoSwapTokens,
} from '../constants/FlamingoSwapConstants'
import { FlamingoSwapRouteHandler } from '../services/swap/handlers'
import { BSNeo3NetworkId } from './BSNeo3Helper'

export class FlamingoSwapHelper {
static listSwappableTokensSymbol(network: Network<BSNeo3NetworkId>): string[] {
return Object.keys(FlamingoSwapRouteHandler.createPoolGraph(network))
}

static getFlamingoSwapPools(network: Network<BSNeo3NetworkId>): FlamingoSwapPools {
const pools = FlamingoSwapConstants.FLAMINGO_SWAP_POOLS[network.id]
if (!pools) {
Expand Down
17 changes: 10 additions & 7 deletions packages/bs-neo3/src/services/swap/FlamingoSwapServiceNeo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@cityofzion/blockchain-service'
import { wallet } from '@cityofzion/neon-core'
import { NeonInvoker } from '@cityofzion/neon-dappkit'
import { api } from '@cityofzion/neon-js'
import Transport from '@ledgerhq/hw-transport'
import EventEmitter from 'events'
import cloneDeep from 'lodash.clonedeep'
Expand All @@ -28,7 +29,7 @@ type LastAmountChanged = 'amountToReceive' | 'amountToUse' | null
export class FlamingoSwapServiceNeo3 implements SwapService<BSNeo3NetworkId> {
eventEmitter: TypedEmitter<SwapServiceEvents>

#ledgerService!: NeonDappKitLedgerServiceNeo3
#ledgerService?: NeonDappKitLedgerServiceNeo3
#network: Network<BSNeo3NetworkId>
#privateAccountToUse: Account | null = null
#privateTokenToReceive: Token | null = null
Expand All @@ -46,7 +47,7 @@ export class FlamingoSwapServiceNeo3 implements SwapService<BSNeo3NetworkId> {
#privateLastAmountChanged: LastAmountChanged = null
#socket: FlamingoSwapSocketService = new FlamingoSwapSocketService()

constructor(network: Network<BSNeo3NetworkId>, ledgerService: NeonDappKitLedgerServiceNeo3) {
constructor(network: Network<BSNeo3NetworkId>, ledgerService?: NeonDappKitLedgerServiceNeo3) {
this.eventEmitter = new EventEmitter() as TypedEmitter<SwapServiceEvents>
this.#network = network
this.#ledgerService = ledgerService
Expand Down Expand Up @@ -94,29 +95,31 @@ export class FlamingoSwapServiceNeo3 implements SwapService<BSNeo3NetworkId> {
}
}

listSwappableTokensSymbol(network: Network<BSNeo3NetworkId>): string[] {
return Object.keys(FlamingoSwapRouteHandler.createPoolGraph(network))
}

async swap(isLedger?: boolean): Promise<void> {
const swapInvocationArgs = this.buildSwapInvocationArgs()

let ledgerTransport: Transport | undefined
let signingCallback: api.SigningFunction | undefined

if (isLedger) {
if (!this.#ledgerService) {
throw new Error('You must provide a ledger service to use Ledger')
}

if (!this.#ledgerService.getLedgerTransport) {
throw new Error('You must provide a getLedgerTransport function to use Ledger')
}

ledgerTransport = await this.#ledgerService.getLedgerTransport(this.#accountToUse!)
signingCallback = this.#ledgerService.getSigningCallback(ledgerTransport)
}

const account = new wallet.Account(this.#accountToUse!.key)

const invoker = await NeonInvoker.init({
rpcAddress: this.#network.url,
account,
signingCallback: ledgerTransport ? this.#ledgerService.getSigningCallback(ledgerTransport) : undefined,
signingCallback,
})

await invoker.invokeFunction(FlamingoSwapInvocationBuilderNeo3.swapInvocation(swapInvocationArgs))
Expand Down

0 comments on commit a8f5630

Please sign in to comment.