-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Owned positions swap section * working example * Add metadata & action views * Review updates * Fix function labels
- Loading branch information
Showing
16 changed files
with
567 additions
and
28 deletions.
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,6 @@ | ||
--- | ||
'@penumbra-zone/query': minor | ||
'@penumbra-zone/wasm': minor | ||
--- | ||
|
||
Customize symbol for LP position NFTs |
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,7 @@ | ||
--- | ||
'@penumbra-zone/perspective': minor | ||
'minifront': minor | ||
'@penumbra-zone/ui': minor | ||
--- | ||
|
||
Support for displaying LP position action views |
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
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,27 @@ | ||
import { Card } from '@penumbra-zone/ui/components/ui/card'; | ||
import { GradientHeader } from '@penumbra-zone/ui/components/ui/gradient-header'; | ||
import { useOwnedPositions } from '../../state/swap/lp-positions.ts'; | ||
import { bech32mPositionId } from '@penumbra-zone/bech32m/plpid'; | ||
|
||
// TODO: Ids are not sufficient in taking action on these | ||
// Required to move forward with this: https://github.com/penumbra-zone/penumbra/pull/4837 | ||
export const LpPositions = () => { | ||
const { data, error } = useOwnedPositions(); | ||
|
||
return !data?.length ? ( | ||
<div className='hidden xl:block' /> | ||
) : ( | ||
<Card layout> | ||
<GradientHeader layout>Limit orders</GradientHeader> | ||
{!!error && <div>❌ There was an error loading your limit orders: ${String(error)}</div>} | ||
{data.map(({ positionId }) => { | ||
const base64Id = bech32mPositionId(positionId ?? { inner: new Uint8Array() }); | ||
return ( | ||
<div key={base64Id} className='flex items-center gap-4 p-2'> | ||
{base64Id} | ||
</div> | ||
); | ||
})} | ||
</Card> | ||
); | ||
}; |
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
20 changes: 20 additions & 0 deletions
20
apps/minifront/src/components/swap/swap-form/limit-order.tsx
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,20 @@ | ||
import { AllSlices } from '../../../state'; | ||
import { useStoreShallow } from '../../../utils/use-store-shallow.ts'; | ||
|
||
const limitOrderSelector = (state: AllSlices) => ({ | ||
assetIn: state.swap.assetIn, | ||
assetOut: state.swap.assetOut, | ||
amount: state.swap.amount, | ||
onSubmit: state.swap.lpPositions.onSubmit, | ||
}); | ||
|
||
export const LimitOrder = () => { | ||
const { onSubmit } = useStoreShallow(limitOrderSelector); | ||
|
||
return ( | ||
<div> | ||
<h1>Limit order</h1> | ||
<button onClick={() => void onSubmit()}>SEND LIMIT ORDER</button> | ||
</div> | ||
); | ||
}; |
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
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,125 @@ | ||
import { SliceCreator, useStore } from '..'; | ||
import { createZQuery, ZQueryState } from '@penumbra-zone/zquery'; | ||
import { penumbra } from '../../prax.ts'; | ||
import { ViewService } from '@penumbra-zone/protobuf/penumbra/view/v1/view_connect'; | ||
import { | ||
OwnedPositionIdsResponse, | ||
TransactionPlannerRequest, | ||
} from '@penumbra-zone/protobuf/penumbra/view/v1/view_pb'; | ||
import { SwapSlice } from './index.ts'; | ||
import { isValidAmount, planBuildBroadcast } from '../helpers.ts'; | ||
import { getAddressIndex } from '@penumbra-zone/getters/address-view'; | ||
import { getAssetIdFromValueView } from '@penumbra-zone/getters/value-view'; | ||
import { PositionState_PositionStateEnum } from '@penumbra-zone/protobuf/penumbra/core/component/dex/v1/dex_pb'; | ||
import { base64ToUint8Array } from '@penumbra-zone/types/base64'; | ||
|
||
export const { ownedPositions, useOwnedPositions } = createZQuery({ | ||
name: 'ownedPositions', | ||
fetch: () => penumbra.service(ViewService).ownedPositionIds({}), | ||
stream: () => { | ||
return { | ||
onValue: ( | ||
prevState: OwnedPositionIdsResponse[] | undefined, | ||
response: OwnedPositionIdsResponse, | ||
) => { | ||
return [...(prevState ?? []), response]; | ||
}, | ||
}; | ||
}, | ||
getUseStore: () => useStore, | ||
get: state => state.swap.lpPositions.ownedPositions, | ||
set: setter => { | ||
const newState = setter(useStore.getState().swap.lpPositions.ownedPositions); | ||
useStore.setState(state => { | ||
state.swap.lpPositions.ownedPositions = newState; | ||
}); | ||
}, | ||
}); | ||
|
||
interface Actions { | ||
onSubmit: () => Promise<void>; | ||
} | ||
|
||
interface State { | ||
ownedPositions: ZQueryState<OwnedPositionIdsResponse[]>; | ||
} | ||
|
||
export type LpPositionsSlice = Actions & State; | ||
|
||
const INITIAL_STATE: State = { | ||
ownedPositions, | ||
}; | ||
|
||
export const createLpPositionsSlice = (): SliceCreator<LpPositionsSlice> => (set, get) => { | ||
return { | ||
...INITIAL_STATE, | ||
onSubmit: async () => { | ||
try { | ||
set(state => { | ||
state.swap.txInProgress = true; | ||
}); | ||
|
||
const txPlannerRequest = assembleLimitOrderReq(get().swap); | ||
await planBuildBroadcast('positionOpen', txPlannerRequest); | ||
|
||
set(state => { | ||
state.swap.amount = ''; | ||
}); | ||
get().shared.balancesResponses.revalidate(); | ||
} finally { | ||
set(state => { | ||
state.swap.txInProgress = false; | ||
}); | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
// TODO: This is temporary data for testing purposes. Update with inputs when component is ready. | ||
const assembleLimitOrderReq = ({ assetIn, amount, assetOut }: SwapSlice) => { | ||
if (!assetIn) { | ||
throw new Error('`assetIn` is undefined'); | ||
} | ||
if (!assetOut) { | ||
throw new Error('`assetOut` is undefined'); | ||
} | ||
if (!isValidAmount(amount, assetIn)) { | ||
throw new Error('Invalid amount'); | ||
} | ||
|
||
return new TransactionPlannerRequest({ | ||
positionOpens: [ | ||
{ | ||
position: { | ||
phi: { | ||
component: { p: { lo: 1000000n }, q: { lo: 1000000n } }, | ||
pair: { | ||
asset1: getAssetIdFromValueView(assetIn.balanceView), | ||
asset2: assetOut.penumbraAssetId, | ||
}, | ||
}, | ||
nonce: crypto.getRandomValues(new Uint8Array(32)), | ||
state: { state: PositionState_PositionStateEnum.OPENED }, | ||
reserves: { r1: { lo: 1n }, r2: {} }, | ||
closeOnFill: true, | ||
}, | ||
}, | ||
], | ||
positionCloses: [ | ||
{ | ||
positionId: { inner: base64ToUint8Array('/C9cn0d8veH0IGt2SCghzfcCWkPWbgUDXpXOPgZyA8c=') }, | ||
}, | ||
], | ||
positionWithdraws: [ | ||
{ | ||
positionId: { inner: base64ToUint8Array('+vbub7BbEAAKLqRorZbNZ4yixPNVFzGl1BAexym3mDc=') }, | ||
reserves: { r1: { lo: 1000000n }, r2: {} }, | ||
tradingPair: { | ||
asset1: getAssetIdFromValueView(assetIn.balanceView), | ||
asset2: assetOut.penumbraAssetId, | ||
}, | ||
}, | ||
], | ||
source: getAddressIndex(assetIn.accountAddress), | ||
}); | ||
}; |
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
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
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
Oops, something went wrong.