Skip to content

Commit

Permalink
handle transaction count for all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrap committed Jan 27, 2024
1 parent 5aa7dfe commit 9b200db
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions services/delegation/src/modules/blockchain/blockchain.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class BlockchainService {
walletKey: string,
public defaultDelegationAmount: ethers.BigNumber,
private ownNodes: string[],
private txCount = 0,
) {
this.provider = new ethers.providers.JsonRpcProvider({
url: endpoint,
Expand Down Expand Up @@ -78,7 +79,7 @@ export class BlockchainService {
await this.rebalanceOwnNodes(true);

try {
const tx = await this.contract.registerValidator(
await this.contract.registerValidator(
address,
addressProof,
vrfKey,
Expand All @@ -88,6 +89,7 @@ export class BlockchainService {
{
gasPrice: this.provider.getGasPrice(),
value: this.defaultDelegationAmount,
nonce: await this.getNonce(),
},
);
return true;
Expand Down Expand Up @@ -213,15 +215,12 @@ export class BlockchainService {

console.log(`Delegating ${toDelegate} to ${ownNode.address}`);

let txCount = await this.wallet.getTransactionCount();

try {
await this.contract.delegate(ownNode.address, {
gasPrice: this.provider.getGasPrice(),
value: toDelegate,
nonce: txCount,
nonce: await this.getNonce(),
});
txCount++;
} catch (e) {
console.error(
`Can't delegate to own nodes - delegation call failed for node ${ownNode.address}`,
Expand Down Expand Up @@ -249,4 +248,13 @@ export class BlockchainService {
}
return validators;
}

private async getNonce() {
if (this.txCount === 0) {
this.txCount = await this.wallet.getTransactionCount();
}
const nonce = this.txCount;
this.txCount++;
return nonce;
}
}

0 comments on commit 9b200db

Please sign in to comment.