Skip to content

Commit

Permalink
API method updates (#3122)
Browse files Browse the repository at this point in the history
* adding more txn tracing and new sdk methods

* smalling naming nits

* adding new function to return status and information at once
  • Loading branch information
Defi-Moses authored Sep 12, 2024
1 parent 2e517b3 commit 913f0e2
Showing 1 changed file with 176 additions and 0 deletions.
176 changes: 176 additions & 0 deletions packages/rest-api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,182 @@ app.get('/bridgeTxInfo', async (req, res) => {
}
})

// Get Synapse Transaction ID
app.get('/getSynapseTxId', (req, res) => {
try {
const query = req.query
const originChainId = Number(query.originChainId)
const bridgeModule = String(query.bridgeModule)
const txHash = String(query.txHash)

if (!originChainId || !bridgeModule || !txHash) {
res.status(400).send({
message: 'Invalid request: Missing required parameters',
})
return
}

Synapse.getSynapseTxId(originChainId, bridgeModule, txHash)
.then((synapseTxId) => {
res.json({ synapseTxId })
})
.catch((err) => {
res.status(400).send({
message:
'Ensure that your request matches the following format: /getSynapseTxId?originChainId=8453&bridgeModule=SynapseRFQ&txHash=0x4acd82091b54cf584d50adcad9f57c61055beaca130016ecc3798d3d61f5487a',
error: err.message,
})
})
} catch (err) {
res.status(400).send({
message:
'Ensure that your request matches the following format: /getSynapseTxId?originChainId=8453&bridgeModule=SynapseRFQ&txHash=0x4acd82091b54cf584d50adcad9f57c61055beaca130016ecc3798d3d61f5487a',
error: err.message,
})
}
})

// Get Bridge Transaction Status
app.get('/getBridgeTxStatus', async (req, res) => {
try {
const query = req.query
const destChainId = Number(query.destChainId)
const bridgeModule = String(query.bridgeModule)
const synapseTxId = String(query.synapseTxId)

if (!destChainId || !bridgeModule || !synapseTxId) {
res.status(400).send({
message: 'Invalid request: Missing required parameters',
})
return
}

try {
const status = await Synapse.getBridgeTxStatus(
destChainId,
bridgeModule,
synapseTxId
)

if (status) {
const txIdWithout0x = synapseTxId.startsWith('0x')
? synapseTxId.slice(2)
: synapseTxId
const graphqlEndpoint = 'https://explorer.omnirpc.io/graphql'
const graphqlQuery = `
{
bridgeTransactions(
useMv: true
kappa: "${txIdWithout0x}"
) {
toInfo {
chainID
address
txnHash
USDValue
tokenSymbol
blockNumber
formattedTime
}
}
}
`

const graphqlResponse = await fetch(graphqlEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: graphqlQuery }),
})

const graphqlData = await graphqlResponse.json()
const toInfo = graphqlData.data.bridgeTransactions[0]?.toInfo || null

res.json({ status, toInfo })
} else {
res.json({ status })
}
} catch (err) {
res.status(400).send({
message: 'Error fetching bridge transaction status',
error: err.message,
})
}
} catch (err) {
res.status(400).send({
message: 'Invalid request',
error: err.message,
})
}
})

// Get Destination Transaction Hash
app.get('/getDestinationTx', async (req, res) => {
try {
const query = req.query
const originChainId = Number(query.originChainId)
const txHash = String(query.txHash)

if (!originChainId || !txHash) {
res.status(400).send({
message: 'Invalid request: Missing required parameters',
})
return
}

try {
const graphqlEndpoint = 'https://explorer.omnirpc.io/graphql'
const graphqlQuery = `
{
bridgeTransactions(
useMv: true
chainIDFrom: ${originChainId}
txnHash: "${txHash}"
) {
toInfo {
chainID
address
txnHash
USDValue
tokenSymbol
blockNumber
formattedTime
}
}
}
`

const graphqlResponse = await fetch(graphqlEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: graphqlQuery }),
})

const graphqlData = await graphqlResponse.json()
const toInfo = graphqlData.data.bridgeTransactions[0]?.toInfo || null

if (toInfo === null) {
res.json({ status: 'pending' })
} else {
res.json({ status: 'completed', toInfo })
}
} catch (err) {
res.status(400).send({
message: 'Error fetching bridge transaction status',
error: err.message,
})
}
} catch (err) {
res.status(400).send({
message: 'Invalid request',
error: err.message,
})
}
})

export const server = app.listen(port, () => {
console.log(`Server listening at ${port}`)
})
Expand Down

0 comments on commit 913f0e2

Please sign in to comment.