Skip to content

Commit

Permalink
feat: hanmdle LP tokens migration
Browse files Browse the repository at this point in the history
  • Loading branch information
filo87 committed Aug 16, 2024
1 parent e6dce28 commit 587730f
Show file tree
Hide file tree
Showing 6 changed files with 1,975 additions and 4,100 deletions.
1 change: 0 additions & 1 deletion chains-cfg/_root.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dataSources:
kind: substrate/BlockHandler
filter:
modulo: 300
#timestamp: '1 0,12 * * *'
# - handler: logEvents
# kind: substrate/EventHandler
- handler: handlePoolCreated
Expand Down
44 changes: 22 additions & 22 deletions chains-evm/eth/centrifuge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ dataSources:
startBlock: 20432390
options:
address: '0x91808B5E2F6d7483D41A681034D7c9DbB64B9E29'
# - kind: ethereum/Runtime #Tinlake
# startBlock: 11063000
# options:
# abi: navFeed
# assets:
# navFeed:
# file: ./abi/navfeed.abi.json
# reserve:
# file: ./abi/reserve.abi.json
# shelf:
# file: ./abi/shelf.abi.json
# pile:
# file: ./abi/pile.abi.json
# multicall:
# file: ./abi/multicall.abi.json
# mapping:
# file: ./dist/index.js
# handlers:
# - handler: handleEthBlock
# kind: ethereum/BlockHandler
# filter:
# modulo: 300
- kind: ethereum/Runtime #Tinlake
startBlock: 11063000
options:
abi: navFeed
assets:
navFeed:
file: ./abi/navfeed.abi.json
reserve:
file: ./abi/reserve.abi.json
shelf:
file: ./abi/shelf.abi.json
pile:
file: ./abi/pile.abi.json
multicall:
file: ./abi/multicall.abi.json
mapping:
file: ./dist/index.js
handlers:
- handler: handleEthBlock
kind: ethereum/BlockHandler
filter:
modulo: 300
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
"@commitlint/cli": "^17.0.2",
"@commitlint/config-conventional": "^17.0.2",
"@jest/globals": "^29.2.0",
"@polkadot/api": "^10",
"@polkadot/typegen": "^10",
"@polkadot/api": "^12",
"@polkadot/typegen": "^12",
"@subql/cli": "latest",
"@subql/common-substrate": "latest",
"@subql/node-ethereum": "latest",
"@subql/testing": "latest",
"@subql/types": "latest",
"@subql/types-core": "latest",
"@types/jest": "^29.1.2",
"@types/node-fetch": "^2.6.11",
"@typescript-eslint/eslint-plugin": "^6.15.0",
Expand All @@ -48,6 +49,7 @@
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.0.1",
"ethers": "^5.7.2",
"husky": "^7.0.0",
"jest": "^29.2.0",
"lint-staged": "^13.2.0",
Expand Down
39 changes: 22 additions & 17 deletions src/mappings/handlers/evmHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise<void> {
await createTrancheTrackerDatasource({ address: tokenAddress })
}
const nullAddress = '0x0000000000000000000000000000000000000000'
const LP_TOKENS_MIGRATION_DATE = '2024-08-07'

export const handleEvmTransfer = errorHandler(_handleEvmTransfer)
async function _handleEvmTransfer(event: TransferLog): Promise<void> {
Expand All @@ -76,13 +77,14 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
const orderData: Omit<InvestorTransactionData, 'address'> = {
poolId: evmToken.poolId,
trancheId: evmToken.trancheId.split('-')[1],
//epochNumber: pool.currentEpoch,
hash: event.transactionHash,
timestamp: new Date(Number(event.block.timestamp) * 1000),
//price: tranche.tokenPrice,
amount: amount.toBigInt(),
}

const isLpTokenMigrationDay =
chainId === '1' && orderData.timestamp.toISOString().startsWith(LP_TOKENS_MIGRATION_DATE)

let fromAddress: string = null,
fromAccount: AccountService = null
if (isFromUserAddress) {
Expand Down Expand Up @@ -127,24 +129,27 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
// Handle Transfer In and Out
if (isFromUserAddress && isToUserAddress) {
const txIn = InvestorTransactionService.transferIn({ ...orderData, address: toAccount.id })
await InvestorPositionService.buy(
txIn.accountId,
txIn.trancheId,
txIn.hash,
txIn.timestamp,
txIn.tokenAmount,
txIn.tokenPrice
)
if (!isLpTokenMigrationDay)
await InvestorPositionService.buy(
txIn.accountId,
txIn.trancheId,
txIn.hash,
txIn.timestamp,
txIn.tokenAmount,
txIn.tokenPrice
)
await txIn.save()

const txOut = InvestorTransactionService.transferOut({ ...orderData, address: fromAccount.id })
const profit = await InvestorPositionService.sellFifo(
txOut.accountId,
txOut.trancheId,
txOut.tokenAmount,
txOut.tokenPrice
)
await txOut.setRealizedProfitFifo(profit)
if (!isLpTokenMigrationDay) {
const profit = await InvestorPositionService.sellFifo(
txOut.accountId,
txOut.trancheId,
txOut.tokenAmount,
txOut.tokenPrice
)
await txOut.setRealizedProfitFifo(profit)
}
await txOut.save()
}
}
4 changes: 4 additions & 0 deletions src/mappings/services/investorPositionService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { InvestorPosition } from '../../types/models/InvestorPosition'
import { WAD } from '../../config'
import { nToBigInt, bnToBn, BN } from '@polkadot/util'
import assert from 'assert'

export class InvestorPositionService extends InvestorPosition {
static init(accountId: string, trancheId: string, hash: string, timestamp: Date, quantity: bigint, price: bigint) {
const [ poolId ] = trancheId.split('-')
assert(quantity, 'Missing quantity')
assert(price, 'Missing price')
assert(hash, 'Missing hash')
const id = `${accountId}-${trancheId}-${hash}`
logger.info(
`Initialising new InvestorPosition with Id ${id} ` +
Expand Down
Loading

0 comments on commit 587730f

Please sign in to comment.