Skip to content

Commit f6c612c

Browse files
committed
fix: incosistent initialisation of accounts on evm
1 parent 8cc1cc6 commit f6c612c

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

src/@types/gobal.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-var */
12
import { ApiPromise } from '@polkadot/api'
23
import type { Provider } from '@ethersproject/providers'
34
import { ApiDecoration } from '@polkadot/api/types'
@@ -11,5 +12,7 @@ declare global {
1112
const api: ApiAt & Provider
1213
const unsafeApi: ApiPromise | undefined
1314
function getNodeEvmChainId(): Promise<string | undefined>
15+
var isSubstrateNode: boolean
16+
var isEvmNode: boolean
1417
}
1518
export {}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const isSubstrateNode = 'query' in api
88
const isEvmNode = typeof (api as Provider).getNetwork === 'function'
99
const ethNetworkProm = isEvmNode ? (api as Provider).getNetwork() : null
1010

11+
global.isSubstrateNode = isSubstrateNode
12+
global.isEvmNode = isEvmNode
1113
global.fetch = fetch as unknown as typeof global.fetch
1214
global.atob = atob as typeof global.atob
1315
global.getNodeEvmChainId = async function () {

src/mappings/handlers/evmHandlers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import { PoolService } from '../services/poolService'
77
import { TrancheService } from '../services/trancheService'
88
import { InvestorTransactionData, InvestorTransactionService } from '../services/investorTransactionService'
99
import { CurrencyService } from '../services/currencyService'
10-
import { BlockchainService } from '../services/blockchainService'
10+
import { BlockchainService, LOCAL_CHAIN_ID } from '../services/blockchainService'
1111
import { CurrencyBalanceService } from '../services/currencyBalanceService'
1212
import { TrancheBalanceService } from '../services/trancheBalanceService'
1313
import { escrows } from '../../config'
1414
import { InvestorPositionService } from '../services/investorPositionService'
1515
import { getPeriodStart } from '../../helpers/timekeeperService'
16-
//const networkPromise = typeof ethApi.getNetwork === 'function' ? ethApi.getNetwork() : null
1716

1817
export const handleEvmDeployTranche = errorHandler(_handleEvmDeployTranche)
1918
async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise<void> {
@@ -23,6 +22,7 @@ async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise<void> {
2322

2423
const chainId = await getNodeEvmChainId()
2524
if (!chainId) throw new Error('Unable to retrieve chainId')
25+
await BlockchainService.getOrInit(LOCAL_CHAIN_ID)
2626
const evmBlockchain = await BlockchainService.getOrInit(chainId)
2727

2828
const poolId = _poolId.toString()
@@ -73,12 +73,14 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
7373
const _isFromUserEscrow = fromEvmAddress === userEscrowAddress
7474

7575
if (!evmToken.poolId || !evmToken.trancheId) throw new Error('This is not a tranche token')
76+
const pool = await PoolService.getById(evmToken.poolId)
77+
if (!pool) throw new Error('Pool not found!')
7678
const trancheId = evmToken.trancheId.split('-')[1]
77-
const tranche = await TrancheService.getById(evmToken.poolId, trancheId)
79+
const tranche = await TrancheService.getById(pool.id, trancheId)
7880
if (!tranche) throw new Error('Tranche not found!')
7981

8082
const orderData: Omit<InvestorTransactionData, 'address'> = {
81-
poolId: evmToken.poolId,
83+
poolId: pool.id,
8284
trancheId: trancheId,
8385
hash: event.transactionHash,
8486
timestamp: timestamp,

src/mappings/services/accountService.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ const EVM_SUFFIX = '45564d00'
55

66
export class AccountService extends Account {
77
static async init(address: string) {
8-
logger.info(`Initialising new account: ${address}`)
98
if (await this.isForeignEvm(address)) {
109
const chainId = this.readEvmChainId(address)
10+
logger.info(`Initialising new account: ${address} as foreign for chainId: ${chainId}`)
1111
const account = new this(address, chainId)
1212
account.evmAddress = address.substring(0, 42)
1313
return account
1414
} else {
15+
logger.info(`Initialising new account: ${address}`)
1516
return new this(address, LOCAL_CHAIN_ID)
1617
}
1718
}
1819

1920
static async getOrInit(address: string, blockchainService = BlockchainService): Promise<AccountService> {
20-
let account = (await this.get(address))
21+
let account = await this.get(address)
2122
if (!account) {
2223
account = await this.init(address)
2324
await blockchainService.getOrInit(account.chainId)
@@ -27,7 +28,7 @@ export class AccountService extends Account {
2728
}
2829

2930
static evmToSubstrate(evmAddress: string, chainId: string) {
30-
const chainHex = parseInt(chainId,10).toString(16).padStart(4, '0')
31+
const chainHex = parseInt(chainId, 10).toString(16).padStart(4, '0')
3132
return `0x${evmAddress.substring(2).toLowerCase()}000000000000${chainHex}${EVM_SUFFIX}`
3233
}
3334

@@ -40,8 +41,12 @@ export class AccountService extends Account {
4041
}
4142

4243
static async isForeignEvm(address: string) {
43-
const nodeEvmChainId = await getNodeEvmChainId()
44-
return this.isEvm(address) && this.readEvmChainId(address) !== nodeEvmChainId
44+
if (isSubstrateNode) {
45+
const nodeEvmChainId = await getNodeEvmChainId()
46+
return this.isEvm(address) && this.readEvmChainId(address) !== nodeEvmChainId
47+
} else {
48+
return this.isEvm(address)
49+
}
4550
}
4651

4752
public isForeignEvm() {

0 commit comments

Comments
 (0)