Repository navigation
feat: permit flow integration - #236
Conversation
|
I think we can significantly improve the maintainability by refactoring the duplicated logic. The initial validation and the signature generation code are repeated across the new functions. I suggest creating two private helper methods:
private async _prepareDeposit(
caller: 'deposit' | 'depositWithPermit' | 'depositWithPermitAndApproveOperator',
amount: TokenAmount,
token: TokenIdentifier) {
if (token !== TOKENS.USDFC) {
throw createError('PaymentsService', caller, `Unsupported token: ${token}`)
}
const depositAmountBigint = typeof amount === 'bigint' ? amount : BigInt(amount)
if (depositAmountBigint <= 0n) {
throw createError('PaymentsService', caller, 'Invalid amount')
}
const signerAddress = await this._signer.getAddress()
const usdfcContract = this._getUsdfcContract()
// Check balance
const usdfcBalance = await usdfcContract.balanceOf(signerAddress)
if (usdfcBalance < depositAmountBigint) {
throw createError(
'PaymentsService',
caller,
`Insufficient USDFC: have ${usdfcBalance.toString()}, need ${depositAmountBigint.toString()}`
)
}
return {
depositAmountBigint,
signerAddress,
paymentsContract: this._getPaymentsContract(),
}
}
private async _createDepositPermitSignature(caller: string, usdfcContract: ethers.Contract, signerAddress: string, depositAmountBigint: bigint, deadline?: number | bigint) {
// ... implementation for creating and returning the signature {v, r, s, permitDeadline}
}By doing this, our main functions will become very clean and focused, like this: async depositWithPermit(amount: TokenAmount, token: TokenIdentifier, deadline?: number | bigint) {
const { depositAmountBigint, signerAddress, usdfcContract, paymentsContract } =
await this._prepareDeposit('depositWithPermit', amount, token);
const { v, r, s, permitDeadline } =
await this._createDepositPermitSignature('depositWithPermit', usdfcContract, signerAddress, depositAmountBigint, deadline);
return paymentsContract.depositWithPermit(/* ...args */);
}Also, it might be a good time to consider adding a simpler callback structure (e.g., an |
e59bbb7 to
eb19251
Compare
| }, | ||
| { | ||
| target: this._usdfcAddress, | ||
| allowFailure: false, |
There was a problem hiding this comment.
since you don't allowFailure for some of these, you need to check the length of the results.
|
|
||
| const depositAmountBigint = typeof amount === 'bigint' ? amount : BigInt(amount) | ||
| if (depositAmountBigint <= 0n) { | ||
| throw createError('PaymentsService', 'depositWithPermit', 'Invalid amount') |
There was a problem hiding this comment.
this error can say the amount was negative since that's what makes it invalid
| /** | ||
| * Deposit funds using ERC-2612 permit and approve an operator in a single transaction | ||
| * This signs an EIP-712 permit for the USDFC token and calls the Payments contract | ||
| * function `depositWithPermitAndApproveOperator` which both deposits and sets operator approval. |
There was a problem hiding this comment.
should document that this can throw an error if the user rejects the signature
|
|
||
| it('should deposit with permit', async () => { | ||
| const depositAmount = ethers.parseUnits('10', 18) | ||
| const tx = await payments.depositWithPermit(depositAmount) |
There was a problem hiding this comment.
should also test this with deadline being number and deadline being bigint.
There was a problem hiding this comment.
also depositWithPermitAndApproveOperator
Introduces support for ERC-2612 permit-based deposits in the payments system, allowing users to approve and deposit USDFC tokens in a single transaction using EIP-712 signatures. It adds new methods to the PaymentsService for permit-based deposits, updates contract ABIs and constants to support EIP-2612, and enhances the test suite and mocks to cover the new functionality. Co-authored-by: Shashank Trivedi <lordforever@Shashanks-MacBook-Pro.local>
This pull request introduces support for ERC-2612 permit-based deposits in the payments system, allowing users to approve and deposit USDFC tokens in a single transaction using EIP-712 signatures. It adds new methods to the
PaymentsServicefor permit-based deposits, updates contract ABIs and constants to support EIP-2612, and enhances the test suite and mocks to cover the new functionality.ERC-2612 Permit Support and Payments Enhancements:
erc20PermitAbifor readingnoncesandversionfrom ERC-20 permit tokens (src/abis/erc20-permit.ts).CONTRACT_ABISto includeERC20_PERMIT, and introducedEIP2612_PERMIT_TYPESfor EIP-712 typed data schema, as well as aPERMIT_DEADLINE_DURATIONconstant for permit expiry (src/utils/constants.ts). [1] [2] [3] [4]PaymentsService Additions:
_getPermitSignatureto generate EIP-2612 permit signatures, handling balance checks, domain creation, nonce retrieval, and signature generation using Multicall3 for efficient RPC batching (src/payments/service.ts).depositWithPermitanddepositWithPermitAndApproveOperatormethods to allow deposits and operator approvals using ERC-2612 permits in a single transaction, improving UX and reducing on-chain transactions (src/payments/service.ts).Testing and Mock Improvements:
depositWithPermitanddepositWithPermitAndApproveOperatorto ensure correct behavior and signature handling (src/test/payments.test.ts).src/test/test-utils.ts). [1] [2] [3]