From d3eb61a9b9c5421e8869d39d8bad860f835bb495 Mon Sep 17 00:00:00 2001 From: JQQQ Date: Wed, 21 Feb 2024 19:10:23 +1300 Subject: [PATCH] update mapping for mainnet --- .../src/mappings/mappingHandlers.ts | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/Stellar/soroban-starter/src/mappings/mappingHandlers.ts b/Stellar/soroban-starter/src/mappings/mappingHandlers.ts index 8b11e35..45af16f 100644 --- a/Stellar/soroban-starter/src/mappings/mappingHandlers.ts +++ b/Stellar/soroban-starter/src/mappings/mappingHandlers.ts @@ -5,10 +5,12 @@ import { SorobanEvent, } from "@subql/types-stellar"; import { AccountCredited, AccountDebited } from "stellar-sdk/lib/horizon/types/effects"; -import { Horizon } from "stellar-sdk"; +import { Horizon, } from "stellar-sdk"; +import { Address,xdr,} from 'soroban-client'; +import {logger} from "ethers"; export async function handleOperation( - op: StellarOperation + op: StellarOperation ): Promise { logger.info(`Indexing operation ${op.id}, type: ${op.type}`); @@ -29,13 +31,13 @@ export async function handleOperation( } export async function handleCredit( - effect: StellarEffect + effect: StellarEffect ): Promise { logger.info(`Indexing effect ${effect.id}, type: ${effect.type}`); const account = await checkAndGetAccount( - effect.account, - effect.ledger.sequence + effect.account, + effect.ledger.sequence ); const credit = Credit.create({ @@ -49,13 +51,13 @@ export async function handleCredit( } export async function handleDebit( - effect: StellarEffect + effect: StellarEffect ): Promise { logger.info(`Indexing effect ${effect.id}, type: ${effect.type}`); const account = await checkAndGetAccount( - effect.account, - effect.ledger.sequence + effect.account, + effect.ledger.sequence ); const debit = Debit.create({ @@ -69,22 +71,24 @@ export async function handleDebit( } export async function handleEvent(event: SorobanEvent): Promise { - logger.info(`New transfer event found at block ${event.ledger}`); + logger.info(`New transfer event found at block ${event.ledger.sequence.toString()}`); // Get data from the event // The transfer event has the following payload \[env, from, to\] // logger.info(JSON.stringify(event)); const { - topic: [env, from, to], + topic: [env, from, to] } = event; - logger.debug(`from account: ${from.address().accountId().value().toString()}`) - logger.debug(`to account: ${to.address().accountId().value().toString()}`) - - const fromAccount = await checkAndGetAccount(from.address().accountId().value().toString(), event.ledger.sequence); - const toAccount = await checkAndGetAccount(to.address().accountId().value().toString(), event.ledger.sequence); - + try { + decodeAddress(from) + decodeAddress(to) + }catch (e) { + logger.info(`decode address failed`) + } + const fromAccount = await checkAndGetAccount(decodeAddress(from), event.ledger.sequence); + const toAccount = await checkAndGetAccount(decodeAddress(to), event.ledger.sequence); // Create the new transfer entity const transfer = Transfer.create({ @@ -103,8 +107,8 @@ export async function handleEvent(event: SorobanEvent): Promise { } async function checkAndGetAccount( - id: string, - ledgerSequence: number + id: string, + ledgerSequence: number ): Promise { let account = await Account.get(id.toLowerCase()); if (!account) { @@ -116,3 +120,15 @@ async function checkAndGetAccount( } return account; } + +// scValToNative not works, temp solution +function decodeAddress(scVal:xdr.ScVal):string{ + try { + return Address.account(scVal.address().accountId().ed25519()).toString(); + }catch (e) { + return Address.contract( + scVal.address().contractId()).toString(); + } + + +} \ No newline at end of file