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

2158: Add contract methods for redeeming pufETH #29

Merged
merged 5 commits into from
Aug 8, 2024
Merged
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
18 changes: 10 additions & 8 deletions lib/contracts/handlers/erc20-permit-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getContract,
Address,
parseSignature,
GetContractReturnType,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { ERC20PERMIT_ABI } from '../abis/tokens-abis';
Expand Down Expand Up @@ -58,14 +59,15 @@ export class ERC20PermitHandler {
* @returns The viem contract.
*/
public getContract() {
return getContract({
address: TOKENS_ADDRESSES[this.token][this.chain],
abi: ERC20PERMIT_ABI,
client: {
wallet: this.walletClient,
public: this.publicClient,
},
});
const address = TOKENS_ADDRESSES[this.token][this.chain];
const abi = ERC20PERMIT_ABI;
const client = { public: this.publicClient, wallet: this.walletClient };

return getContract({ address, abi, client }) as GetContractReturnType<
typeof abi,
typeof client,
Address
>;
}

/**
Expand Down
26 changes: 17 additions & 9 deletions lib/contracts/handlers/puf-locker-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { WalletClient, PublicClient, getContract, Address, padHex } from 'viem';
import {
WalletClient,
PublicClient,
getContract,
Address,
padHex,
GetContractReturnType,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { PUF_LOCKER_ABIS } from '../abis/puf-locker-abis';
import { CONTRACT_ADDRESSES } from '../addresses';
Expand Down Expand Up @@ -52,14 +59,15 @@ export class PufLockerHandler {
* @returns The viem contract.
*/
public getContract() {
return getContract({
address: CONTRACT_ADDRESSES[this.chain].PufLocker as Address,
abi: PUF_LOCKER_ABIS[this.chain].PufLocker,
client: {
wallet: this.walletClient,
public: this.publicClient,
},
});
const address = CONTRACT_ADDRESSES[this.chain].PufLocker as Address;
const abi = PUF_LOCKER_ABIS[this.chain].PufLocker;
const client = { public: this.publicClient, wallet: this.walletClient };

return getContract({ address, abi, client }) as GetContractReturnType<
typeof abi,
typeof client,
Address
>;
}

/**
Expand Down
25 changes: 16 additions & 9 deletions lib/contracts/handlers/puf-token-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { WalletClient, PublicClient, getContract, Address } from 'viem';
import {
WalletClient,
PublicClient,
getContract,
GetContractReturnType,
Address,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { PUF_TOKEN_ABIS } from '../abis/puf-token-abis';
import { PufToken, TOKENS_ADDRESSES } from '../tokens';
Expand Down Expand Up @@ -49,14 +55,15 @@ export class PufTokenHandler {
* @returns The viem contract.
*/
public getContract() {
return getContract({
address: TOKENS_ADDRESSES[this.pufToken][this.chain],
abi: PUF_TOKEN_ABIS[this.chain].PufToken,
client: {
wallet: this.walletClient,
public: this.publicClient,
},
});
const address = TOKENS_ADDRESSES[this.pufToken][this.chain];
const abi = PUF_TOKEN_ABIS[this.chain].PufToken;
const client = { public: this.publicClient, wallet: this.walletClient };

return getContract({ address, abi, client }) as GetContractReturnType<
typeof abi,
typeof client,
Address
>;
}

/**
Expand Down
25 changes: 16 additions & 9 deletions lib/contracts/handlers/puffer-depositor-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Address, PublicClient, WalletClient, getContract } from 'viem';
import {
Address,
GetContractReturnType,
PublicClient,
WalletClient,
getContract,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { PUFFER_DEPOSITOR_ABIS } from '../abis/puffer-depositor-abis';
import { CONTRACT_ADDRESSES } from '../addresses';
Expand Down Expand Up @@ -43,14 +49,15 @@ export class PufferDepositorHandler {
* @returns The viem contract.
*/
public getContract() {
return getContract({
address: CONTRACT_ADDRESSES[this.chain].PufferDepositor as Address,
abi: PUFFER_DEPOSITOR_ABIS[this.chain].PufferDepositor,
client: {
wallet: this.walletClient,
public: this.publicClient,
},
});
const address = CONTRACT_ADDRESSES[this.chain].PufferDepositor as Address;
const abi = PUFFER_DEPOSITOR_ABIS[this.chain].PufferDepositor;
const client = { public: this.publicClient, wallet: this.walletClient };

return getContract({ address, abi, client }) as GetContractReturnType<
typeof abi,
typeof client,
Address
>;
}

/**
Expand Down
26 changes: 17 additions & 9 deletions lib/contracts/handlers/puffer-l2-depositor-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { WalletClient, PublicClient, getContract, Address, padHex } from 'viem';
import {
WalletClient,
PublicClient,
getContract,
Address,
padHex,
GetContractReturnType,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { PUFFER_L2_DEPOSITOR_ABIS } from '../abis/puffer-depositor-abis';
import { CONTRACT_ADDRESSES } from '../addresses';
Expand Down Expand Up @@ -52,14 +59,15 @@ export class PufferL2DepositorHandler {
* @returns The viem contract.
*/
public getContract() {
return getContract({
address: CONTRACT_ADDRESSES[this.chain].PufferL2Depositor as Address,
abi: PUFFER_L2_DEPOSITOR_ABIS[this.chain].PufferL2Depositor,
client: {
wallet: this.walletClient,
public: this.publicClient,
},
});
const address = CONTRACT_ADDRESSES[this.chain].PufferL2Depositor as Address;
const abi = PUFFER_L2_DEPOSITOR_ABIS[this.chain].PufferL2Depositor;
const client = { public: this.publicClient, wallet: this.walletClient };

return getContract({ address, abi, client }) as GetContractReturnType<
typeof abi,
typeof client,
Address
>;
}

/**
Expand Down
48 changes: 48 additions & 0 deletions lib/contracts/handlers/puffer-vault-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,52 @@ describe('PufferVaultHandler', () => {
expect(typeof (await estimate())).toBe('bigint');
expect(isHash(await transact())).toBe(true);
});

it('should preview redeem for the given pufETH', async () => {
const mockRedeemAmount = 10n;
vaultTestingUtils.mockCall('previewRedeem', [mockRedeemAmount]);

const wethAmount = await handler.previewRedeem(10n);
expect(wethAmount).toBe(mockRedeemAmount);
});

it('should get max possible pufETH that can be redeemed', async () => {
const mockMaxRedeem = 10n;
vaultTestingUtils.mockCall('maxRedeem', [mockMaxRedeem]);

const maxRedeemPufETH = await handler.maxRedeem(mockAccount);
expect(maxRedeemPufETH).toBe(mockMaxRedeem);
});

it('should get exit fee basis points', async () => {
const mockPoints = 10n;
vaultTestingUtils.mockCall('getExitFeeBasisPoints', [mockPoints]);

const exitFeeBasisPoints = await handler.getExitFeeBasisPoints();
expect(exitFeeBasisPoints).toBe(mockPoints);
});

it('should get remaining assets daily withdrawal limit', async () => {
const mockLimit = 10n;
vaultTestingUtils.mockCall('getRemainingAssetsDailyWithdrawalLimit', [
mockLimit,
]);

const withdrawalLimit =
await handler.getRemainingAssetsDailyWithdrawalLimit();
expect(withdrawalLimit).toBe(mockLimit);
});

it('should redeem pufETH in exchange for WETH', async () => {
vaultTestingUtils.mockTransaction('redeem');

const { transact, estimate } = handler.redeem(
mockAccount,
mockAccount,
10n,
);

expect(typeof (await estimate())).toBe('bigint');
expect(isHash(await transact())).toBe(true);
});
});
108 changes: 99 additions & 9 deletions lib/contracts/handlers/puffer-vault-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Address, PublicClient, WalletClient, getContract } from 'viem';
import {
Address,
GetContractReturnType,
PublicClient,
WalletClient,
getContract,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { CONTRACT_ADDRESSES } from '../addresses';
import { PUFFER_VAULT_ABIS } from '../abis/puffer-vault-abis';
Expand Down Expand Up @@ -35,14 +41,15 @@ export class PufferVaultHandler {
* @returns The viem contract.
*/
public getContract() {
return getContract({
address: CONTRACT_ADDRESSES[this.chain].PufferVault as Address,
abi: PUFFER_VAULT_ABIS[this.chain].PufferVaultV2,
client: {
wallet: this.walletClient,
public: this.publicClient,
},
});
const address = CONTRACT_ADDRESSES[this.chain].PufferVault as Address;
const abi = PUFFER_VAULT_ABIS[this.chain].PufferVaultV2;
const client = { public: this.publicClient, wallet: this.walletClient };

return getContract({ address, abi, client }) as GetContractReturnType<
typeof abi,
typeof client,
Address
>;
}

/**
Expand Down Expand Up @@ -145,4 +152,87 @@ export class PufferVaultHandler {

return { transact, estimate };
}

/**
* Preview the amount of WETH that can be redeemed for the given
* amount of pufETH using the `.redeem()` method.
*
* @param value Value of pufETH to redeem.
* @returns Preview of the amount of WETH that can be redeemed.
*/
public previewRedeem(value: bigint) {
return this.getContract().read.previewRedeem([value]);
}

/**
* Calculates the maximum amount of pufETH shares that can be redeemed
* by the owner.
*
* @param ownerAddress Address of the owner's wallet.
* @returns Maximum amount of pufETH shares that can be redeemed.
*/
public maxRedeem(ownerAddress: Address) {
return this.getContract().read.maxRedeem([ownerAddress]);
}

/**
* Returns how many basis points of a fee there are when exiting. For
* example, a 1% fee would mean 1% of the user's requested pufETH is
* burned (which increases the value for all pufETH holders) before
* the ETH is redeemed. i.e., you get 1% less ETH back.
*
* @returns Basis points of the exit fee.
*/
public getExitFeeBasisPoints() {
return this.getContract().read.getExitFeeBasisPoints();
}

/**
* Returns how much WETH can still be withdrawn today.
*
* @returns Remaining WETH daily withdrawal limit.
*/
public getRemainingAssetsDailyWithdrawalLimit() {
return this.getContract().read.getRemainingAssetsDailyWithdrawalLimit();
}

/**
* Redeems pufETH shares in exchange for WETH assets from the vault.
* In the process, the pufETH shares of the owner are burned. This
* doesn't make the transaction but returns two methods namely
* `transact` and `estimate`.
*
* @param ownerAddress Address of the owner of pufETH.
* @param receiverAddress Address of the receiver of WETH.
* @param shares Amount of pufETH shares to redeem.
* @returns `transact: (value: bigint) => Promise<Address>` - Used to
* make the transaction with the given value.
*
* `estimate: () => Promise<bigint>` - Gas estimate of the
* transaction.
*/
public redeem(
ownerAddress: Address,
receiverAddress: Address,
shares: bigint,
) {
const transact = async () =>
await this.getContract().write.redeem(
[shares, receiverAddress, ownerAddress],
{
account: ownerAddress,
chain: this.viemChain,
},
);

const estimate = async () =>
await this.getContract().estimateGas.redeem(
[shares, receiverAddress, ownerAddress],
{
account: ownerAddress,
},
);

return { transact, estimate };
}
}
Loading
Loading