Skip to content

Commit

Permalink
Merge pull request #13 from 0xPolygon/gas-estimate
Browse files Browse the repository at this point in the history
fix: compress claim gas estimate
  • Loading branch information
py-zoid authored Jun 7, 2024
2 parents 72349aa + a8e3923 commit 6af4a5f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .github/taskdef/staging-taskdef.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ env_vars:
value: https://api-gateway.polygon.technology/api/v3/merkle-proof/testnet
- name: CLAIM_COMPRESSOR_CONTRACT
value: "0xeADFA914e7aBf33179E7820Bd13043962a7B5F9d"
- name: BRIDGE_CONTRACT
value: "0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582"
- name: GAS_STATION_URL
value: https://gasstation-testnet.polygon.technology/zkevm/cardona
- name: SOURCE_NETWORKS
Expand Down
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import AutoClaimService from "./services/auto-claim.js";
import { ethers } from 'ethers';
import config from "./config/index.js";
import claimCompressorAbi from "./abi/claim_compressor.js";
import bridgeAbi from "./abi/bridge.js";
import SlackNotify from "./services/slack-notify.js";
import GasStation from "./services/gas-station.js";
import TransactionService from "./services/transaction.js";
Expand Down Expand Up @@ -35,19 +36,22 @@ async function start() {
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.CLAIM_COMPRESSOR_CONTRACT as string,
claimCompressorAbi,
wallet
);

let slackNotify = null;
if (config.SLACK_URL) {
slackNotify = new SlackNotify(config.SLACK_URL)
}
autoClaimService = new AutoClaimService(
config.NETWORK as string,
contract,
new ethers.Contract(
config.CLAIM_COMPRESSOR_CONTRACT as string,
claimCompressorAbi,
wallet
),
new ethers.Contract(
config.BRIDGE_CONTRACT as string,
bridgeAbi,
wallet
),
new TransactionService(
config.PROOF_URL as string,
config.TRANSACTIONS_URL as string,
Expand All @@ -56,7 +60,8 @@ async function start() {
config.TRANSACTIONS_API_KEY,
config.PROOF_API_KEY
),
new GasStation(config.GAS_STATION_URL as string,),
new GasStation(config.GAS_STATION_URL as string),
config.DESTINATION_NETWORK as string,
slackNotify
);

Expand Down
71 changes: 39 additions & 32 deletions src/services/auto-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ export default class AutoClaimService {
/**
* @constructor
*
* @param {ethers.Contract} contract
* @param {string} network
* @param {ethers.Contract} compressContract
* @param {ethers.Contract} bridgeContract
* @param {TransactionService} transactionService
* @param {GasStation} gasStation
* @param {string} destinationNetwork
* @param {SlackNotify | null} slackNotify
*/
constructor(
private network: string,
private contract: ethers.Contract,
private compressContract: ethers.Contract,
private bridgeContract: ethers.Contract,
private transactionService: TransactionService,
private gasStation: GasStation,
private destinationNetwork: string,
private slackNotify: SlackNotify | null = null
) { }

Expand All @@ -35,44 +43,37 @@ export default class AutoClaimService {
}

async estimateGas(transaction: ITransaction, proof: IProof, globalIndex: BigInt): Promise<boolean> {
let compressedClaimCalls = null;
try {
if (transaction.dataType === 'ERC20') {
compressedClaimCalls = await this.contract.compressClaimCall(
await this.bridgeContract.claimAsset.estimateGas(
proof.merkle_proof,
proof.rollup_merkle_proof,
globalIndex.toString(),
proof.main_exit_root,
proof.rollup_exit_root,
[{
smtProofLocalExitRoot: proof.merkle_proof,
smtProofRollupExitRoot: proof.rollup_merkle_proof,
globalIndex: globalIndex.toString(),
originNetwork: transaction.originTokenNetwork,
originAddress: transaction.originTokenAddress,
destinationAddress: transaction.receiver,
amount: transaction.amounts ? transaction.amounts[0] : '0',
metadata: transaction.metadata && transaction.metadata !== "" ? transaction.metadata : '0x',
isMessage: false
}]
transaction.originTokenNetwork,
transaction.originTokenAddress,
this.destinationNetwork,
transaction.receiver,
transaction.amounts ? transaction.amounts[0] : '0',
transaction.metadata && transaction.metadata !== "" ? transaction.metadata : '0x'
)

} else {
compressedClaimCalls = await this.contract.compressClaimCall(
await this.bridgeContract.claimMessage.estimateGas(
proof.merkle_proof,
proof.rollup_merkle_proof,
globalIndex.toString(),
proof.main_exit_root,
proof.rollup_exit_root,
[{
smtProofLocalExitRoot: proof.merkle_proof,
smtProofRollupExitRoot: proof.rollup_merkle_proof,
globalIndex: globalIndex.toString(),
originNetwork: '0',
originAddress: transaction.transactionInitiator,
destinationAddress: transaction.receiver,
amount: transaction.originTokenAddress ? '0' : transaction.amounts ? transaction.amounts[0] : '0',
metadata: transaction.metadata && transaction.metadata !== "" ? transaction.metadata : '0x',
isMessage: true
}]
'0',
transaction.transactionInitiator,
this.destinationNetwork,
transaction.receiver,
transaction.originTokenAddress ? '0' : transaction.amounts ? transaction.amounts[0] : '0',
transaction.metadata && transaction.metadata !== "" ? transaction.metadata : '0x',
)
}

await this.contract.sendCompressedClaims.estimateGas(compressedClaimCalls);
return true;
} catch (error: any) {
if (this.slackNotify) {
Expand Down Expand Up @@ -130,14 +131,14 @@ export default class AutoClaimService {
}
}

response = await this.contract.compressClaimCall(
response = await this.compressContract.compressClaimCall(
main_exit_root,
rollup_exit_root,
data,
{ gasPrice }
)
response = await this.contract.sendCompressedClaims(response)
response = await this.compressContract.sendCompressedClaims(response)

Logger.info({
type: 'claimBatch',
status: 'success',
Expand Down Expand Up @@ -175,6 +176,12 @@ export default class AutoClaimService {
}
}

Logger.info({
location: 'AutoClaimService',
function: 'claimTransactions',
call: 'finalClaimableTransaction length',
data: finalClaimableTransaction.length
})
const length = finalClaimableTransaction.length;
for (let i = 0; i < length; i += 5) {
const batch = finalClaimableTransaction.slice(i, i + 5);
Expand Down
2 changes: 1 addition & 1 deletion src/services/gas-station.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class GasStation {
throw new Error("something went wront while calculating gasPrice")
} catch (error: any) {
Logger.error({
location: 'AutoClaimService',
location: 'GasStation',
function: 'getGasPrice',
error: error.message
});
Expand Down
12 changes: 6 additions & 6 deletions src/services/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class TransactionService {

async getPendingTransactions(): Promise<ITransaction[]> {
Logger.info({
location: 'AutoClaimService',
location: 'TransactionService',
function: 'getPendingTransactions',
call: 'started'
})
Expand All @@ -41,14 +41,14 @@ export default class TransactionService {
}
} catch (error: any) {
Logger.error({
location: 'AutoClaimService',
location: 'TransactionService',
function: 'getPendingTransactions',
error: error.message
});
}

Logger.info({
location: 'AutoClaimService',
location: 'TransactionService',
function: 'getPendingTransactions',
call: 'completed',
length: transactions.length
Expand All @@ -59,7 +59,7 @@ export default class TransactionService {

async getProof(sourceNetwork: number, depositCount: number): Promise<IProof | null> {
Logger.info({
location: 'AutoClaimService',
location: 'TransactionService',
function: 'getProof',
call: 'started',
data: {
Expand All @@ -86,7 +86,7 @@ export default class TransactionService {
}
} catch (error: any) {
Logger.error({
location: 'AutoClaimService',
location: 'TransactionService',
function: 'getProof',
error: error.message,
data: {
Expand All @@ -96,7 +96,7 @@ export default class TransactionService {
});
}
Logger.info({
location: 'AutoClaimService',
location: 'TransactionService',
function: 'getProof',
call: 'completed'
})
Expand Down

0 comments on commit 6af4a5f

Please sign in to comment.