Skip to content

Commit

Permalink
attemp fix transfer account
Browse files Browse the repository at this point in the history
  • Loading branch information
jiqiang90 committed Feb 21, 2024
1 parent f147e5f commit 48012ee
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions Stellar/soroban-testnet-starter/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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<Horizon.HorizonApi.PaymentOperationResponse>
Expand Down Expand Up @@ -69,22 +71,24 @@ export async function handleDebit(
}

export async function handleEvent(event: SorobanEvent): Promise<void> {
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({
Expand Down Expand Up @@ -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();
}


}

0 comments on commit 48012ee

Please sign in to comment.