Skip to content

Commit

Permalink
Adapt synapseCCTP, add check to synapseBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Dec 14, 2023
1 parent 0626e04 commit cd19b3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
29 changes: 9 additions & 20 deletions packages/sdk-router/src/router/synapseCCTPRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SynapseCCTPRouter as SynapseCCTPRouterContract } from '../typechain/Syn
import { Router } from './router'
import { Query, narrowToCCTPRouterQuery, reduceToQuery } from './query'
import cctpAbi from '../abi/SynapseCCTP.json'
import { getMatchingTxLog } from '../utils/logs'
import { BigintIsh } from '../constants'
import {
BridgeToken,
Expand All @@ -30,6 +31,9 @@ export class SynapseCCTPRouter extends Router {
private readonly routerContract: SynapseCCTPRouterContract
private cctpContractCache: Contract | undefined

// All possible events emitted by the SynapseCCTP contract in the origin transaction
private readonly originEvents = ['CircleRequestSent']

constructor(chainId: number, provider: Provider, address: string) {
// Parent constructor throws if chainId or provider are undefined
super(chainId, provider)
Expand Down Expand Up @@ -122,27 +126,12 @@ export class SynapseCCTPRouter extends Router {
*/
public async getBridgeID(txHash: string): Promise<string> {
const cctpContract = await this.getCctpContract()
// Extract the CircleRequestSent event topic
// We know it always exists as we are using the correct ABI
const circleRequestSentTopic = cctpContract.interface.getEventTopic(
Object.values(cctpContract.interface.events).find(
(event) => event.name === 'CircleRequestSent'
)!
const cctpLog = await getMatchingTxLog(
this.provider,
txHash,
cctpContract,
this.originEvents
)
// Iterate through logs to find CircleRequestSent event emitted by SynapseCCTP in the provided tx
const txReceipt = await this.provider.getTransactionReceipt(txHash)
if (!txReceipt) {
throw new Error('Failed to get transaction receipt')
}
const cctpLog = txReceipt.logs.find((log) => {
return (
log.address === cctpContract.address &&
log.topics[0] === circleRequestSentTopic
)
})
if (!cctpLog) {
throw new Error('CircleRequestSent log not found')
}
// RequestID always exists in the log as we are using the correct ABI
const parsedLog = cctpContract.interface.parseLog(cctpLog)
return parsedLog.args.requestID
Expand Down
20 changes: 20 additions & 0 deletions packages/sdk-router/src/router/synapseRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
reduceToFeeConfig,
reduceToPoolToken,
} from './types'
import { getMatchingTxLog } from '../utils/logs'

/**
* Wraps [tokens, lpToken] returned by the SynapseRouter contract into a PoolInfo object.
Expand Down Expand Up @@ -60,6 +61,16 @@ export class SynapseRouter extends Router {
private readonly routerContract: SynapseRouterContract
private bridgeContractCache: Contract | undefined

// All possible events emitted by the SynapseBridge contract in the origin transaction (in alphabetical order)
private readonly originEvents = [
'TokenDeposit',
'TokenDepositAndSwap',
'TokenRedeem',
'TokenRedeemAndRemove',
'TokenRedeemAndSwap',
'TokenRedeemV2',
]

constructor(chainId: number, provider: Provider, address: string) {
// Parent constructor throws if chainId or provider are undefined
super(chainId, provider)
Expand Down Expand Up @@ -142,6 +153,15 @@ export class SynapseRouter extends Router {
* @inheritdoc Router.getBridgeID
*/
public async getBridgeID(txHash: string): Promise<string> {
// Check that the transaction hash refers to an origin transaction
const bridgeContract = await this.getBridgeContract()
await getMatchingTxLog(
this.provider,
txHash,
bridgeContract,
this.originEvents
)
// Once we know the transaction is an origin transaction, we can calculate the bridge ID
return solidityKeccak256(['string'], [txHash])
}

Expand Down

0 comments on commit cd19b3b

Please sign in to comment.