Skip to content

Commit

Permalink
Merge pull request #152 from barebind/fix/nonce-validation-error
Browse files Browse the repository at this point in the history
Fix nonce validation error
  • Loading branch information
gitcoindev authored Feb 8, 2024
2 parents 81dcd74 + 40bfcaf commit 659ec67
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions static/scripts/rewards/web3/erc20-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,18 @@ export async function generateInvalidatePermitAdminControl(permit: Erc20Permit)
});
}

//mimics https://github.com/Uniswap/permit2/blob/a7cd186948b44f9096a35035226d7d70b9e24eaf/src/SignatureTransfer.sol#L150
export async function isNonceClaimed(permit: Erc20Permit): Promise<boolean> {
const provider = new ethers.providers.JsonRpcProvider(networkRpcs[permit.networkId]);
const permit2Contract = new ethers.Contract(permit2Address, permit2Abi, provider);

const { wordPos, bitPos } = nonceBitmap(BigNumber.from(permit.permit.nonce));
const bitmap = await permit2Contract.nonceBitmap(permit.owner, wordPos);

const bit = BigNumber.from(1).shl(bitPos).and(bitmap);
const bit = BigNumber.from(1).shl(bitPos);
const flipped = BigNumber.from(bitmap).xor(bit);

return bit.eq(0);
return bit.and(flipped).eq(0);
}

export async function invalidateNonce(signer: ethers.providers.JsonRpcSigner, nonce: BigNumberish): Promise<void> {
Expand All @@ -147,7 +149,7 @@ export async function invalidateNonce(signer: ethers.providers.JsonRpcSigner, no
await permit2Contract.invalidateUnorderedNonces(wordPos, mask);
}

// mimics https://github.com/Uniswap/permit2/blob/db96e06278b78123970183d28f502217bef156f4/src/SignatureTransfer.sol#L150
// mimics https://github.com/Uniswap/permit2/blob/db96e06278b78123970183d28f502217bef156f4/src/SignatureTransfer.sol#L142
export function nonceBitmap(nonce: BigNumberish): { wordPos: BigNumber; bitPos: number } {
// wordPos is the first 248 bits of the nonce
const wordPos = BigNumber.from(nonce).shr(8);
Expand Down

1 comment on commit 659ec67

@ubiquibot
Copy link

@ubiquibot ubiquibot bot commented on 659ec67 Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.