Skip to content

Commit

Permalink
new: compress claims
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinmittal23 committed May 8, 2024
1 parent e2375b1 commit 2dbb3bc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 57 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@maticnetwork/chain-indexer-framework": "^1.3.5",
"axios": "^1.6.8",
"dotenv": "^16.0.1",
"ethers": "^6.12.0",
"ethers": "^6.12.1",
"long": "^5.2.0"
},
"devDependencies": {
Expand Down
96 changes: 47 additions & 49 deletions src/services/auto-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,44 @@ export default class AutoClaimService {
}

async estimateGas(transaction: ITransaction, proof: IProof, globalIndex: BigInt): Promise<boolean> {
let compressedClaimCalls = null;
try {
if (transaction.dataType === 'ERC20') {
await this.contract.estimateGas.compressClaimCall(
compressedClaimCalls = await this.contract.compressClaimCall(
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
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
}]
)

} else {
await this.contract.estimateGas.compressClaimCall(
compressedClaimCalls = await this.contract.compressClaimCall.estimateGas(
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
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
}]
)
}

await this.contract.sendCompressedClaims.estimateGas(compressedClaimCalls);
return true;
} catch (error: any) {
if (this.slackNotify) {
Expand All @@ -89,7 +92,7 @@ export default class AutoClaimService {

async claim(batch: { transaction: ITransaction, proof: IProof, globalIndex: BigInt }[]): Promise<ethers.TransactionResponse | null> {
const gasPrice = await this.gasStation.getGasPrice();
let tx: ethers.TransactionResponse | null = null;
let response: ethers.TransactionResponse | null = null;
try {
Logger.info({
type: 'claimBatch',
Expand All @@ -102,47 +105,48 @@ export default class AutoClaimService {
for (const tx of batch) {
if (tx.transaction.dataType === 'ERC20') {
data.push({
SmtProofLocalExitRoot: tx.proof.merkle_proof,
SmtProofRollupExitRoot: tx.proof.rollup_merkle_proof,
GlobalIndex: tx.globalIndex.toString(),
OriginNetwork: tx.transaction.originTokenNetwork,
OriginAddress: tx.transaction.originTokenAddress,
DestinationAddress: tx.transaction.receiver,
Amount: tx.transaction.amounts ? tx.transaction.amounts[0] : '0',
Metadata: tx.transaction.metadata && tx.transaction.metadata !== "" ? tx.transaction.metadata : '0x',
IsMessage: false
smtProofLocalExitRoot: tx.proof.merkle_proof,
smtProofRollupExitRoot: tx.proof.rollup_merkle_proof,
globalIndex: tx.globalIndex.toString(),
originNetwork: tx.transaction.originTokenNetwork,
originAddress: tx.transaction.originTokenAddress,
destinationAddress: tx.transaction.receiver,
amount: tx.transaction.amounts ? tx.transaction.amounts[0] : '0',
metadata: tx.transaction.metadata && tx.transaction.metadata !== "" ? tx.transaction.metadata : '0x',
isMessage: false
})
} else {
data.push({
SmtProofLocalExitRoot: tx.proof.merkle_proof,
SmtProofRollupExitRoot: tx.proof.rollup_merkle_proof,
GlobalIndex: tx.globalIndex.toString(),
OriginNetwork: '0',
OriginAddress: tx.transaction.transactionInitiator,
DestinationAddress: tx.transaction.receiver,
Amount: tx.transaction.originTokenAddress ? '0' : tx.transaction.amounts ? tx.transaction.amounts[0] : '0',
Metadata: tx.transaction.metadata && tx.transaction.metadata !== "" ? tx.transaction.metadata : '0x',
IsMessage: true
smtProofLocalExitRoot: tx.proof.merkle_proof,
smtProofRollupExitRoot: tx.proof.rollup_merkle_proof,
globalIndex: tx.globalIndex.toString(),
originNetwork: '0',
originAddress: tx.transaction.transactionInitiator,
destinationAddress: tx.transaction.receiver,
amount: tx.transaction.originTokenAddress ? '0' : tx.transaction.amounts ? tx.transaction.amounts[0] : '0',
metadata: tx.transaction.metadata && tx.transaction.metadata !== "" ? tx.transaction.metadata : '0x',
isMessage: true
})
}
}

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

Logger.info({
type: 'claimBatch',
status: 'success',
claimTransactionHash: transaction.hash
claimTransactionHash: response?.hash
})
} catch (error: any) {
Logger.error({ error })
}
return tx;
return response;
}

async claimTransactions() {
Expand Down Expand Up @@ -177,12 +181,6 @@ export default class AutoClaimService {
await this.claim(batch);
}

const remaining = length % 5;
if (remaining > 0) {
const lastBatch = finalClaimableTransaction.slice(-remaining);
await this.claim(lastBatch);
}

Logger.info({
location: 'AutoClaimService',
function: 'claimTransactions',
Expand Down

0 comments on commit 2dbb3bc

Please sign in to comment.