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

Commit

Permalink
182 evm transfers not recorded (#184)
Browse files Browse the repository at this point in the history
* fix: evm transfers not recorded
Fixes #182

* fix: evm Transfers not recorded
Fixes #182
  • Loading branch information
filo87 authored Jan 3, 2024
1 parent d7cb34a commit 8924aa7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion chains-evm/eth/centrifuge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ network:
dictionary: "https://gx.api.subquery.network/sq/subquery/eth-dictionary"
dataSources:
- kind: ethereum/Runtime
startBlock: 18671269
startBlock: 18721000
options:
address: '0x78E9e622A57f70F1E0Ec652A4931E4e278e58142'
26 changes: 18 additions & 8 deletions src/mappings/handlers/evmHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TrancheService } from '../services/trancheService'
import { InvestorTransactionData, InvestorTransactionService } from '../services/investorTransactionService'
import { CurrencyService } from '../services/currencyService'
import { BlockchainService } from '../services/blockchainService'
import { CurrencyBalanceService } from '../services/currencyBalanceService'

export const handleEvmDeployTranche = errorHandler(_handleEvmDeployTranche)
async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise<void> {
Expand All @@ -19,7 +20,7 @@ async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise<void> {
const poolId = _poolId.toString()
const trancheId = _trancheId.substring(0, 34)

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

const pool = await PoolService.getOrSeed(poolId)
await TrancheService.getOrSeed(pool.id, trancheId)
Expand All @@ -30,12 +31,12 @@ async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise<void> {
export const handleEvmTransfer = errorHandler(_handleEvmTransfer)
async function _handleEvmTransfer(event: TransferLog): Promise<void> {
const [fromEvmAddress, toEvmAddress, amount] = event.args
logger.info(`Transfer ${fromEvmAddress.toString()}-${toEvmAddress.toString()} of ${amount.toString()}`)
logger.info(`Transfer ${fromEvmAddress}-${toEvmAddress} of ${amount.toString()} at block: ${event.blockNumber}`)

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

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

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

const txOut = InvestorTransactionService.transferOut({ ...orderData, address: fromAccount.id })
await txOut.save()

const fromBalance = await CurrencyBalanceService.getOrInitEvm(fromAddress, evmToken.id)
await fromBalance.debit(amount.toBigInt())
await fromBalance.save()
}

if (toEvmAddress.toString() !== evmTokenAddress) {
const toAddress = AccountService.evmToSubstrate(toEvmAddress.toString(), blockchain.id)
if (toEvmAddress !== evmTokenAddress) {
const toAddress = AccountService.evmToSubstrate(toEvmAddress, blockchain.id)
const toAccount = await AccountService.getOrInit(toAddress)
const txIn = InvestorTransactionService.transferIn({ ...orderData, address: toAccount.id })
await txIn.save()

const toBalance = await CurrencyBalanceService.getOrInitEvm(toAddress, blockchain.id)
await toBalance.credit(amount.toBigInt())
await toBalance.save()
}
}
9 changes: 9 additions & 0 deletions src/mappings/services/currencyBalanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export class CurrencyBalanceService extends CurrencyBalance {
return currencyBalance
}

static async getOrInitEvm(address: string, currency: string) {
let currencyBalance = await this.getById(address, currency)
if (currencyBalance === undefined) {
currencyBalance = this.init(address, currency)
await currencyBalance.save()
}
return currencyBalance
}

public async getBalance() {
const [_chainId, currencyType, ...currencySpec] = this.currencyId.split('-')
const enumPayload = formatEnumPayload(currencyType, ...currencySpec)
Expand Down
11 changes: 11 additions & 0 deletions src/mappings/services/currencyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ export class CurrencyService extends Currency {
}
return currency as CurrencyService
}

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)
if (!currency) {
currency = this.init(chainId, currencyId, WAD_DIGITS)
await currency.save()
}
return currency as CurrencyService
}
}

export const currencyFormatters: CurrencyFormatters = {
Expand Down

0 comments on commit 8924aa7

Please sign in to comment.