Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

181 filter evm transfers to escrows #185

Merged
merged 2 commits into from
Jan 9, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@types/jest": "^29.1.2",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"abab": "^2.0.6",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.0.1",
Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ type Currency @entity {
decimals: Int!

tokenAddress: String
escrowAddress: String

pool: Pool
tranche: Tranche
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '@polkadot/api-augment'
import { atob } from 'abab'

global.atob = atob

export * from './mappings/handlers/blockHandlers'
export * from './mappings/handlers/poolsHandlers'
Expand Down
27 changes: 20 additions & 7 deletions src/mappings/handlers/evmHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,47 @@ import { InvestorTransactionData, InvestorTransactionService } from '../services
import { CurrencyService } from '../services/currencyService'
import { BlockchainService } from '../services/blockchainService'
import { CurrencyBalanceService } from '../services/currencyBalanceService'
import { PoolManagerAbi__factory } from '../../types/contracts'

export const handleEvmDeployTranche = errorHandler(_handleEvmDeployTranche)
async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise<void> {
const [_poolId, _trancheId, tokenAddress] = event.args

const chainId = parseInt(event.transaction.chainId,16).toString(10)
await BlockchainService.getOrInit(chainId)
const chainId = parseInt(event.transaction.chainId, 16).toString(10)
const blockchain = await BlockchainService.getOrInit(chainId)

const poolId = _poolId.toString()
const trancheId = _trancheId.substring(0, 34)

logger.info(`Adding DynamicSource for pool ${poolId}-${trancheId} token: ${tokenAddress} block: ${event.blockNumber}`)
logger.info(
`Adding DynamicSource for tranche ${poolId}-${trancheId} token: ${tokenAddress} block: ${event.blockNumber}`
)

const pool = await PoolService.getOrSeed(poolId)
await TrancheService.getOrSeed(pool.id, trancheId)
const tranche = await TrancheService.getOrSeed(pool.id, trancheId)

const currency = await CurrencyService.getOrInitEvm(blockchain.id, tokenAddress)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const poolManager = PoolManagerAbi__factory.connect(event.address, api as any)
const escrowAddress = await poolManager.escrow()
await currency.initEvmDetails(tokenAddress, escrowAddress, tranche.poolId, tranche.trancheId)
await currency.save()

await createTrancheTrackerDatasource({ address: tokenAddress })
}

const nullAddress = '0x0000000000000000000000000000000000000000'

export const handleEvmTransfer = errorHandler(_handleEvmTransfer)
async function _handleEvmTransfer(event: TransferLog): Promise<void> {
const [fromEvmAddress, toEvmAddress, amount] = event.args
logger.info(`Transfer ${fromEvmAddress}-${toEvmAddress} of ${amount.toString()} at block: ${event.blockNumber}`)

const evmTokenAddress = event.address
const chainId = parseInt(event.transaction.chainId,16).toString(10)
const chainId = parseInt(event.transaction.chainId, 16).toString(10)
const blockchain = await BlockchainService.getOrInit(chainId)
const evmToken = await CurrencyService.getOrInitEvm(blockchain.id, evmTokenAddress)
const escrowAddress = evmToken.escrowAddress

const orderData: Omit<InvestorTransactionData, 'address'> = {
poolId: evmToken.poolId,
Expand All @@ -48,7 +61,7 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
amount: amount.toBigInt(),
}

if (fromEvmAddress !== evmTokenAddress) {
if (fromEvmAddress !== evmTokenAddress && fromEvmAddress !== escrowAddress && fromEvmAddress !== nullAddress) {
const fromAddress = AccountService.evmToSubstrate(fromEvmAddress, blockchain.id)
const fromAccount = await AccountService.getOrInit(fromAddress)

Expand All @@ -60,7 +73,7 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
await fromBalance.save()
}

if (toEvmAddress !== evmTokenAddress) {
if (toEvmAddress !== evmTokenAddress && toEvmAddress !== escrowAddress && toEvmAddress !== nullAddress) {
const toAddress = AccountService.evmToSubstrate(toEvmAddress, blockchain.id)
const toAccount = await AccountService.getOrInit(toAddress)
const txIn = InvestorTransactionService.transferIn({ ...orderData, address: toAccount.id })
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/accountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class AccountService extends Account {
return `0x${evmAddress.substring(2).toLowerCase()}000000000000${chainHex}${EVM_SUFFIX}`
}

static readEvmChainId(evmAddress: string) {
return parseInt(evmAddress.slice(-12, -8), 16).toString(10)
static readEvmChainId(address: string) {
return parseInt(address.slice(-12, -8), 16).toString(10)
}

static isEvm(address: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/services/borrowerTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class BorrowerTransactionService extends BorrowerTransaction {
const tx = new BorrowerTransactionService(
`${data.hash}-${data.epochNumber.toString()}-${type.toString()}`,
data.timestamp,
data.poolId.toString(),
data.poolId,
data.hash,
data.address,
data.epochNumber,
Expand Down
11 changes: 9 additions & 2 deletions src/mappings/services/currencyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CurrencyService extends Currency {
static async getOrInit(chainId: string, currencyType: string, ...currencyValue: string[]) {
const currencyId = currencyValue.length > 0 ? `${currencyType}-${currencyValue.join('-')}` : currencyType
const id = `${chainId}-${currencyId}`
let currency: CurrencyService = await this.get(id)
let currency: CurrencyService = (await this.get(id)) as CurrencyService
if (!currency) {
const enumPayload = formatEnumPayload(currencyType, ...currencyValue)
const assetMetadata = (await api.query.ormlAssetRegistry.metadata(enumPayload)) as Option<AssetMetadata>
Expand All @@ -29,13 +29,20 @@ export class CurrencyService extends Currency {
static async getOrInitEvm(chainId: string, currencyType: string, ...currencyValue: string[]) {
const currencyId = currencyValue.length > 0 ? `${currencyType}-${currencyValue.join('-')}` : currencyType
const id = `${chainId}-${currencyId}`
let currency: CurrencyService = await this.get(id)
let currency: CurrencyService = (await this.get(id)) as CurrencyService
if (!currency) {
currency = this.init(chainId, currencyId, WAD_DIGITS)
await currency.save()
}
return currency as CurrencyService
}

public initEvmDetails(tokenAddress: string, escrowAddress: string, poolId: string, trancheId: string) {
this.tokenAddress = tokenAddress
this.escrowAddress = escrowAddress
this.poolId = poolId
this.trancheId = `${poolId}-${trancheId}`
}
}

export const currencyFormatters: CurrencyFormatters = {
Expand Down
10 changes: 9 additions & 1 deletion src/mappings/services/investorTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class InvestorTransactionService extends InvestorTransaction {
`${data.hash}-${data.epochNumber.toString()}-${type.toString()}`,
data.hash,
data.address,
data.poolId.toString(),
data.poolId,
`${data.poolId}-${data.trancheId}`,
data.timestamp,
type
Expand Down Expand Up @@ -108,10 +108,18 @@ export class InvestorTransactionService extends InvestorTransaction {
}

static transferIn(data: InvestorTransactionData) {
logger.info(
`Transfer In for address ${data.address} in pool ${data.poolId} tranche ${data.trancheId} ` +
`with amount: ${data.amount}`
)
return this.init(data, InvestorTransactionType.TRANSFER_IN)
}

static transferOut(data: InvestorTransactionData) {
logger.info(
`Transfer Out for address ${data.address} in pool ${data.poolId} tranche ${data.trancheId} ` +
`with amount: ${data.amount}`
)
return this.init(data, InvestorTransactionType.TRANSFER_OUT)
}

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"target": "es2017",
//"strict": true,
"paths": {
"centrifuge-subql/*": ["./src/*"]
//"centrifuge-subql/*": ["./src/*"]
}
},
"include": [
"src/**/*",
"node_modules/@subql/types-core/dist/global.d.ts",
"node_modules/@subql/types/dist/global.d.ts"
"node_modules/@subql/types/dist/global.d.ts",
],
// "exclude": ["src/**/*.test.ts"],
"exports": {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4008,6 +4008,11 @@ JSONStream@^1.3.5:
jsonparse "^1.2.0"
through ">=2.2.7 <3"

abab@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.6.tgz#41b80f2c871d19686216b82309231cfd3cb3d291"
integrity sha512-j2afSsaIENvHZN2B8GOpF566vZ5WVk5opAiMTvWgaQT8DkbOqsTfvNAvHoRGU2zzP8cPoqys+xHTRDWW8L+/BA==

abort-controller@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
Expand Down