-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcomposite.swap.ts
55 lines (45 loc) · 1.99 KB
/
composite.swap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { DfTxIndexer, DfTxTransaction } from './_abstract'
import { CCompositeSwap, CompositeSwap, PoolId } from '@defichain/jellyfish-transaction'
import { RawBlock } from '../_abstract'
import { Inject, Injectable } from '@nestjs/common'
import { NetworkName } from '@defichain/jellyfish-network'
import BigNumber from 'bignumber.js'
import { PoolPairPathMapping } from './pool.pair.path.mapping'
import { PoolSwapIndexer } from './pool.swap'
@Injectable()
export class CompositeSwapIndexer extends DfTxIndexer<CompositeSwap> {
OP_CODE: number = CCompositeSwap.OP_CODE
constructor (
private readonly poolSwapIndexer: PoolSwapIndexer,
private readonly poolPairPathMapping: PoolPairPathMapping,
@Inject('NETWORK') protected readonly network: NetworkName
) {
super()
}
async indexTransaction (block: RawBlock, transaction: DfTxTransaction<CompositeSwap>): Promise<void> {
const data = transaction.dftx.data
const poolSwap = data.poolSwap
const poolIds = await this.getPoolIdsForTokens(data)
const fromAmount: BigNumber = poolSwap.fromAmount
for (const pool of poolIds) {
await this.poolSwapIndexer.indexSwap(block, transaction, `${pool.id}`, poolSwap.fromTokenId, fromAmount)
}
}
async invalidateTransaction (_: RawBlock, transaction: DfTxTransaction<CompositeSwap>): Promise<void> {
const data = transaction.dftx.data
const poolSwap = data.poolSwap
const poolIds = await this.getPoolIdsForTokens(data)
const fromAmount: BigNumber = poolSwap.fromAmount
for (const pool of poolIds) {
await this.poolSwapIndexer.invalidateSwap(transaction, `${pool.id}`, poolSwap.fromTokenId, fromAmount)
}
}
async getPoolIdsForTokens (compositeSwap: CompositeSwap): Promise<PoolId[]> {
if (compositeSwap.pools.length > 0) {
return compositeSwap.pools
}
const poolSwap = compositeSwap.poolSwap
const pair = await this.poolPairPathMapping.findPair(poolSwap.fromTokenId, poolSwap.toTokenId)
return [{ id: Number(pair.id) }]
}
}