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

feat: approval type addition #456

Draft
wants to merge 3 commits into
base: chore/separate-permit
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions packages/contract-helpers/src/commons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ export type EthereumTransactionTypeExtended = {
gas: GasResponse;
};

/**
* Extension of EthereumTransactionTypeExtended which allow building a permit with the raw parameters
*/
export type Erc20ApprovalTransactionType = EthereumTransactionTypeExtended & {
params: {
owner: tEthereumAddress;
spender: tEthereumAddress;
token: tEthereumAddress;
amount: string;
};
};

export type TransactionGenerationMethod = {
rawTxMethod: () => Promise<PopulatedTransaction>;
from: tEthereumAddress;
Expand Down
2 changes: 0 additions & 2 deletions packages/contract-helpers/src/commons/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export const getTxValue = (reserve: string, amount: string): string => {

export const DEFAULT_NULL_VALUE_ON_TX = BigNumber.from(0).toHexString();
export const DEFAULT_APPROVE_AMOUNT = constants.MaxUint256.toString();
export const MAX_UINT_AMOUNT =
'115792089237316195423570985008687907853269984665640564039457584007913129639935';
export const SUPER_BIG_ALLOWANCE_NUMBER =
'11579208923731619542357098500868790785326998466564056403945758400791';
export const API_ETH_MOCK_ADDRESS =
Expand Down
37 changes: 18 additions & 19 deletions packages/contract-helpers/src/erc20-2612/erc20_2612.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 @@ -84,7 +83,7 @@ describe('ERC20_2612', () => {
const user = '0x0000000000000000000000000000000000000006';
const reserve = '0x0000000000000000000000000000000000000007';
const spender = '0x0000000000000000000000000000000000000008';
const amount = '123.456';
const amount = '123456000000000000000000';
const decimals = 18;
const deadline = Math.round(Date.now() / 1000 + 3600).toString();
jest.spyOn(provider, 'getTransactionCount').mockResolvedValue(1);
Expand Down Expand Up @@ -115,8 +114,8 @@ describe('ERC20_2612', () => {

jest.spyOn(instance, 'getNonce').mockReturnValue(Promise.resolve(1));
const signature: string = await instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand All @@ -130,7 +129,7 @@ describe('ERC20_2612', () => {

expect(message.owner).toEqual(user);
expect(message.spender).toEqual(spender);
expect(message.value).toEqual(valueToWei(amount, decimals));
expect(message.value).toEqual(amount);
expect(message.nonce).toEqual(1);
expect(message.deadline).toEqual(deadline);
});
Expand All @@ -154,8 +153,8 @@ describe('ERC20_2612', () => {

const amount = '-1';
const signature: string = await instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand Down Expand Up @@ -191,8 +190,8 @@ describe('ERC20_2612', () => {
jest.spyOn(instance, 'getNonce').mockReturnValue(Promise.resolve(null));

const signature: string = await instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand All @@ -217,8 +216,8 @@ describe('ERC20_2612', () => {
.mockReturnValue(Promise.resolve(true));

const signature: string = await instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand All @@ -232,8 +231,8 @@ describe('ERC20_2612', () => {
const user = 'asdf';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand All @@ -248,8 +247,8 @@ describe('ERC20_2612', () => {
const reserve = 'asdf';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand All @@ -264,8 +263,8 @@ describe('ERC20_2612', () => {
const amount = '0';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand All @@ -278,8 +277,8 @@ describe('ERC20_2612', () => {
const amount = 'asdf';
await expect(async () =>
instance.signERC20Approval({
user,
reserve,
owner: user,
token: reserve,
spender,
amount,
deadline,
Expand Down
50 changes: 19 additions & 31 deletions packages/contract-helpers/src/erc20-2612/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BigNumber, constants, providers } from 'ethers';
import { BigNumber, providers, constants } 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,
Expand All @@ -13,8 +12,9 @@ import { IERC202612__factory } from './typechain/IERC202612__factory';

export type GetNonceType = { token: string; owner: string };
export type SignERC20ApprovalType = {
user: tEthereumAddress;
reserve: tEthereumAddress;
owner: tEthereumAddress;
token: tEthereumAddress;
tokenName: string;
spender: tEthereumAddress;
amount: string;
deadline: string;
Expand Down Expand Up @@ -65,36 +65,24 @@ export class ERC20_2612Service
// Sign permit supply
@ERC20Validator
public async signERC20Approval(
@isEthAddress('user')
@isEthAddress('reserve')
@isEthAddress('owner')
@isEthAddress('token')
@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,
{
owner,
token,
spender,
amount,
});

if (approved) {
return '';
}

deadline,
tokenName,
}: SignERC20ApprovalType,
): Promise<string> {
const { chainId } = await this.provider.getNetwork();

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

if (nonce === null) {
Expand All @@ -119,15 +107,15 @@ export class ERC20_2612Service
},
primaryType: 'Permit',
domain: {
name,
name: tokenName,
version: '1',
chainId,
verifyingContract: reserve,
verifyingContract: token,
},
message: {
owner: user,
owner,
spender,
value: convertedAmount,
value: amount === '-1' ? constants.MaxUint256.toString() : amount,
nonce,
deadline,
},
Expand Down
12 changes: 9 additions & 3 deletions packages/contract-helpers/src/erc20-contract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BigNumber, providers } from 'ethers';
import BaseService from '../commons/BaseService';
import {
eEthereumTxType,
EthereumTransactionTypeExtended,
Erc20ApprovalTransactionType,
tEthereumAddress,
transactionType,
} from '../commons/types';
Expand All @@ -24,7 +24,7 @@ export interface IERC20ServiceInterface {
decimalsOf: (token: tEthereumAddress) => Promise<number>;
getTokenData: (token: tEthereumAddress) => Promise<TokenMetadataType>;
isApproved: (args: ApproveType) => Promise<boolean>;
approve: (args: ApproveType) => EthereumTransactionTypeExtended;
approve: (args: ApproveType) => Erc20ApprovalTransactionType;
}

export type ApproveType = {
Expand Down Expand Up @@ -66,7 +66,7 @@ export class ERC20Service
@isEthAddress('spender')
@isPositiveAmount('amount')
{ user, token, spender, amount }: ApproveType,
): EthereumTransactionTypeExtended {
): Erc20ApprovalTransactionType {
const erc20Contract: IERC20Detailed = this.getContractInstance(token);

const txCallback: () => Promise<transactionType> = this.generateTxCallback({
Expand All @@ -79,6 +79,12 @@ export class ERC20Service
tx: txCallback,
txType: eEthereumTxType.ERC20_APPROVAL,
gas: this.generateTxPriceEstimation([], txCallback),
params: {
owner: user,
spender,
amount,
token,
},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ describe('LendingPool', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
});
const synthetixSpy = jest
.spyOn(lendingPoolInstance.synthetixService, 'synthetixValidation')
Expand Down Expand Up @@ -1018,6 +1024,12 @@ describe('LendingPool', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
});
const synthetixSpy = jest
.spyOn(lendingPoolInstance.synthetixService, 'synthetixValidation')
Expand Down Expand Up @@ -1083,6 +1095,12 @@ describe('LendingPool', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
});
const synthetixSpy = jest
.spyOn(lendingPoolInstance.synthetixService, 'synthetixValidation')
Expand Down Expand Up @@ -1609,6 +1627,12 @@ describe('LendingPool', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
});

const liquidationCallTxObj = await lendingPoolInstance.liquidationCall({
Expand Down Expand Up @@ -1821,6 +1845,12 @@ describe('LendingPool', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
});
const swapCollateralTxObj = await lendingPoolInstance.swapCollateral({
user,
Expand Down Expand Up @@ -2541,6 +2571,12 @@ describe('LendingPool', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
});

const repayWithCollateralTxObj =
Expand Down Expand Up @@ -3885,6 +3921,12 @@ describe('LendingPool', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
});

const repayWithCollateralTxObj =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ describe('StakingService', () => {
gasLimit: '1',
gasPrice: '1',
}),
params: {
amount: '0',
owner: '0x0',
spender: '0x0',
token: '0x0',
},
}));

const stakeTxObj = await instance.stake(user, amount);
Expand Down
Loading