Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: separate permit #455

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ export function ERC20Validator(

amountGtThan0OrMinus1(target, propertyName, arguments);

amount0OrPositiveValidator(target, propertyName, arguments);

return method.apply(this, arguments);
};
}
Expand Down
211 changes: 210 additions & 1 deletion packages/contract-helpers/src/erc20-2612/erc20_2612.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber, providers } from 'ethers';
import { BigNumber, constants, providers } from 'ethers';
import { valueToWei } from '../commons/utils';
import { IERC202612 } from './typechain/IERC202612';
import { IERC202612__factory } from './typechain/IERC202612__factory';
import { ERC20_2612Interface, ERC20_2612Service } from './index';
Expand Down Expand Up @@ -78,4 +79,212 @@ describe('ERC20_2612', () => {
);
});
});

describe('signERC20Approval', () => {
const user = '0x0000000000000000000000000000000000000006';
const reserve = '0x0000000000000000000000000000000000000007';
const spender = '0x0000000000000000000000000000000000000008';
const amount = '123.456';
const decimals = 18;
const deadline = Math.round(Date.now() / 1000 + 3600).toString();
jest.spyOn(provider, 'getTransactionCount').mockResolvedValue(1);
jest.spyOn(provider, 'getNetwork').mockResolvedValue({
name: 'mainnet',
chainId: 1,
});

afterEach(() => {
jest.clearAllMocks();
});

it('Expects the permission string to be returned when all params', async () => {
const instance = new ERC20_2612Service(provider);

jest.spyOn(instance.erc20Service, 'getTokenData').mockReturnValue(
Promise.resolve({
name: 'mockToken',
decimals,
symbol: 'MT',
address: '0x0000000000000000000000000000000000000006',
}),
);

jest
.spyOn(instance.erc20Service, 'isApproved')
.mockReturnValue(Promise.resolve(false));

jest.spyOn(instance, 'getNonce').mockReturnValue(Promise.resolve(1));
const signature: string = await instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { primaryType, domain, message } = await JSON.parse(signature);
expect(primaryType).toEqual('Permit');
expect(domain.name).toEqual('mockToken');
expect(domain.chainId).toEqual(1);

expect(message.owner).toEqual(user);
expect(message.spender).toEqual(spender);
expect(message.value).toEqual(valueToWei(amount, decimals));
expect(message.nonce).toEqual(1);
expect(message.deadline).toEqual(deadline);
});
it('Expects the permission string to be returned when all params and amount -1', async () => {
const instance = new ERC20_2612Service(provider);

jest.spyOn(instance.erc20Service, 'getTokenData').mockReturnValue(
Promise.resolve({
name: 'mockToken',
decimals,
symbol: 'MT',
address: '0x0000000000000000000000000000000000000006',
}),
);

jest
.spyOn(instance.erc20Service, 'isApproved')
.mockReturnValue(Promise.resolve(false));

jest.spyOn(instance, 'getNonce').mockReturnValue(Promise.resolve(1));

const amount = '-1';
const signature: string = await instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { primaryType, domain, message } = await JSON.parse(signature);
expect(primaryType).toEqual('Permit');
expect(domain.name).toEqual('mockToken');
expect(domain.chainId).toEqual(1);

expect(message.owner).toEqual(user);
expect(message.spender).toEqual(spender);
expect(message.value).toEqual(constants.MaxUint256.toString());
expect(message.nonce).toEqual(1);
expect(message.deadline).toEqual(deadline);
});
it('Expects the permission string to be `` when no nonce', async () => {
const instance = new ERC20_2612Service(provider);

jest.spyOn(instance.erc20Service, 'getTokenData').mockReturnValue(
Promise.resolve({
name: 'mockToken',
decimals,
symbol: 'MT',
address: '0x0000000000000000000000000000000000000006',
}),
);

jest
.spyOn(instance.erc20Service, 'isApproved')
.mockReturnValue(Promise.resolve(false));
jest.spyOn(instance, 'getNonce').mockReturnValue(Promise.resolve(null));

const signature: string = await instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
});

expect(signature).toEqual('');
});
it('Expects the permission string to be `` when already approved', async () => {
const instance = new ERC20_2612Service(provider);

jest.spyOn(instance.erc20Service, 'getTokenData').mockReturnValue(
Promise.resolve({
name: 'mockToken',
decimals,
symbol: 'MT',
address: '0x0000000000000000000000000000000000000006',
}),
);

jest
.spyOn(instance.erc20Service, 'isApproved')
.mockReturnValue(Promise.resolve(true));

const signature: string = await instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
});

expect(signature).toEqual('');
});
it('Expects to fail when user not eth address', async () => {
const instance = new ERC20_2612Service(provider);

const user = 'asdf';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
}),
).rejects.toThrowError(
`Address: ${user} is not a valid ethereum Address`,
);
});
it('Expects to fail when reserve not eth address', async () => {
const instance = new ERC20_2612Service(provider);

const reserve = 'asdf';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
}),
).rejects.toThrowError(
`Address: ${reserve} is not a valid ethereum Address`,
);
});
it('Expects to fail when amount not positive', async () => {
const instance = new ERC20_2612Service(provider);

const amount = '0';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
}),
).rejects.toThrowError(`Amount: ${amount} needs to be greater than 0`);
});
it('Expects to fail when amount not number', async () => {
const instance = new ERC20_2612Service(provider);

const amount = 'asdf';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
spender,
amount,
deadline,
}),
).rejects.toThrowError(`Amount: ${amount} needs to be greater than 0`);
});
});
});
95 changes: 92 additions & 3 deletions packages/contract-helpers/src/erc20-2612/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import { BigNumber, providers } from 'ethers';
import { BigNumber, constants, providers } from 'ethers';
import BaseService from '../commons/BaseService';
import { tEthereumAddress } from '../commons/types';
import { valueToWei } from '../commons/utils';
import { ERC20Validator } from '../commons/validators/methodValidators';
import { isEthAddress } from '../commons/validators/paramValidators';
import {
isEthAddress,
isPositiveOrMinusOneAmount,
} from '../commons/validators/paramValidators';
import { ERC20Service, IERC20ServiceInterface } from '../erc20-contract';
import { IERC202612 } from './typechain/IERC202612';
import { IERC202612__factory } from './typechain/IERC202612__factory';

export type GetNonceType = { token: string; owner: string };
export type SignERC20ApprovalType = {
user: tEthereumAddress;
reserve: tEthereumAddress;
spender: tEthereumAddress;
amount: string;
deadline: string;
};

export interface ERC20_2612Interface {
getNonce: (args: GetNonceType) => Promise<number | null>;
signERC20Approval: (args: SignERC20ApprovalType) => Promise<string>;
}

export class ERC20_2612Service
extends BaseService<IERC202612>
implements ERC20_2612Interface
{
readonly erc20Service: IERC20ServiceInterface;

constructor(provider: providers.Provider) {
super(provider, IERC202612__factory);

this.erc20Service = new ERC20Service(provider);
this.getNonce = this.getNonce.bind(this);
}

Expand Down Expand Up @@ -45,4 +61,77 @@ export class ERC20_2612Service

return null;
}

// Sign permit supply
@ERC20Validator
public async signERC20Approval(
@isEthAddress('user')
@isEthAddress('reserve')
@isEthAddress('spender')
@isPositiveOrMinusOneAmount('amount')
{ user, reserve, spender, amount, deadline }: SignERC20ApprovalType,
): Promise<string> {
const { getTokenData, isApproved } = this.erc20Service;
const { name, decimals } = await getTokenData(reserve);

const convertedAmount =
amount === '-1'
? constants.MaxUint256.toString()
: valueToWei(amount, decimals);

const approved = await isApproved({
token: reserve,
user,
spender,
amount,
});

if (approved) {
return '';
}

const { chainId } = await this.provider.getNetwork();

const nonce = await this.getNonce({
token: reserve,
owner: user,
});

if (nonce === null) {
return '';
}

const typeData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Permit: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
],
},
primaryType: 'Permit',
domain: {
name,
version: '1',
chainId,
verifyingContract: reserve,
},
message: {
owner: user,
spender,
value: convertedAmount,
nonce,
deadline,
},
};
return JSON.stringify(typeData);
}
}
1 change: 1 addition & 0 deletions packages/contract-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './uiStakeDataProvider-contract';
export * from './incentive-controller';
export * from './incentive-controller-v2';
export * from './erc20-contract';
export * from './erc20-2612';
export * from './lendingPool-contract';
export * from './faucet-contract';
export * from './staking-contract';
Expand Down
Loading