Skip to content

Commit

Permalink
Merge pull request #10 from 0xPolygon/gateway-integration
Browse files Browse the repository at this point in the history
upd: gateway integration
  • Loading branch information
nitinmittal23 authored May 9, 2024
2 parents d8c077d + 30ee402 commit aabecc1
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
PRIVATE_KEY=0x # The private key of the EOA which will be submitting the claim transaction
NETWORK=testnet # testnet/mainnet
TRANSACTIONS_URL= https://api-gateway.polygon.technology/api/v3/transactions/testnet # The transaction list endpoint of the bridge API service
TRANSACTIONS_API_KEY=64cbf956-198a-47e0-b4a1-2b3432d8f70d
PROOF_URL= https://api-gateway.polygon.technology/api/v3/merkle-proof/testnet # The merkle proof endpoint of the bridge API service
RPC_URL=https://zkyoto.explorer.startale.com # The rpc of your chain
PROOF_API_KEY=64cbf956-198a-47e0-b4a1-2b3432d8f70d
BRIDGE_CONTRACT=0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582 # contract address of your bridge contract
GAS_STATION_URL=https://gasstation-staging.polygon.technology/astar/zkyoto # Follow Readme to spin up your own gas estimation service
SOURCE_NETWORKS=[0,1] # The list of soruce network ID's of the chains you want to monitor and process claim transactions from
Expand Down
6 changes: 4 additions & 2 deletions .github/taskdef/staging-taskdef.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ env_vars:
- name: NETWORK
value: testnet
- name: TRANSACTIONS_URL
value: https://bridge-api-testnet-dev.polygon.technology/transactions
value: https://api-gateway.polygon.technology/api/v3/transactions/testnet
- name: PROOF_URL
value: https://bridge-api-testnet-dev.polygon.technology/merkle-proof
value: https://api-gateway.polygon.technology/api/v3/merkle-proof/testnet
- name: BRIDGE_CONTRACT
value: "0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582"
- name: GAS_STATION_URL
Expand All @@ -29,3 +29,5 @@ secret_vars:
- RPC_URL
- SLACK_URL
- SENTRY_DSN
- TRANSACTIONS_API_KEY
- PROOF_API_KEY
37 changes: 1 addition & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"axios": "^1.6.8",
"dotenv": "^16.0.1",
"ethers": "^6.12.0",
"long": "^5.2.0",
"node-cron": "^3.0.3"
"long": "^5.2.0"
},
"devDependencies": {
"@types/node-cron": "^3.0.11",
Expand Down
12 changes: 7 additions & 5 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export default {
PRIVATE_KEY: process.env.PRIVATE_KEY,
SOURCE_NETWORKS: process.env.SOURCE_NETWORKS || '[]',
DESTINATION_NETWORK: process.env.DESTINATION_NETWORK,
TRANSACTIONS_URL: process.env.TRANSACTIONS_URL || 'https://bridge-api-testnet-dev.polygon.technology/transactions',
PROOF_URL: process.env.PROOF_URL || 'https://bridge-api-testnet-dev.polygon.technology/merkle-proof',
RPC_URL: process.env.RPC_URL || 'https://rpc.cardona.zkevm-rpc.com',
BRIDGE_CONTRACT: process.env.BRIDGE_CONTRACT || '0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582',
GAS_STATION_URL: process.env.GAS_STATION_URL || 'https://gasstation.polygon.technology/zkevm',
TRANSACTIONS_URL: process.env.TRANSACTIONS_URL,
TRANSACTIONS_API_KEY: process.env.TRANSACTIONS_API_KEY,
PROOF_URL: process.env.PROOF_URL,
PROOF_API_KEY: process.env.PROOF_API_KEY,
RPC_URL: process.env.RPC_URL,
BRIDGE_CONTRACT: process.env.BRIDGE_CONTRACT,
GAS_STATION_URL: process.env.GAS_STATION_URL,
SLACK_URL: process.env.SLACK_URL,
NETWORK: process.env.NETWORK,
LOGGER: {
Expand Down
32 changes: 20 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import AutoClaimService from "./services/auto-claim.js";
import { ethers } from 'ethers';
import config from "./config/index.js";
import bridgeAbi from "./abi/bridge.js";
import { schedule } from "node-cron";
import SlackNotify from "./services/slack-notify.js";
import GasStation from "./services/gas-station.js";
import TransactionService from "./services/transaction.js";
Expand All @@ -22,37 +21,46 @@ Logger.create({
}
});

let autoClaimService: AutoClaimService;
async function run() {
while (true) {
await autoClaimService.claimTransactions();
await new Promise(r => setTimeout(r, 120000));
}
}

async function start() {
try {

const provider = new ethers.JsonRpcProvider(config.RPC_URL);
const wallet = new ethers.Wallet(config.PRIVATE_KEY as string, provider);

const contract = new ethers.Contract(
config.BRIDGE_CONTRACT,
config.BRIDGE_CONTRACT as string,
bridgeAbi,
wallet
);

let slackNotify = null;
if (config.SLACK_URL) {
slackNotify = new SlackNotify(config.SLACK_URL)
}
const autoClaimService = new AutoClaimService(
config.NETWORK as string,
autoClaimService = new AutoClaimService(
config.NETWORK as string,
contract,
new TransactionService(
config.PROOF_URL,
config.TRANSACTIONS_URL,
config.PROOF_URL as string,
config.TRANSACTIONS_URL as string,
config.SOURCE_NETWORKS,
config.DESTINATION_NETWORK as string
config.DESTINATION_NETWORK as string,
config.TRANSACTIONS_API_KEY,
config.PROOF_API_KEY
),
new GasStation(config.GAS_STATION_URL),
new GasStation(config.GAS_STATION_URL as string,),
slackNotify
);

// await autoClaimService.claimTransactions();
schedule("*/1 * * * *", autoClaimService.claimTransactions.bind(autoClaimService));
run();
} catch (error) {
// Logger.error({ error });
}
Expand Down
23 changes: 13 additions & 10 deletions src/services/auto-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,18 @@ export default class AutoClaimService {
{ gasPrice }
)
}
} catch (error) {
} catch (error: any) {
if (this.slackNotify) {
await this.slackNotify.notifyAdminForError({
network: this.network,
claimType: transaction.dataType as string,
bridgeTxHash: transaction.transactionHash as string,
sourceNetwork: transaction.sourceNetwork,
destinationNetwork: transaction.destinationNetwork,
error: error.message ? error.message : JSON.stringify(error),
depositIndex: transaction.counter as number
});
}
Logger.error({ error })
}
return tx;
Expand All @@ -104,15 +115,7 @@ export default class AutoClaimService {
let tx = await this.claim(transaction, proof, globalIndex, gasPrice);

if (tx && this.slackNotify) {
await this.slackNotify.notifyAdmin({
network: this.network,
claimType: transaction.dataType as string,
bridgeTxHash: transaction.transactionHash as string,
sourceNetwork: transaction.sourceNetwork,
destinationNetwork: transaction.destinationNetwork,
claimTxHash: tx.hash,
depositIndex: transaction.counter as number
});
await this.slackNotify.notifyAdminForSuccess(tx.hash);

Logger.info({
type: 'transactionCompleted',
Expand Down
25 changes: 21 additions & 4 deletions src/services/slack-notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { INotifyParams } from "../types/index.js";

export default class SlackNotify {

constructor(private slackWebhookUrl: string) {}
constructor(private slackWebhookUrl: string) { }

async notifyAdmin(params: INotifyParams) {
async notifyAdminForError(params: INotifyParams) {
await axios.post(this.slackWebhookUrl, {
blocks: [
{
Expand Down Expand Up @@ -47,14 +47,31 @@ export default class SlackNotify {
type: "section",
text: {
type: "mrkdwn",
text: "*claimTxHash:* " + params.claimTxHash,
text: "*depositIndex:* " + params.depositIndex,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: "*depositIndex:* " + params.depositIndex,
text: "*Error:* " + params.error,
},
},
{
type: "divider"
},
],
});
}

async notifyAdminForSuccess(claimTxHash: string) {
await axios.post(this.slackWebhookUrl, {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "*transactionHash:* " + claimTxHash,
},
},
{
Expand Down
22 changes: 20 additions & 2 deletions src/services/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default class TransactionService {
private transactionUrl: string,
private sourceNetworks: string,
private destinationNetwork: string,
private transactionApiKey: string | undefined,
private proofApiKey: string | undefined,
) { }

async getPendingTransactions(): Promise<ITransaction[]> {
Expand All @@ -24,8 +26,15 @@ export default class TransactionService {
JSON.parse(this.sourceNetworks).forEach((networkId: number) => {
sourceNetworkIds = `${sourceNetworkIds}&sourceNetworkIds=${networkId}`
})
let headers = {};
if (this.transactionApiKey) {
headers = {
'x-access-token': this.transactionApiKey
}
}
let transactionData = await axios.get(
`${this.transactionUrl}?userAddress=${sourceNetworkIds}&destinationNetworkIds=${this.destinationNetwork}&status=READY_TO_CLAIM`
`${this.transactionUrl}?userAddress=${sourceNetworkIds}&destinationNetworkIds=${this.destinationNetwork}&status=READY_TO_CLAIM`,
{ headers }
);
if (transactionData && transactionData.data && transactionData.data.result) {
transactions = transactionData.data.result;
Expand Down Expand Up @@ -59,7 +68,16 @@ export default class TransactionService {
})
let proof: IProof | null = null;
try {
let proofData = await axios.get(`${this.proofUrl}?networkId=${sourceNetwork}&depositCount=${depositCount}`);
let headers = {};
if (this.proofApiKey) {
headers = {
'x-access-token': this.proofApiKey
}
}
let proofData = await axios.get(
`${this.proofUrl}?networkId=${sourceNetwork}&depositCount=${depositCount}`,
{ headers }
);
if (
proofData && proofData.data && proofData.data.proof &&
proofData.data.proof.merkle_proof && !proofData.data.proof.merkle_proof.message
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface INotifyParams {
bridgeTxHash: string
sourceNetwork: number
destinationNetwork: number
claimTxHash: string
error: string
depositIndex: number
}

Expand Down

0 comments on commit aabecc1

Please sign in to comment.