Skip to content

Commit

Permalink
Use PositionManager for Andromeda USDC
Browse files Browse the repository at this point in the history
  • Loading branch information
noisekit committed Jan 17, 2025
1 parent 93f2cdc commit 6f163ea
Show file tree
Hide file tree
Showing 24 changed files with 228 additions and 217 deletions.
116 changes: 7 additions & 109 deletions liquidity/components/DepositModal/DepositMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import { createMachine, assign } from 'xstate';

export const Events = {
SET_REQUIRE_APPROVAL: 'SET_REQUIRE_APPROVAL',
SET_REQUIRE_APPROVAL_FOR_STATAUSDC: 'SET_REQUIRE_APPROVAL_FOR_STATAUSDC',
SET_WRAP_AMOUNT: 'SET_WRAP_AMOUNT',
SET_INFINITE_APPROVAL: 'SET_INFINITE_APPROVAL',
SET_IS_STATA_USDC: 'SET_IS_STATA_USDC',
SET_HAS_ENOUGH_STATAUSDC: 'SET_HAS_ENOUGH_STATAUSDC',
RETRY: 'RETRY',
RUN: 'RUN',
WRAP_USDC: 'WRAP_USDC',
SUCCESS: 'SUCCESS',
FAILURE: 'FAILURE',
RESET: 'RESET',
Expand All @@ -20,8 +16,6 @@ export const State = {
idle: 'idle',
wrap: 'wrap',
approveCollateral: 'approveCollateral',
wrapUSDC: 'wrapUSDC',
approveUSDCForStata: 'approveUSDCForStata',
deposit: 'deposit',
failed: 'failed',
success: 'success',
Expand All @@ -31,16 +25,10 @@ const FailedSteps = {
[State.approveCollateral]: State.approveCollateral,
[State.wrap]: State.wrap,
[State.deposit]: State.deposit,
[State.wrapUSDC]: State.wrapUSDC,
[State.approveUSDCForStata]: State.approveUSDCForStata,
} as const;

export const ServiceNames = {
wrapEth: 'wrapEth',

approveUSDCForStata: 'approveUSDCForStata',
wrapUSDCToStataUSDC: 'wrapUSDCToStataUSDC',

approveCollateral: 'approveCollateral',
executeDeposit: 'executeDeposit',
} as const;
Expand All @@ -53,26 +41,16 @@ type Context = {
requireApproval: boolean;
wrapAmount: Wei;
infiniteApproval: boolean;
isStataUSDC: boolean;
requireStataUSDCApproval: boolean;
hasEnoughStataUSDC: boolean;
};

type EventNamesType = typeof Events;

type DepositEvents =
| { type: EventNamesType['SET_IS_STATA_USDC']; isStataUSDC: boolean }
| { type: EventNamesType['SET_HAS_ENOUGH_STATAUSDC']; hasEnoughStataUSDC: boolean }
| { type: EventNamesType['SET_REQUIRE_APPROVAL']; requireApproval: boolean }
| {
type: EventNamesType['SET_REQUIRE_APPROVAL_FOR_STATAUSDC'];
requireStataUSDCApproval: boolean;
}
| { type: EventNamesType['SET_WRAP_AMOUNT']; wrapAmount: Wei }
| { type: EventNamesType['SET_INFINITE_APPROVAL']; infiniteApproval: boolean }
| { type: EventNamesType['RETRY'] }
| { type: EventNamesType['RUN'] }
| { type: EventNamesType['WRAP_USDC'] }
| { type: EventNamesType['SUCCESS'] }
| { type: EventNamesType['FAILURE'] }
| { type: EventNamesType['RESET'] };
Expand All @@ -95,14 +73,6 @@ type MachineState =
value: StateType['deposit'];
context: Context & { error: null };
}
| {
value: StateType['wrapUSDC'];
context: Context & { error: null };
}
| {
value: StateType['approveUSDCForStata'];
context: Context & { error: null };
}
| {
value: StateType['failed'];
context: Context & { error: { error: Error; step: keyof typeof FailedSteps } };
Expand All @@ -119,9 +89,6 @@ const initialContext = {
error: null,
requireApproval: false,
infiniteApproval: false,
isStataUSDC: false,
requireStataUSDCApproval: false,
hasEnoughStataUSDC: false,
};

export const DepositMachine = createMachine<Context, DepositEvents, MachineState>({
Expand All @@ -142,42 +109,24 @@ export const DepositMachine = createMachine<Context, DepositEvents, MachineState
[Events.SET_REQUIRE_APPROVAL]: {
actions: assign({ requireApproval: (_context, event) => event.requireApproval }),
},
[Events.SET_REQUIRE_APPROVAL_FOR_STATAUSDC]: {
actions: assign({
requireStataUSDCApproval: (_context, event) => event.requireStataUSDCApproval,
}),
},
[Events.SET_WRAP_AMOUNT]: {
actions: assign({ wrapAmount: (_context, event) => event.wrapAmount }),
},
[Events.SET_INFINITE_APPROVAL]: {
actions: assign({ infiniteApproval: (_context, event) => event.infiniteApproval }),
},
[Events.SET_IS_STATA_USDC]: {
actions: assign({ isStataUSDC: (_context, event) => event.isStataUSDC }),
},
},
states: {
[State.idle]: {
on: {
[Events.RUN]: [
{
target: State.wrap,
cond: (context) => context.wrapAmount.gt(0),
},
{
target: State.approveUSDCForStata,
cond: (context) => context.isStataUSDC && context.requireStataUSDCApproval,
},
{
target: State.wrapUSDC,
cond: (context) => context.isStataUSDC,
},
{ target: State.wrap, cond: (context) => context.wrapAmount.gt(0) },
{ target: State.approveCollateral, cond: (context) => context.requireApproval },
{ target: State.deposit },
],
},
},

[State.wrap]: {
invoke: {
src: ServiceNames.wrapEth,
Expand All @@ -188,61 +137,18 @@ export const DepositMachine = createMachine<Context, DepositEvents, MachineState
}),
},
onDone: [
{
target: State.approveUSDCForStata,
cond: (context) => context.requireStataUSDCApproval && context.isStataUSDC,
},
{ target: State.approveCollateral, cond: (context) => context.requireApproval },
{ target: State.deposit },
],
},
},
[State.approveUSDCForStata]: {
invoke: {
src: ServiceNames.approveUSDCForStata,
onError: {
target: State.failed,
actions: assign({
error: (_context, event) => ({
error: event.data,
step: FailedSteps.approveUSDCForStata,
}),
}),
},
onDone: {
target: State.wrapUSDC,
},
},
},
[State.wrapUSDC]: {
invoke: {
src: ServiceNames.wrapUSDCToStataUSDC,
onError: {
target: State.failed,
actions: assign({
error: (_context, event) => ({ error: event.data, step: FailedSteps.wrapUSDC }),
}),
},
onDone: [
{
target: State.approveCollateral,
cond: (context) => context.requireApproval,
},
{
target: State.deposit,
},
],
},
},

[State.approveCollateral]: {
invoke: {
src: ServiceNames.approveCollateral,
onDone: [
{
target: State.deposit,
},
],
onDone: {
target: State.deposit,
},
onError: {
target: State.failed,
actions: assign({
Expand All @@ -269,6 +175,7 @@ export const DepositMachine = createMachine<Context, DepositEvents, MachineState
},
},
},

[State.failed]: {
on: {
[Events.RETRY]: [
Expand All @@ -287,19 +194,10 @@ export const DepositMachine = createMachine<Context, DepositEvents, MachineState
cond: (c) => c.error?.step === FailedSteps.deposit,
actions: assign({ error: (_) => null }),
},
{
target: State.approveUSDCForStata,
cond: (c) => c.error?.step === FailedSteps.approveUSDCForStata,
actions: assign({ error: (_) => null }),
},
{
target: State.wrapUSDC,
cond: (c) => c.error?.step === FailedSteps.wrapUSDC,
actions: assign({ error: (_) => null }),
},
],
},
},

[State.success]: {},
},
});
1 change: 0 additions & 1 deletion liquidity/components/DepositModal/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './DepositMachine';
export * from './DepositModal';
export * from './StataDepositModal';
8 changes: 0 additions & 8 deletions liquidity/components/DepositModal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,20 @@
"@snx-v3/Multistep": "workspace:*",
"@snx-v3/constants": "workspace:*",
"@snx-v3/format": "workspace:*",
"@snx-v3/txnReducer": "workspace:*",
"@snx-v3/useAccountProxy": "workspace:*",
"@snx-v3/useApprove": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useCollateralPriceUpdates": "workspace:*",
"@snx-v3/useCollateralTypes": "workspace:*",
"@snx-v3/useContractErrorParser": "workspace:*",
"@snx-v3/useCoreProxy": "workspace:*",
"@snx-v3/useDeposit": "workspace:*",
"@snx-v3/useDepositBaseAndromeda": "workspace:*",
"@snx-v3/useLiquidityPosition": "workspace:*",
"@snx-v3/useParams": "workspace:*",
"@snx-v3/usePositionManagerAndromedaStataUSDC": "workspace:*",
"@snx-v3/useSpotMarketProxy": "workspace:*",
"@snx-v3/useSynthToken": "workspace:*",
"@snx-v3/useUSDC": "workspace:*",
"@snx-v3/useWrapEth": "workspace:*",
"@snx-v3/withERC7412": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@tanstack/react-query": "^5.8.3",
"@xstate/react": "^3.2.2",
"debug": "^4.3.7",
"ethers": "^5.7.2",
"react": "^18.2.0",
"xstate": "^4.38.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useCollateralPriceUpdates } from '@snx-v3/useCollateralPriceUpdates';
import { useCollateralType } from '@snx-v3/useCollateralTypes';
import { useContractErrorParser } from '@snx-v3/useContractErrorParser';
import { type PositionPageSchemaType, useParams } from '@snx-v3/useParams';
import { usePositionManagerAndromedaStataUSDC } from '@snx-v3/usePositionManagerAndromedaStataUSDC';
import { usePositionManagerForCollateral } from '@snx-v3/usePositionManagerForCollateral';
import { useUSDC } from '@snx-v3/useUSDC';
import { withERC7412 } from '@snx-v3/withERC7412';
import { Wei } from '@synthetixio/wei';
Expand All @@ -22,9 +22,9 @@ import debug from 'debug';
import { ethers } from 'ethers';
import React from 'react';

const log = debug('snx:StataDepositModal');
const log = debug('snx:DepositModalAndromeda');

export function StataDepositModal({ onClose }: { onClose: () => void }) {
export function DepositModalAndromeda({ onClose }: { onClose: () => void }) {
const [params] = useParams<PositionPageSchemaType>();

const { data: collateralType } = useCollateralType(params.collateralSymbol);
Expand All @@ -38,7 +38,7 @@ export function StataDepositModal({ onClose }: { onClose: () => void }) {

const { data: USDC } = useUSDC();
const { data: AccountProxy } = useAccountProxy();
const { data: PositionManagerAndromedaStataUSDC } = usePositionManagerAndromedaStataUSDC();
const { data: PositionManager } = usePositionManagerForCollateral({ collateralType });

const { data: priceUpdateTx } = useCollateralPriceUpdates();

Expand All @@ -53,15 +53,15 @@ export function StataDepositModal({ onClose }: { onClose: () => void }) {
} = useApprove({
contractAddress: USDC?.address,
amount: collateralChange.toBN().mul(D6).div(D18),
spender: PositionManagerAndromedaStataUSDC?.address,
spender: PositionManager?.address,
});

const isReady =
network &&
provider &&
signer &&
AccountProxy &&
PositionManagerAndromedaStataUSDC &&
PositionManager &&
USDC?.address &&
collateralType?.tokenAddress &&
collateralChange.gt(0);
Expand Down Expand Up @@ -97,30 +97,28 @@ export function StataDepositModal({ onClose }: { onClose: () => void }) {
signer
);
const approveAccountTx = AccountProxyContract.populateTransaction.approve(
PositionManagerAndromedaStataUSDC.address,
PositionManager.address,
params.accountId
);
callsPromises.push(approveAccountTx);
}

const PositionManagerAndromedaStataUSDCContract = new ethers.Contract(
PositionManagerAndromedaStataUSDC.address,
PositionManagerAndromedaStataUSDC.abi,
const PositionManagerContract = new ethers.Contract(
PositionManager.address,
PositionManager.abi,
signer
);

if (params.accountId) {
const increasePositionTx =
PositionManagerAndromedaStataUSDCContract.populateTransaction.increasePosition(
params.accountId,
collateralChange.toBN().mul(D6).div(D18)
);
const increasePositionTx = PositionManagerContract.populateTransaction.increasePosition(
params.accountId,
collateralChange.toBN().mul(D6).div(D18)
);
callsPromises.push(increasePositionTx);
} else {
const setupPositionTx =
PositionManagerAndromedaStataUSDCContract.populateTransaction.setupPosition(
collateralChange.toBN().mul(D6).div(D18)
);
const setupPositionTx = PositionManagerContract.populateTransaction.setupPosition(
collateralChange.toBN().mul(D6).div(D18)
);
callsPromises.push(setupPositionTx);
}

Expand All @@ -133,7 +131,7 @@ export function StataDepositModal({ onClose }: { onClose: () => void }) {
provider,
network,
calls,
'useDepositAndromedaStataUSDC',
'useDepositAndromeda',
walletAddress
);

Expand Down
1 change: 1 addition & 0 deletions liquidity/components/DepositModalAndromeda/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DepositModalAndromeda';
31 changes: 31 additions & 0 deletions liquidity/components/DepositModalAndromeda/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@snx-v3/DepositModalAndromeda",
"private": true,
"main": "index.ts",
"version": "0.0.1",
"dependencies": {
"@chakra-ui/icons": "^2.1.1",
"@chakra-ui/react": "^2.8.2",
"@snx-v3/Amount": "workspace:*",
"@snx-v3/ContractError": "workspace:*",
"@snx-v3/ManagePositionContext": "workspace:*",
"@snx-v3/Multistep": "workspace:*",
"@snx-v3/constants": "workspace:*",
"@snx-v3/txnReducer": "workspace:*",
"@snx-v3/useAccountProxy": "workspace:*",
"@snx-v3/useApprove": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useCollateralPriceUpdates": "workspace:*",
"@snx-v3/useCollateralTypes": "workspace:*",
"@snx-v3/useContractErrorParser": "workspace:*",
"@snx-v3/useParams": "workspace:*",
"@snx-v3/usePositionManagerForCollateral": "workspace:*",
"@snx-v3/useUSDC": "workspace:*",
"@snx-v3/withERC7412": "workspace:*",
"@synthetixio/wei": "^2.74.4",
"@tanstack/react-query": "^5.8.3",
"debug": "^4.3.7",
"ethers": "^5.7.2",
"react": "^18.2.0"
}
}
Loading

0 comments on commit 6f163ea

Please sign in to comment.