Skip to content

Commit

Permalink
Add depositPreApproved for PufLockerHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
9inpachi committed Jul 11, 2024
1 parent d20a33c commit b5daa78
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 12 deletions.
17 changes: 17 additions & 0 deletions lib/contracts/handlers/puf-locker-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ describe('PufTokenHandler', () => {
expect(maxLock).toBe(lockPeriods[1]);
});

it('should deposit the given pre-approved token into the locker', async () => {
contractTestingUtils.mockTransaction('deposit');
jest
.spyOn((handler as any).erc20PermitHandler, 'getPermitSignature')
.mockReturnValue(Promise.resolve(mockPermitSignature));

const { transact, estimate } = await handler.depositPreApproved(
Token.pufWETH,
mockAccount,
1n,
10n,
);

expect(typeof (await estimate())).toBe('bigint');
expect(isHash(await transact())).toBe(true);
});

it('should deposit the given token into the locker', async () => {
contractTestingUtils.mockTransaction('deposit');
jest
Expand Down
61 changes: 58 additions & 3 deletions lib/contracts/handlers/puf-locker-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletClient, PublicClient, getContract, Address } from 'viem';
import { WalletClient, PublicClient, getContract, Address, padHex } from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { PUF_LOCKER_ABIS } from '../abis/puf-locker-abis';
import { CHAIN_ADDRESSES } from '../addresses';
Expand Down Expand Up @@ -101,13 +101,68 @@ export class PufLockerHandler {
}

/**
* Deposit the given PufToken into the locker.
* Deposit the given pre-approved PufToken into the locker. A token
* can be pre-approved using `Token.approve()`, This doesn't make the
* transaction but returns two methods namely `transact` and
* `estimate`.
*
* @param pufToken PufToken to deposit.
* @param walletAddress Wallet address of the depositor.
* @param value Amount of the deposit.
* @param lockPeriod The period for the deposit in seconds.
* @returns The transaction hash of the deposit.
* @returns `transact: () => Promise<Address>` - Used to make the
* transaction.
*
* `estimate: () => Promise<bigint>` - Gas estimate of the
* transaction.
*/
public async depositPreApproved(
pufToken: PufToken,
walletAddress: Address,
value: bigint,
lockPeriod: bigint,
) {
const depositArgs = <const>[
TOKENS_ADDRESSES[pufToken][this.chain],
lockPeriod,
// Only `amount` is needed if `Token.approve()` is already called.
// So using mock values for other properties.
{
r: padHex('0x', { size: 32 }),
s: padHex('0x', { size: 32 }),
v: 0,
deadline: 0n,
amount: value,
},
];

const transact = () =>
this.getContract().write.deposit(depositArgs, {
account: walletAddress,
chain: this.viemChain,
});
const estimate = () =>
this.getContract().estimateGas.deposit(depositArgs, {
account: walletAddress,
});

return { transact, estimate };
}

/**
* Deposit the given PufToken into the locker. This doesn't make the
* transaction but returns two methods namely `transact` and
* `estimate`.
*
* @param pufToken PufToken to deposit.
* @param walletAddress Wallet address of the depositor.
* @param value Amount of the deposit.
* @param lockPeriod The period for the deposit in seconds.
* @returns `transact: () => Promise<Address>` - Used to make the
* transaction.
*
* `estimate: () => Promise<bigint>` - Gas estimate of the
* transaction.
*/
public async deposit(
pufToken: PufToken,
Expand Down
2 changes: 1 addition & 1 deletion lib/contracts/handlers/puffer-l2-depositor-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('PufferL2DepositorHandler', () => {
it('should deposit pre-approved token', async () => {
contractTestingUtils.mockTransaction('deposit');

const { transact, estimate } = handler.depositAfterApproval(
const { transact, estimate } = handler.depositPreApproved(
Token.stETH,
mockAccount,
10n,
Expand Down
16 changes: 8 additions & 8 deletions lib/contracts/handlers/puffer-l2-depositor-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export class PufferL2DepositorHandler {

/**
* Deposit the given token which is pre-approved using
* `token.approve()` in exchange for pufETH. This doesn't make the
* transaction but returns two methods namely `transact` and
* `Token.approve()` in exchange for wrapped PufToken. This doesn't
* make the transaction but returns two methods namely `transact` and
* `estimate`.
*
* @param token Token to deposit.
Expand All @@ -68,15 +68,15 @@ export class PufferL2DepositorHandler {
* `estimate: () => Promise<bigint>` - Gas estimate of the
* transaction.
*/
public depositAfterApproval(
public depositPreApproved(
token: NonPufToken,
walletAddress: Address,
value: bigint,
) {
const depositArgs = <const>[
TOKENS_ADDRESSES[token][this.chain],
walletAddress,
// Only `amount` is needed if `token.approve()` is already called.
// Only `amount` is needed if `Token.approve()` is already called.
// So using mock values for other properties.
{
r: padHex('0x', { size: 32 }),
Expand All @@ -102,13 +102,13 @@ export class PufferL2DepositorHandler {
}

/**
* Deposit the given token in exchange for pufETH. This doesn't make
* the transaction but returns two methods namely `transact` and
* `estimate`.
* Deposit the given token in exchange for the wrapped PufToken. This
* doesn't make the transaction but returns two methods namely
* `transact` and `estimate`.
*
* Note that not all token contracts support permit signatures (e.g.
* USDC). If a token's contract doesn't support permit signatures, use
* `Token.approve()` and call `depositAfterApproval()` instead.
* `Token.approve()` and call `this.depositPreApproved()` instead.
*
* @param token Token to deposit.
* @param walletAddress Wallet address to take the token from.
Expand Down

0 comments on commit b5daa78

Please sign in to comment.