Skip to content

Commit

Permalink
chore: Remove old SDK and add latest package as dependancy
Browse files Browse the repository at this point in the history
  • Loading branch information
saadahmsiddiqui committed Oct 29, 2024
1 parent d4cedef commit 845c920
Show file tree
Hide file tree
Showing 22 changed files with 418 additions and 268 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"devDependencies": {
"@aws-sdk/client-sns": "^3.418.0",
"@buildwithsygma/sygma-sdk-core": "^2.1.0",
"@buildwithsygma/core": "^1.4.0",
"@chainsafe/eslint-config": "^1.1.0",
"@polkadot/types": "10.9.1",
"@rushstack/eslint-patch": "^1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/DomainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { FastifyReply, FastifyRequest } from "fastify"
import { Environment } from "@buildwithsygma/sygma-sdk-core"
import { Environment } from "@buildwithsygma/core"
import { ResourcesMetadataConfig } from "../utils/resourcesMetadata"
import { logger } from "../utils/logger"
import { DomainMetadataConfig } from "../utils/domainMetadata"
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/RoutesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { FastifyReply, FastifyRequest } from "fastify"
import { Environment } from "@buildwithsygma/sygma-sdk-core"
import { Environment } from "@buildwithsygma/core"
import { IRouteByResourceType, IRoutesByDomain } from "../interfaces"
import { logger } from "../utils/logger"
import { RoutesService } from "../services/routes.service"
Expand Down
80 changes: 6 additions & 74 deletions src/indexer/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,19 @@
/*
The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { XcmAssetId } from "@polkadot/types/interfaces"
import { logger } from "../../utils/logger"
/**
* The Licensed Work is (c) 2023 Sygma
* SPDX-License-Identifier: LGPL-3.0-only
*/
import { EthereumConfig, SubstrateConfig } from "@buildwithsygma/core"

export type LocalDomainConfig = {
url: string
startBlock: number
}

export const enum ResourceTypes {
FUNGIBLE = "fungible",
NON_FUNGIBLE = "nonfungible",
PERMISSIONED_GENERIC = "permissionedGeneric",
PERMISSIONLESS_GENERIC = "permissionlessGeneric",
}

export type SharedConfig = {
domains: Array<Domain>
}

export enum DomainTypes {
EVM = "evm",
SUBSTRATE = "substrate",
}

export type Domain = {
id: number
name: string
type: DomainTypes
bridge: string
feeRouter: string
feeHandlers: Array<FeeHandlerType>
handlers: Array<Handler>
nativeTokenSymbol: string
nativeTokenDecimals: number
startBlock: number
resources: Array<EvmResource | SubstrateResource>
}
type Handler = {
type: ResourceTypes
address: string
}

type FeeHandlerType = {
type: string
address: string
}

export type EvmResource = {
resourceId: string
type: ResourceTypes
address: string
symbol: string
decimals: number
}

export type SubstrateResource = {
resourceId: string
type: ResourceTypes
address: string
symbol: string
decimals: number
assetName: string
xcmMultiAssetId: XcmAssetId
}

export type RpcUrlConfig = Array<{
id: number
endpoint: string
}>

export const getSharedConfig = async (url: string): Promise<SharedConfig> => {
try {
const response = await fetch(url)
return (await response.json()) as SharedConfig
} catch (e) {
logger.error(`Failed to fetch config for ${process.env.STAGE || ""}`, e)
return Promise.reject(e)
}
}

export const getSsmDomainConfig = (): Map<number, string> => {
const parsedResponse = JSON.parse(process.env.RPC_URL_CONFIG!) as RpcUrlConfig
const rpcUrlMap = new Map<number, string>()
Expand All @@ -93,7 +25,7 @@ export const getSsmDomainConfig = (): Map<number, string> => {
}

// Note: based on the actual env vars that we pass, map the domains that we are going to use
export const getDomainsToIndex = (domains: Domain[]): Domain[] => {
export const getDomainsToIndex = (domains: Array<EthereumConfig | SubstrateConfig>): Array<EthereumConfig | SubstrateConfig> => {
const parsedResponse = JSON.parse(process.env.RPC_URL_CONFIG!) as RpcUrlConfig

return domains.filter(domain => {
Expand Down
25 changes: 14 additions & 11 deletions src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { caching } from "cache-manager"
import { logger } from "../utils/logger"
import { SubstrateIndexer } from "./services/substrateIndexer/substrateIndexer"
import { EvmIndexer } from "./services/evmIndexer/evmIndexer"
import { getSharedConfig, DomainTypes, Domain, getSsmDomainConfig, getDomainsToIndex, SubstrateResource } from "./config"
import { getSsmDomainConfig, getDomainsToIndex } from "./config"
import DomainRepository from "./repository/domain"
import DepositRepository from "./repository/deposit"
import TransferRepository from "./repository/transfer"
Expand All @@ -23,6 +23,7 @@ import AccountRepository from "./repository/account"
import CoinMarketCapService from "./services/coinmarketcap/coinmarketcap.service"
import { checkTransferStatus, getCronJob } from "./services/monitoringService"
import { NotificationSender } from "./services/monitoringService/notificationSender"
import { EthereumConfig, SubstrateConfig, Config, Environment, Network, SubstrateResource, SygmaConfig } from "@buildwithsygma/core"

interface DomainIndexer {
listenToEvents(): Promise<void>
Expand Down Expand Up @@ -72,7 +73,9 @@ init()
})

async function init(): Promise<{ domainIndexers: Array<DomainIndexer>; app: FastifyInstance; cron: CronJob }> {
const sharedConfig = await getSharedConfig(process.env.SHARED_CONFIG_URL!)
const config = new Config()
await config.init(process.env.SYGMA_ENVIRONMENT! as Environment)
const sharedConfig = config.getConfiguration()

const chainAnalysisUrl = process.env.CHAIN_ANALYSIS_URL || ""
const chainAnalysisApiKey = process.env.CHAIN_ANALYSIS_API_KEY || ""
Expand All @@ -97,11 +100,11 @@ async function init(): Promise<{ domainIndexers: Array<DomainIndexer>; app: Fast
const accountRepository = new AccountRepository()

const app = healthcheckRoute()
const resourceMap = await insertDomains(sharedConfig.domains, resourceRepository, domainRepository)
const resourceMap = await insertDomains(sharedConfig, resourceRepository, domainRepository)

const rpcUrlConfig = getSsmDomainConfig()

const domainsToIndex = getDomainsToIndex(sharedConfig.domains)
const domainsToIndex = getDomainsToIndex(sharedConfig.domains as Array<SubstrateConfig | EthereumConfig>)
const domainIndexers: Array<DomainIndexer> = []

const notificationSender = new NotificationSender(process.env.SNS_REGION!)
Expand All @@ -116,7 +119,7 @@ async function init(): Promise<{ domainIndexers: Array<DomainIndexer>; app: Fast
continue
}

if (domain.type == DomainTypes.SUBSTRATE) {
if (domain.type == Network.SUBSTRATE) {
try {
const substrateIndexer = new SubstrateIndexer(
domainRepository,
Expand All @@ -135,12 +138,12 @@ async function init(): Promise<{ domainIndexers: Array<DomainIndexer>; app: Fast
} catch (err) {
logger.error(`Error on domain: ${domain.id}... skipping`)
}
} else if (domain.type == DomainTypes.EVM) {
} else if (domain.type == Network.EVM) {
try {
const evmIndexer = new EvmIndexer(
domain,
rpcURL,
domainsToIndex,
domainsToIndex as EthereumConfig[],
domainRepository,
depositRepository,
transferRepository,
Expand All @@ -164,18 +167,18 @@ async function init(): Promise<{ domainIndexers: Array<DomainIndexer>; app: Fast
}

async function insertDomains(
domains: Array<Domain>,
config: SygmaConfig,
resourceRepository: ResourceRepository,
domainRepository: DomainRepository,
): Promise<Map<string, SubstrateResource>> {
const resourceMap = new Map<string, SubstrateResource>()
for (const domain of domains) {
for (const domain of config.domains) {
await domainRepository.insertDomain(domain.id, domain.startBlock.toString(), domain.name)
for (const resource of domain.resources) {
if (domain.type == DomainTypes.SUBSTRATE) {
if (domain.type === Network.SUBSTRATE) {
resourceMap.set(resource.resourceId, resource as SubstrateResource)
}
await resourceRepository.insertResource({ id: resource.resourceId, type: resource.type, decimals: resource.decimals })
await resourceRepository.insertResource({ id: resource.resourceId, type: resource.type, decimals: resource.decimals ?? 0 })
}
}
return resourceMap
Expand Down
4 changes: 2 additions & 2 deletions src/indexer/repository/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { PrismaClient } from "@prisma/client"
import { ResourceTypes } from "../config"
import { ResourceType } from "@buildwithsygma/core"

class ResourceRepository {
public resource = new PrismaClient().resource

public async insertResource(resource: { id: string; type: ResourceTypes; decimals: number }): Promise<void> {
public async insertResource(resource: { id: string; type: ResourceType; decimals: number }): Promise<void> {
await this.resource.upsert({
where: { id: resource.id },
update: { type: resource.type, decimals: resource.decimals },
Expand Down
7 changes: 4 additions & 3 deletions src/indexer/services/evmIndexer/evmEventParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { Provider, Log } from "ethers"
import { Domain, EvmResource } from "../../config"

import { getDecodedLogs } from "../../utils/evm"
import { logger } from "../../../utils/logger"
import { DecodedLogs } from "./evmTypes"
import { EthereumConfig, EvmResource, SubstrateConfig } from "@buildwithsygma/core"

export const nativeTokenAddress = "0x0000000000000000000000000000000000000000"

export async function decodeLogs(
provider: Provider,
domain: Domain,
domain: EthereumConfig,
logs: Log[],
resourceMap: Map<string, EvmResource>,
domains: Domain[],
domains: Array<EthereumConfig | SubstrateConfig>,
): Promise<DecodedLogs> {
const decodedLogs: DecodedLogs = {
deposit: [],
Expand Down
23 changes: 12 additions & 11 deletions src/indexer/services/evmIndexer/evmIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { Domain, EvmResource, SharedConfig } from "indexer/config"

import { ethers } from "ethers"

import winston from "winston"
Expand All @@ -19,6 +19,7 @@ import CoinMarketCapService from "../coinmarketcap/coinmarketcap.service"
import { OfacComplianceService } from "../ofac"
import { getLogs } from "./evmfilter"
import { decodeLogs } from "./evmEventParser"
import { EthereumConfig, EvmResource, Resource, SubstrateConfig, SygmaConfig } from "@buildwithsygma/core"

const BLOCK_TIME = Number(process.env.BLOCK_TIME) || 15000
const BLOCK_DELAY = Number(process.env.BLOCK_DELAY) || 10
Expand All @@ -32,20 +33,20 @@ export class EvmIndexer {
private transferRepository: TransferRepository
private executionRepository: ExecutionRepository
private feeRepository: FeeRepository
private domain: Domain
private domains: Domain[]
private domain: EthereumConfig
private domains: Array<SubstrateConfig | EthereumConfig>
private resourceMap: Map<string, EvmResource>
private stopped = false
private ofacComplianceService: OfacComplianceService
private accountRepository: AccountRepository
private coinMarketCapService: CoinMarketCapService
private sharedConfig: SharedConfig
private sygmaConfig: SygmaConfig
private logger: winston.Logger

constructor(
domain: Domain,
domain: EthereumConfig,
rpcURL: string,
domains: Domain[],
domains: EthereumConfig[],
domainRepository: DomainRepository,
depositRepository: DepositRepository,
transferRepository: TransferRepository,
Expand All @@ -54,7 +55,7 @@ export class EvmIndexer {
ofacComplianceService: OfacComplianceService,
accountRepository: AccountRepository,
coinMarketCapServiceInstance: CoinMarketCapService,
sharedConfig: SharedConfig,
sygmaConfig: SygmaConfig,
) {
this.provider = new ethers.JsonRpcProvider(rpcURL)
this.domainRepository = domainRepository
Expand All @@ -66,10 +67,10 @@ export class EvmIndexer {
this.ofacComplianceService = ofacComplianceService
this.domains = domains
this.resourceMap = new Map<string, EvmResource>()
domain.resources.map((resource: EvmResource) => this.resourceMap.set(resource.resourceId, resource))
domain.resources.map((resource: Resource) => this.resourceMap.set(resource.resourceId, resource as EvmResource))
this.accountRepository = accountRepository
this.coinMarketCapService = coinMarketCapServiceInstance
this.sharedConfig = sharedConfig
this.sygmaConfig = sygmaConfig
this.logger = rootLogger.child({
domain: domain.name,
domainID: domain.id,
Expand All @@ -82,7 +83,7 @@ export class EvmIndexer {

async listenToEvents(): Promise<void> {
const lastIndexedBlock = await this.getLastIndexedBlock(this.domain.id)
let currentBlock = this.domain.startBlock
let currentBlock = Number(this.domain.startBlock)
if (lastIndexedBlock && lastIndexedBlock > this.domain.startBlock) {
currentBlock = lastIndexedBlock + 1
}
Expand Down Expand Up @@ -131,7 +132,7 @@ export class EvmIndexer {
this.ofacComplianceService,
this.accountRepository,
this.coinMarketCapService,
this.sharedConfig,
this.sygmaConfig,
)
}

Expand Down
5 changes: 2 additions & 3 deletions src/indexer/services/evmIndexer/evmfilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import { EthereumConfig } from "@buildwithsygma/core"
import { Provider, Log, id } from "ethers"

import { Domain } from "../../config"

enum Topics {
DEPOSIT = "Deposit(uint8,bytes32,uint64,address,bytes,bytes)",
FAILED_HANDLER_EXECUTION = "FailedHandlerExecution(bytes,uint8,uint64)",
PROPOSAL_EXECUTION = "ProposalExecution(uint8,uint64,bytes32,bytes)",
}

export async function getLogs(provider: Provider, domain: Domain, fromBlock: number, toBlock: number): Promise<Log[]> {
export async function getLogs(provider: Provider, domain: EthereumConfig, fromBlock: number, toBlock: number): Promise<Log[]> {
const filter: Array<string> = [domain.bridge]

return await provider.getLogs({
Expand Down
Loading

0 comments on commit 845c920

Please sign in to comment.