Skip to content

Commit

Permalink
- code formatted
Browse files Browse the repository at this point in the history
- building error removed
  • Loading branch information
bgodlin committed Mar 2, 2024
1 parent 947d76a commit fdfe3e0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 60 deletions.
9 changes: 4 additions & 5 deletions Stellar/soroban-futurenet-starter/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "soroban-futurenet-starter",
"name": "soroban-testnet-starter",
"version": "0.0.1",
"description": "This project can be use as a starting point for developing your new Stellar Soroban Future Network SubQuery project",
"description": "This project can be use as a starting point for developing your new Stellar Soroban Test Network SubQuery project",
"main": "dist/index.js",
"scripts": {
"build": "subql build",
Expand All @@ -22,13 +22,12 @@
"license": "MIT",
"dependencies": {
"@subql/common": "latest",
"@subql/types-stellar": "latest",
"stellar-sdk": "^10.4.1"
"@subql/types-stellar": "latest"
},
"devDependencies": {
"@subql/cli": "latest",
"@subql/testing": "latest",
"@subql/types": "latest",
"@subql/testing": "latest",
"ethers": "^5.7.2",
"typescript": "^5.2.2"
}
Expand Down
4 changes: 2 additions & 2 deletions Stellar/soroban-futurenet-starter/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ const project: StellarProject = {
dataSources: [
{
kind: StellarDatasourceKind.Runtime,
startBlock: 49260759, // Set this as a logical start block, it might be block 1 (genesis) or when your contract was deployed
startBlock: 599994, // Set this as a logical start block, it might be block 1 (genesis) or when your contract was deployed
mapping: {
file: "./dist/index.js",
handlers: [
{
handler: "handleOperation",
kind: StellarHandlerKind.Operation,
filter: {
type: Horizon.OperationResponseType.payment,
type: Horizon.HorizonApi.OperationResponseType.payment,
},
},
{
Expand Down
41 changes: 35 additions & 6 deletions Stellar/soroban-futurenet-starter/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import {
StellarEffect,
SorobanEvent,
} from "@subql/types-stellar";
import { AccountCredited, AccountDebited } from "stellar-sdk/lib/types/effects";
import {
AccountCredited,
AccountDebited,
} from "stellar-sdk/lib/horizon/types/effects";
import { Horizon } from "stellar-sdk";
import { Address, xdr } from "soroban-client";
import { logger } from "ethers";

export async function handleOperation(
op: StellarOperation<Horizon.PaymentOperationResponse>
op: StellarOperation<Horizon.HorizonApi.PaymentOperationResponse>
): Promise<void> {
logger.info(`Indexing operation ${op.id}, type: ${op.type}`);

Expand Down Expand Up @@ -69,7 +74,9 @@ 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\]
Expand All @@ -78,15 +85,28 @@ export async function handleEvent(event: SorobanEvent): Promise<void> {
topic: [env, from, to],
} = event;

const fromAccount = await checkAndGetAccount(from, event.ledger.sequence);
const toAccount = await checkAndGetAccount(to, 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({
id: event.id,
ledger: event.ledger.sequence,
date: new Date(event.ledgerClosedAt),
contract: event.contractId,
contract: event.contractId?.contractId().toString()!,
fromId: fromAccount.id,
toId: toAccount.id,
value: BigInt(event.value.decoded!),
Expand All @@ -111,3 +131,12 @@ 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();
}
}
64 changes: 36 additions & 28 deletions Stellar/soroban-starter/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import {
StellarEffect,
SorobanEvent,
} from "@subql/types-stellar";
import { AccountCredited, AccountDebited } from "stellar-sdk/lib/horizon/types/effects";
import { Horizon, } from "stellar-sdk";
import { Address,xdr,} from 'soroban-client';
import {logger} from "ethers";
import {
AccountCredited,
AccountDebited,
} from "stellar-sdk/lib/horizon/types/effects";
import { Horizon } from "stellar-sdk";
import { Address, xdr } from "soroban-client";
import { logger } from "ethers";

export async function handleOperation(
op: StellarOperation<Horizon.HorizonApi.PaymentOperationResponse>
op: StellarOperation<Horizon.HorizonApi.PaymentOperationResponse>
): Promise<void> {
logger.info(`Indexing operation ${op.id}, type: ${op.type}`);

Expand All @@ -31,13 +34,13 @@ export async function handleOperation(
}

export async function handleCredit(
effect: StellarEffect<AccountCredited>
effect: StellarEffect<AccountCredited>
): Promise<void> {
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({
Expand All @@ -51,13 +54,13 @@ export async function handleCredit(
}

export async function handleDebit(
effect: StellarEffect<AccountDebited>
effect: StellarEffect<AccountDebited>
): Promise<void> {
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({
Expand All @@ -71,24 +74,32 @@ export async function handleDebit(
}

export async function handleEvent(event: SorobanEvent): Promise<void> {
logger.info(`New transfer event found at block ${event.ledger.sequence.toString()}`);
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;

try {
decodeAddress(from)
decodeAddress(to)
}catch (e) {
logger.info(`decode address failed`)
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);
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 All @@ -107,8 +118,8 @@ export async function handleEvent(event: SorobanEvent): Promise<void> {
}

async function checkAndGetAccount(
id: string,
ledgerSequence: number
id: string,
ledgerSequence: number
): Promise<Account> {
let account = await Account.get(id.toLowerCase());
if (!account) {
Expand All @@ -122,13 +133,10 @@ async function checkAndGetAccount(
}

// scValToNative not works, temp solution
function decodeAddress(scVal:xdr.ScVal):string{
function decodeAddress(scVal: xdr.ScVal): string {
try {
return Address.account(scVal.address().accountId().ed25519()).toString();
}catch (e) {
return Address.contract(
scVal.address().contractId()).toString();
} catch (e) {
return Address.contract(scVal.address().contractId()).toString();
}


}
}
46 changes: 27 additions & 19 deletions Stellar/soroban-testnet-starter/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import {
StellarEffect,
SorobanEvent,
} from "@subql/types-stellar";
import { AccountCredited, AccountDebited } from "stellar-sdk/lib/horizon/types/effects";
import { Horizon, } from "stellar-sdk";
import { Address,xdr,} from 'soroban-client';
import {logger} from "ethers";
import {
AccountCredited,
AccountDebited,
} from "stellar-sdk/lib/horizon/types/effects";
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 @@ -71,24 +74,32 @@ export async function handleDebit(
}

export async function handleEvent(event: SorobanEvent): Promise<void> {
logger.info(`New transfer event found at block ${event.ledger.sequence.toString()}`);
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;

try {
decodeAddress(from)
decodeAddress(to)
}catch (e) {
logger.info(`decode address failed`)
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);
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 @@ -122,13 +133,10 @@ async function checkAndGetAccount(
}

// scValToNative not works, temp solution
function decodeAddress(scVal:xdr.ScVal):string{
function decodeAddress(scVal: xdr.ScVal): string {
try {
return Address.account(scVal.address().accountId().ed25519()).toString();
}catch (e) {
return Address.contract(
scVal.address().contractId()).toString();
} catch (e) {
return Address.contract(scVal.address().contractId()).toString();
}


}
}

0 comments on commit fdfe3e0

Please sign in to comment.