Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Feature/upgrade to ethers v6 #25

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"license": "MIT",
"dependencies": {
"ethers": "^5.3.1",
"ethers": "^6.11.1",
"tiny-invariant": "^1.3.1"
},
"devDependencies": {
Expand Down
56 changes: 28 additions & 28 deletions src/allowanceTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ describe('AllowanceTransfer', () => {
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: MaxAllowanceTransferAmount.toString(),
expiration: MaxAllowanceExpiration.toString(),
nonce: MaxOrderedNonce.toString(),
amount: MaxAllowanceTransferAmount,
expiration: MaxAllowanceExpiration,
nonce: MaxOrderedNonce,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: MaxSigDeadline.toString(),
sigDeadline: MaxSigDeadline,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -28,12 +28,12 @@ describe('AllowanceTransfer', () => {
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: MaxOrderedNonce.add(1).toString(),
amount: 0n,
expiration: 0n,
nonce: MaxOrderedNonce + 1n,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
sigDeadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -47,12 +47,12 @@ describe('AllowanceTransfer', () => {
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: MaxAllowanceTransferAmount.add(1).toString(),
expiration: '0',
nonce: 0,
amount: MaxAllowanceTransferAmount + 1n,
expiration: 0n,
nonce: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
sigDeadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -66,12 +66,12 @@ describe('AllowanceTransfer', () => {
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: MaxAllowanceExpiration.add(1).toString(),
nonce: 0,
amount: 0n,
expiration: MaxAllowanceExpiration + 1n,
nonce: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
sigDeadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -85,12 +85,12 @@ describe('AllowanceTransfer', () => {
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: 0,
amount: 0n,
expiration: 0n,
nonce: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: MaxSigDeadline.add(1).toString(),
sigDeadline: MaxSigDeadline + 1n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -105,12 +105,12 @@ describe('AllowanceTransfer', () => {
{
details: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: 0,
amount: 0n,
expiration: 0n,
nonce: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
sigDeadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -125,13 +125,13 @@ describe('AllowanceTransfer', () => {
details: [
{
token: '0x0000000000000000000000000000000000000000',
amount: '0',
expiration: '0',
nonce: 0,
amount: 0n,
expiration: 0n,
nonce: 0n,
},
],
spender: '0x0000000000000000000000000000000000000000',
sigDeadline: '0',
sigDeadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand Down
25 changes: 12 additions & 13 deletions src/allowanceTransfer.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import invariant from 'tiny-invariant'
import { TypedDataDomain, TypedDataField } from '@ethersproject/abstract-signer'
import { BigNumberish } from '@ethersproject/bignumber'
import { _TypedDataEncoder } from '@ethersproject/hash'
import { TypedDataDomain, TypedDataField } from 'ethers'
import { TypedDataEncoder } from 'ethers'
import { MaxSigDeadline, MaxOrderedNonce, MaxAllowanceTransferAmount, MaxAllowanceExpiration } from './constants'
import { permit2Domain } from './domain'

export interface PermitDetails {
token: string
amount: BigNumberish
expiration: BigNumberish
nonce: BigNumberish
amount: bigint
expiration: bigint
nonce: bigint
}

export interface PermitSingle {
details: PermitDetails
spender: string
sigDeadline: BigNumberish
sigDeadline: bigint
}

export interface PermitBatch {
details: PermitDetails[]
spender: string
sigDeadline: BigNumberish
sigDeadline: bigint
}

export type PermitSingleData = {
Expand Down Expand Up @@ -78,7 +77,7 @@ export abstract class AllowanceTransfer {
permit2Address: string,
chainId: number
): PermitSingleData | PermitBatchData {
invariant(MaxSigDeadline.gte(permit.sigDeadline), 'SIG_DEADLINE_OUT_OF_RANGE')
invariant(MaxSigDeadline >= permit.sigDeadline, 'SIG_DEADLINE_OUT_OF_RANGE');

const domain = permit2Domain(permit2Address, chainId)
if (isPermit(permit)) {
Expand All @@ -100,12 +99,12 @@ export abstract class AllowanceTransfer {

public static hash(permit: PermitSingle | PermitBatch, permit2Address: string, chainId: number): string {
const { domain, types, values } = AllowanceTransfer.getPermitData(permit, permit2Address, chainId)
return _TypedDataEncoder.hash(domain, types, values)
return TypedDataEncoder.hash(domain, types, values)
}
}

function validatePermitDetails(details: PermitDetails) {
invariant(MaxOrderedNonce.gte(details.nonce), 'NONCE_OUT_OF_RANGE')
invariant(MaxAllowanceTransferAmount.gte(details.amount), 'AMOUNT_OUT_OF_RANGE')
invariant(MaxAllowanceExpiration.gte(details.expiration), 'EXPIRATION_OUT_OF_RANGE')
invariant(MaxOrderedNonce >= details.nonce, 'NONCE_OUT_OF_RANGE');
invariant(MaxAllowanceTransferAmount >= details.amount, 'AMOUNT_OUT_OF_RANGE');
invariant(MaxAllowanceExpiration >= details.expiration, 'EXPIRATION_OUT_OF_RANGE');
}
19 changes: 9 additions & 10 deletions src/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { BigNumber } from '@ethersproject/bignumber'
import { MaxUint48, MaxUint160, MaxUint256, InstantExpiration } from './constants'

describe('Constants', () => {
it('MaxUint256', () => {
expect(MaxUint256).toEqual(BigNumber.from(2).pow(256).sub(1))
})
expect(MaxUint256).toEqual((2n ** 256n) - 1n);
});

it('MaxUint160', () => {
expect(MaxUint160).toEqual(BigNumber.from(2).pow(160).sub(1))
})
expect(MaxUint160).toEqual((2n ** 160n) - 1n);
});

it('MaxUint48', () => {
expect(MaxUint48).toEqual(BigNumber.from(2).pow(48).sub(1))
})
expect(MaxUint48).toEqual((2n ** 48n) - 1n);
});

it('InstantExpiration', () => {
expect(InstantExpiration).toEqual(BigNumber.from(0))
})
})
expect(InstantExpiration).toEqual(0n);
});
})
11 changes: 5 additions & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { BigNumber } from '@ethersproject/bignumber'

export const PERMIT2_ADDRESS = '0x000000000022D473030F116dDEE9F6B43aC78BA3'

export const MaxUint48 = BigNumber.from('0xffffffffffff')
export const MaxUint160 = BigNumber.from('0xffffffffffffffffffffffffffffffffffffffff')
export const MaxUint256 = BigNumber.from('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
export const MaxUint48 = BigInt('0xffffffffffff')
export const MaxUint160 =BigInt('0xffffffffffffffffffffffffffffffffffffffff')
export const MaxUint256 =BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')

// alias max types for their usages
// allowance transfer types
Expand All @@ -17,4 +15,5 @@ export const MaxSignatureTransferAmount = MaxUint256
export const MaxUnorderedNonce = MaxUint256
export const MaxSigDeadline = MaxUint256

export const InstantExpiration: BigNumber = BigNumber.from(0)
export const InstantExpiration = BigInt(0)

2 changes: 1 addition & 1 deletion src/domain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypedDataDomain, TypedDataField } from '@ethersproject/abstract-signer'
import { TypedDataDomain, TypedDataField } from 'ethers'

const PERMIT2_DOMAIN_NAME = 'Permit2'

Expand Down
8 changes: 3 additions & 5 deletions src/providers/AllowanceProvider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { BigNumber } from '@ethersproject/bignumber'
import { Provider } from '@ethersproject/providers'
import { Contract, Provider } from 'ethers'
import Permit2Abi from '../../abis/Permit2.json'
import { Contract } from '@ethersproject/contracts'

export interface AllowanceData {
amount: BigNumber
amount: bigint
nonce: number
expiration: number
}
Expand All @@ -20,7 +18,7 @@ export class AllowanceProvider {
return await this.permit2.allowance(owner, token, spender)
}

async getAllowance(token: string, owner: string, spender: string): Promise<BigNumber> {
async getAllowance(token: string, owner: string, spender: string): Promise<bigint> {
return (await this.getAllowanceData(token, owner, spender)).amount
}

Expand Down
48 changes: 24 additions & 24 deletions src/signatureTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ describe('SignatureTransfer', () => {
{
permitted: {
token: '0x0000000000000000000000000000000000000000',
amount: MaxSignatureTransferAmount.toString(),
amount: MaxSignatureTransferAmount,
},
spender: '0x0000000000000000000000000000000000000000',
nonce: MaxUnorderedNonce.toString(),
deadline: MaxSigDeadline.toString(),
nonce: MaxUnorderedNonce,
deadline: MaxSigDeadline,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -27,11 +27,11 @@ describe('SignatureTransfer', () => {
{
permitted: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
amount: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
nonce: MaxUnorderedNonce.add(1).toString(),
deadline: '0',
nonce: MaxUnorderedNonce + 1n,
deadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -45,11 +45,11 @@ describe('SignatureTransfer', () => {
{
permitted: {
token: '0x0000000000000000000000000000000000000000',
amount: MaxSignatureTransferAmount.add(1).toString(),
amount: MaxSignatureTransferAmount + 1n,
},
spender: '0x0000000000000000000000000000000000000000',
nonce: '0',
deadline: '0',
nonce: 0n,
deadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -63,11 +63,11 @@ describe('SignatureTransfer', () => {
{
permitted: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
amount: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
nonce: '0',
deadline: MaxSigDeadline.add(1).toString(),
nonce: 0n,
deadline: MaxSigDeadline + 1n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -82,11 +82,11 @@ describe('SignatureTransfer', () => {
{
permitted: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
amount: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
nonce: '0',
deadline: '0',
nonce: 0n,
deadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -100,11 +100,11 @@ describe('SignatureTransfer', () => {
{
permitted: {
token: '0x0000000000000000000000000000000000000000',
amount: '0',
amount: 0n,
},
spender: '0x0000000000000000000000000000000000000000',
nonce: '0',
deadline: '0',
nonce: 0n,
deadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1,
Expand All @@ -124,12 +124,12 @@ describe('SignatureTransfer', () => {
permitted: [
{
token: '0x0000000000000000000000000000000000000000',
amount: '0',
amount: 0n,
},
],
spender: '0x0000000000000000000000000000000000000000',
nonce: '0',
deadline: '0',
nonce: 0n,
deadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1
Expand All @@ -144,12 +144,12 @@ describe('SignatureTransfer', () => {
permitted: [
{
token: '0x0000000000000000000000000000000000000000',
amount: '0',
amount: 0n,
},
],
spender: '0x0000000000000000000000000000000000000000',
nonce: '0',
deadline: '0',
nonce: 0n,
deadline: 0n,
},
'0x0000000000000000000000000000000000000000',
1,
Expand Down
Loading