-
Notifications
You must be signed in to change notification settings - Fork 277
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: add transaction builder functions for interacting with Morpho vaults #1852
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
26c4a85
save
dschlabach 896e00d
Merge branch 'main' into dms/earn-provider
dschlabach d2bec65
save
dschlabach 3bf5ff1
Merge branch 'main' into dms/earn-provider
dschlabach 64ce951
init
dschlabach 6303e4b
add tests
dschlabach 5aad232
remove async for now
dschlabach f4fc58f
update interface
dschlabach 821f98c
lint
dschlabach 8e6589b
fix; add tests
dschlabach 58d03af
rename const
dschlabach 4796812
comments
dschlabach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { EarnProvider, useEarnContext } from './EarnProvider'; | ||
|
||
describe('EarnProvider', () => { | ||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<EarnProvider vaultAddress="0x123">{children}</EarnProvider> | ||
); | ||
|
||
it('provides vault address through context', () => { | ||
const { result } = renderHook(() => useEarnContext(), { wrapper }); | ||
|
||
expect(result.current.vaultAddress).toBe('0x123'); | ||
}); | ||
|
||
it('throws error when useEarnContext is used outside of provider', () => { | ||
expect(() => renderHook(() => useEarnContext())).toThrow( | ||
'useEarnContext must be used within an EarnProvider', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useValue } from '@/core-react/internal/hooks/useValue'; | ||
import { type ReactNode, createContext, useContext } from 'react'; | ||
import type { Address } from 'viem'; | ||
|
||
interface EarnContextType { | ||
vaultAddress: Address; | ||
} | ||
|
||
const EarnContext = createContext<EarnContextType | undefined>(undefined); | ||
|
||
interface EarnProviderProps { | ||
vaultAddress: Address; | ||
children: ReactNode; | ||
} | ||
|
||
export function EarnProvider({ vaultAddress, children }: EarnProviderProps) { | ||
const value = useValue({ | ||
vaultAddress, | ||
}); | ||
|
||
return <EarnContext.Provider value={value}>{children}</EarnContext.Provider>; | ||
} | ||
|
||
export function useEarnContext() { | ||
const context = useContext(EarnContext); | ||
if (!context) { | ||
throw new Error('useEarnContext must be used within an EarnProvider'); | ||
} | ||
return context; | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const METAMORPHO_ABI = [ | ||
brendan-defi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
inputs: [ | ||
{ internalType: 'uint256', name: 'assets', type: 'uint256' }, | ||
{ internalType: 'address', name: 'receiver', type: 'address' }, | ||
], | ||
name: 'deposit', | ||
outputs: [{ internalType: 'uint256', name: 'shares', type: 'uint256' }], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [ | ||
{ internalType: 'uint256', name: 'assets', type: 'uint256' }, | ||
{ internalType: 'address', name: 'receiver', type: 'address' }, | ||
{ internalType: 'address', name: 'owner', type: 'address' }, | ||
], | ||
name: 'withdraw', | ||
outputs: [{ internalType: 'uint256', name: 'shares', type: 'uint256' }], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
] as const; | ||
|
||
export const USDC_ADDRESS = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; | ||
export const USDC_DECIMALS = 6; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// 🌲☀🌲 | ||
// Components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { METAMORPHO_ABI, USDC_ADDRESS, USDC_DECIMALS } from '@/earn/constants'; | ||
import { encodeFunctionData, parseUnits } from 'viem'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { buildDepositToMorphoTx } from './deposit'; | ||
|
||
describe('buildDepositToMorphoTx', () => { | ||
const mockArgs = { | ||
vaultAddress: '0xd63070114470f685b75B74D60EEc7c1113d33a3D', | ||
tokenAddress: USDC_ADDRESS, | ||
amount: parseUnits('1000', USDC_DECIMALS), | ||
receiverAddress: '0x9E95f497a7663B70404496dB6481c890C4825fe1', | ||
} as const; | ||
|
||
it('should return an array with two transactions', async () => { | ||
const result = buildDepositToMorphoTx(mockArgs); | ||
expect(result).toHaveLength(2); | ||
}); | ||
|
||
it('should build correct approve transaction', async () => { | ||
const result = buildDepositToMorphoTx(mockArgs); | ||
const expectedAmount = mockArgs.amount; | ||
|
||
const expectedApproveData = encodeFunctionData({ | ||
abi: [ | ||
{ | ||
name: 'approve', | ||
type: 'function', | ||
inputs: [ | ||
{ name: 'spender', type: 'address' }, | ||
{ name: 'amount', type: 'uint256' }, | ||
], | ||
outputs: [{ type: 'bool' }], | ||
stateMutability: 'nonpayable', | ||
}, | ||
], | ||
functionName: 'approve', | ||
args: [mockArgs.vaultAddress, expectedAmount], | ||
}); | ||
|
||
expect(result[0]).toEqual({ | ||
to: mockArgs.tokenAddress, | ||
data: expectedApproveData, | ||
}); | ||
}); | ||
|
||
it('should build correct deposit transaction', async () => { | ||
const result = buildDepositToMorphoTx(mockArgs); | ||
const expectedAmount = mockArgs.amount; | ||
|
||
const expectedDepositData = encodeFunctionData({ | ||
abi: METAMORPHO_ABI, | ||
functionName: 'deposit', | ||
args: [expectedAmount, mockArgs.receiverAddress], | ||
}); | ||
|
||
expect(result[1]).toEqual({ | ||
to: mockArgs.vaultAddress, | ||
data: expectedDepositData, | ||
}); | ||
}); | ||
|
||
it('should handle zero amount', async () => { | ||
const result = buildDepositToMorphoTx({ | ||
...mockArgs, | ||
amount: 0n, | ||
}); | ||
|
||
expect(result).toHaveLength(2); | ||
expect(result[0].to).toBe(mockArgs.tokenAddress); | ||
expect(result[1].to).toBe(mockArgs.vaultAddress); | ||
}); | ||
|
||
it('should handle decimal amounts', async () => { | ||
const result = buildDepositToMorphoTx({ | ||
...mockArgs, | ||
amount: parseUnits('100.5', USDC_DECIMALS), | ||
}); | ||
|
||
const expectedAmount = parseUnits('100.5', USDC_DECIMALS); | ||
expect(result).toHaveLength(2); | ||
expect( | ||
encodeFunctionData({ | ||
abi: METAMORPHO_ABI, | ||
functionName: 'deposit', | ||
args: [expectedAmount, mockArgs.receiverAddress], | ||
}), | ||
).toBe(result[1].data); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { METAMORPHO_ABI } from '@/earn/constants'; | ||
import type { Call } from '@/transaction/types'; | ||
import { type Address, encodeFunctionData, erc20Abi } from 'viem'; | ||
|
||
type DepositToMorphoArgs = { | ||
vaultAddress: Address; | ||
tokenAddress: Address; | ||
amount: bigint; | ||
receiverAddress: Address; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add descriptions for each of theses plz? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
export function buildDepositToMorphoTx({ | ||
brendan-defi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
vaultAddress, | ||
tokenAddress, | ||
amount, | ||
receiverAddress, | ||
}: DepositToMorphoArgs): Call[] { | ||
// User needs to approve the token they're depositing | ||
const approveTxData = encodeFunctionData({ | ||
abi: erc20Abi, | ||
functionName: 'approve', | ||
args: [vaultAddress, amount], | ||
}); | ||
|
||
// Once approved, user can deposit the token into the vault | ||
const depositTxData = encodeFunctionData({ | ||
abi: METAMORPHO_ABI, | ||
functionName: 'deposit', | ||
args: [amount, receiverAddress], | ||
}); | ||
|
||
return [ | ||
{ | ||
to: tokenAddress, | ||
data: approveTxData, | ||
}, | ||
{ | ||
to: vaultAddress, | ||
data: depositTxData, | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { METAMORPHO_ABI, USDC_DECIMALS } from '@/earn/constants'; | ||
import { encodeFunctionData, parseUnits } from 'viem'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { buildWithdrawFromMorphoTx } from './withdraw'; | ||
|
||
describe('buildWithdrawFromMorphoTx', () => { | ||
const mockArgs = { | ||
vaultAddress: '0xd63070114470f685b75B74D60EEc7c1113d33a3D', | ||
amount: parseUnits('1000', USDC_DECIMALS), | ||
receiverAddress: '0x9E95f497a7663B70404496dB6481c890C4825fe1', | ||
} as const; | ||
|
||
it('should return an array with one transaction', async () => { | ||
const result = await buildWithdrawFromMorphoTx(mockArgs); | ||
expect(result).toHaveLength(1); | ||
}); | ||
|
||
it('should build correct withdraw transaction', async () => { | ||
const result = await buildWithdrawFromMorphoTx(mockArgs); | ||
const expectedAmount = mockArgs.amount; | ||
|
||
const expectedWithdrawData = encodeFunctionData({ | ||
abi: METAMORPHO_ABI, | ||
functionName: 'withdraw', | ||
args: [ | ||
expectedAmount, | ||
mockArgs.receiverAddress, | ||
mockArgs.receiverAddress, | ||
], | ||
}); | ||
|
||
expect(result[0]).toEqual({ | ||
to: mockArgs.vaultAddress, | ||
data: expectedWithdrawData, | ||
}); | ||
}); | ||
|
||
it('should handle zero amount', async () => { | ||
const result = await buildWithdrawFromMorphoTx({ | ||
...mockArgs, | ||
amount: 0n, | ||
}); | ||
|
||
const expectedAmount = parseUnits('0', USDC_DECIMALS); | ||
expect(result).toHaveLength(1); | ||
expect( | ||
encodeFunctionData({ | ||
abi: METAMORPHO_ABI, | ||
functionName: 'withdraw', | ||
args: [ | ||
expectedAmount, | ||
mockArgs.receiverAddress, | ||
mockArgs.receiverAddress, | ||
], | ||
}), | ||
).toBe(result[0].data); | ||
}); | ||
|
||
it('should handle decimal amounts', async () => { | ||
const result = await buildWithdrawFromMorphoTx({ | ||
...mockArgs, | ||
amount: parseUnits('100.5', USDC_DECIMALS), | ||
}); | ||
|
||
const expectedAmount = parseUnits('100.5', USDC_DECIMALS); | ||
expect(result).toHaveLength(1); | ||
expect( | ||
encodeFunctionData({ | ||
abi: METAMORPHO_ABI, | ||
functionName: 'withdraw', | ||
args: [ | ||
expectedAmount, | ||
mockArgs.receiverAddress, | ||
mockArgs.receiverAddress, | ||
], | ||
}), | ||
).toBe(result[0].data); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { METAMORPHO_ABI } from '@/earn/constants'; | ||
import type { Call } from '@/transaction/types'; | ||
import { type Address, encodeFunctionData } from 'viem'; | ||
|
||
type WithdrawFromMorphoArgs = { | ||
vaultAddress: Address; | ||
amount: bigint; | ||
receiverAddress: Address; | ||
}; | ||
|
||
export async function buildWithdrawFromMorphoTx({ | ||
vaultAddress, | ||
amount, | ||
receiverAddress, | ||
}: WithdrawFromMorphoArgs): Promise<Call[]> { | ||
const withdrawTxData = encodeFunctionData({ | ||
abi: METAMORPHO_ABI, | ||
functionName: 'withdraw', | ||
args: [amount, receiverAddress, receiverAddress], | ||
}); | ||
|
||
return [ | ||
{ | ||
to: vaultAddress, | ||
data: withdrawTxData, | ||
}, | ||
]; | ||
} | ||
brendan-defi marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
our pattern has not been to throw if
useComponentContext
s have been used outside of their providers. Perhaps that's bad practice, but it's useful in certain cases (eg. if you want to attempt to use a context, and if you get back values use them, and if not source them from elsewhere).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're not throwing these errors in our context hooks, that seems like a code smell. It's the standard way of doing it for pretty much all major libraries.
We also do this in:
useNFTLifecycleContext
useTheme
useBuyContext
useCheckoutContext
useTransactionContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we should include this in all providers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as far as I know, wallet is the exception so it will silently fail on an unconnected wallet button
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the providers that do not currently do this are:

OnchainKitProvider
IdentityProvider
WalletProvider
can y'all think of a reason we wouldn't want to throw in these cases? if not I can add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a big issue that we don't throw in
useOnchainKit
especially