Skip to content

Commit

Permalink
use u256 max for max withdraw [bug fix]
Browse files Browse the repository at this point in the history
  • Loading branch information
akiraonstarknet committed Sep 30, 2024
1 parent 94413ea commit 0aaf9f8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
32 changes: 18 additions & 14 deletions src/components/Deposit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { DUMMY_BAL_ATOM } from '@/store/balance.atoms';
import { StrategyInfo } from '@/store/strategies.atoms';
import { StrategyTxProps } from '@/store/transactions.atom';
import { IStrategyActionHook, TokenInfo } from '@/strategies/IStrategy';
import {
DepositActionInputs,
IStrategyActionHook,
TokenInfo,
} from '@/strategies/IStrategy';
import { MyMenuItemProps, MyMenuListProps } from '@/utils';
import MyNumber from '@/utils/MyNumber';
import { ChevronDownIcon } from '@chakra-ui/icons';
Expand Down Expand Up @@ -31,7 +35,6 @@ import { useAccount, useProvider } from '@starknet-react/core';
import { useAtomValue } from 'jotai';
import mixpanel from 'mixpanel-browser';
import { useEffect, useMemo, useState } from 'react';
import { ProviderInterface, uint256 } from 'starknet';
import LoadingWrap from './LoadingWrap';
import TxButton from './TxButton';

Expand All @@ -40,11 +43,7 @@ interface DepositProps {
// ? If you want to add more button text, you can add here
// ? @dev ensure below actionType is updated accordingly
buttonText: 'Deposit' | 'Redeem';
callsInfo: (
amount: MyNumber,
address: string,
provider: ProviderInterface,
) => IStrategyActionHook[];
callsInfo: (inputs: DepositActionInputs) => IStrategyActionHook[];
}

export default function Deposit(props: DepositProps) {
Expand All @@ -57,8 +56,12 @@ export default function Deposit(props: DepositProps) {

// This is the selected market token
const [selectedMarket, setSelectedMarket] = useState(
props.callsInfo(MyNumber.fromZero(), address || '0x0', provider)[0]
.tokenInfo,
props.callsInfo({
amount: MyNumber.fromZero(),
address: address || '0x0',
provider,
isMax: isMaxClicked,
})[0].tokenInfo,
);

// This is processed amount stored in MyNumber format and meant for sending tx
Expand Down Expand Up @@ -90,11 +93,12 @@ export default function Deposit(props: DepositProps) {

// constructs tx calls
const { calls, actions } = useMemo(() => {
const amountParam =
isMaxClicked && !isDeposit
? new MyNumber(uint256.UINT_256_MAX.toString(), amount.decimals)
: amount;
const actions = props.callsInfo(amountParam, address || '0x0', provider);
const actions = props.callsInfo({
amount,
address: address || '0x0',
provider,
isMax: isMaxClicked,
});
const hook = actions.find((a) => a.tokenInfo.name === selectedMarket.name);
if (!hook) return { calls: [], actions };
return { calls: hook.calls, actions };
Expand Down
21 changes: 11 additions & 10 deletions src/strategies/IStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ export interface AmountInfo {
tokenInfo: TokenInfo;
}

export interface DepositActionInputs {
amount: MyNumber;
address: string;
provider: ProviderInterface;
isMax: boolean;
}

export interface WithdrawActionInputs extends DepositActionInputs {}

export class IStrategyProps {
readonly liveStatus: StrategyLiveStatus;
readonly id: string;
Expand Down Expand Up @@ -114,19 +123,11 @@ export class IStrategyProps {
return `Risk factor: ${this.riskFactor}/5 (${factorLevel} risk)`;
}

depositMethods = (
amount: MyNumber,
address: string,
provider: ProviderInterface,
): IStrategyActionHook[] => {
depositMethods = (inputs: DepositActionInputs): IStrategyActionHook[] => {
return [];
};

withdrawMethods = (
amount: MyNumber,
address: string,
provider: ProviderInterface,
): IStrategyActionHook[] => {
withdrawMethods = (inputs: WithdrawActionInputs): IStrategyActionHook[] => {
return [];
};

Expand Down
18 changes: 7 additions & 11 deletions src/strategies/auto_strk.strat.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import CONSTANTS, { TOKENS, TokenName } from '@/constants';
import { PoolInfo } from '@/store/pools';
import {
DepositActionInputs,
IStrategy,
IStrategySettings,
StrategyAction,
StrategyLiveStatus,
TokenInfo,
WithdrawActionInputs,
} from './IStrategy';
import ERC20Abi from '@/abi/erc20.abi.json';
import AutoStrkAbi from '@/abi/autoStrk.abi.json';
import MasterAbi from '@/abi/master.abi.json';
import MyNumber from '@/utils/MyNumber';
import { Contract, ProviderInterface, uint256 } from 'starknet';
import { Contract, uint256 } from 'starknet';
import { atom } from 'jotai';
import {
DUMMY_BAL_ATOM,
Expand Down Expand Up @@ -165,11 +167,8 @@ export class AutoTokenStrategy extends IStrategy {
// this.leverage = this.netYield / normalYield;
// }

depositMethods = (
amount: MyNumber,
address: string,
provider: ProviderInterface,
) => {
depositMethods = (inputs: DepositActionInputs) => {
const { amount, address, provider } = inputs;
const baseTokenInfo: TokenInfo = TOKENS.find(
(t) => t.name == this.token.name,
) as TokenInfo; //
Expand Down Expand Up @@ -247,11 +246,8 @@ export class AutoTokenStrategy extends IStrategy {
];
};

withdrawMethods = (
amount: MyNumber,
address: string,
provider: ProviderInterface,
) => {
withdrawMethods = (inputs: WithdrawActionInputs) => {
const { amount, address, provider } = inputs;
const frmToken: TokenInfo = TOKENS.find(
(t) => t.token == this.strategyAddress,
) as TokenInfo;
Expand Down
23 changes: 11 additions & 12 deletions src/strategies/delta_neutral_mm.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import CONSTANTS, { NFTS, TokenName } from '@/constants';
import { PoolInfo } from '@/store/pools';
import {
DepositActionInputs,
IStrategy,
IStrategySettings,
NFTInfo,
StrategyAction,
StrategyLiveStatus,
TokenInfo,
WithdrawActionInputs,
} from './IStrategy';
import { zkLend } from '@/store/zklend.store';
import ERC20Abi from '@/abi/erc20.abi.json';
import DeltaNeutralAbi from '@/abi/deltraNeutral.abi.json';
import MyNumber from '@/utils/MyNumber';
import { Call, Contract, ProviderInterface, uint256 } from 'starknet';
import { Call, Contract, uint256 } from 'starknet';
import { nostraLending } from '@/store/nostralending.store';
import { getPrice, getTokenInfoFromName, standariseAddress } from '@/utils';
import {
Expand Down Expand Up @@ -233,11 +235,8 @@ export class DeltaNeutralMM extends IStrategy {
];
}

depositMethods = (
amount: MyNumber,
address: string,
provider: ProviderInterface,
) => {
depositMethods = (inputs: DepositActionInputs) => {
const { amount, address, provider } = inputs;
const baseTokenInfo = this.token;

if (!address || address == '0x0') {
Expand Down Expand Up @@ -336,11 +335,8 @@ export class DeltaNeutralMM extends IStrategy {
}
};

withdrawMethods = (
amount: MyNumber,
address: string,
provider: ProviderInterface,
) => {
withdrawMethods = (inputs: WithdrawActionInputs) => {
const { amount, address, provider, isMax } = inputs;
const mainToken = { ...this.token };

// removing max amount restrictions on withdrawal
Expand All @@ -365,8 +361,11 @@ export class DeltaNeutralMM extends IStrategy {
provider,
);

const finalAmount = isMax
? new MyNumber(uint256.UINT_256_MAX.toString(), amount.decimals)
: amount;
const call = strategyContract.populate('withdraw', [
uint256.bnToUint256(amount.toString()),
uint256.bnToUint256(finalAmount.toString()),
address,
500, // 5% max slippage
]);
Expand Down

0 comments on commit 0aaf9f8

Please sign in to comment.