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-86drvz6jc Swap Multi Invoke - Implement routes definitions to Swap… #91

Merged
merged 1 commit into from
Aug 21, 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": "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
Loading