From 1923b8e7d7f43fd4463b7db8c89e81a3f9ae0bb6 Mon Sep 17 00:00:00 2001 From: Yusuf Seyrek Date: Wed, 30 Aug 2023 11:24:03 +0300 Subject: [PATCH] feat(SC update): added contract types and fixed breaking changes (#409) --- src/components/Modals/BorrowModal.tsx | 2 +- .../Modals/FundWithdraw/WithdrawFromAccount.tsx | 11 ++++++++--- src/store/slices/broadcast.ts | 16 ++++++++-------- .../MarsCreditManager.types.ts | 4 ++-- src/types/interfaces/store/broadcast.d.ts | 8 ++++++-- src/utils/accounts.ts | 2 +- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/components/Modals/BorrowModal.tsx b/src/components/Modals/BorrowModal.tsx index 55d6aff4..141abc58 100644 --- a/src/components/Modals/BorrowModal.tsx +++ b/src/components/Modals/BorrowModal.tsx @@ -93,7 +93,7 @@ function BorrowModal(props: Props) { } else { result = await borrow({ accountId: account.id, - coin: { denom: asset.denom, amount: amount.toString() }, + coin: BNCoin.fromDenomAndBigNumber(asset.denom, amount), borrowToWallet, }) } diff --git a/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx b/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx index 4cbf8151..ca6ff577 100644 --- a/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx +++ b/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx @@ -14,7 +14,7 @@ import useToggle from 'hooks/useToggle' import { useUpdatedAccount } from 'hooks/useUpdatedAccount' import useStore from 'store' import { BNCoin } from 'types/classes/BNCoin' -import { cloneAccount, getMergedBalances, removeDepostisAndLends } from 'utils/accounts' +import { cloneAccount, getMergedBalances, removeDepositsAndLends } from 'utils/accounts' import { byDenom } from 'utils/array' import { getEnabledMarketAssets } from 'utils/assets' @@ -34,7 +34,7 @@ export default function WithdrawFromAccount(props: Props) { const { simulateWithdraw } = useUpdatedAccount(account) const { computeMaxWithdrawAmount } = useHealthComputer(account) const accountClone = cloneAccount(account) - const borrowAccount = removeDepostisAndLends(accountClone, currentAsset.denom) + const borrowAccount = removeDepositsAndLends(accountClone, currentAsset.denom) const { computeMaxBorrowAmount } = useHealthComputer(borrowAccount) const balances = getMergedBalances(account, getEnabledMarketAssets()) const maxWithdrawAmount = computeMaxWithdrawAmount(currentAsset.denom) @@ -65,7 +65,12 @@ export default function WithdrawFromAccount(props: Props) { async function onConfirm() { setIsConfirming(true) - const coins = [BNCoin.fromDenomAndBigNumber(currentAsset.denom, amount)] + const coins = [ + { + coin: BNCoin.fromDenomAndBigNumber(currentAsset.denom, amount), + isMax: max.isEqualTo(amount), + }, + ] const borrow = !debtAmount.isZero() ? [BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)] : [] diff --git a/src/store/slices/broadcast.ts b/src/store/slices/broadcast.ts index bdee7144..d36e67d7 100644 --- a/src/store/slices/broadcast.ts +++ b/src/store/slices/broadcast.ts @@ -89,9 +89,9 @@ export default function createBroadcastSlice( return { toast: null, - borrow: async (options: { accountId: string; coin: Coin; borrowToWallet: boolean }) => { - const borrowAction: Action = { borrow: options.coin } - const withdrawAction: Action = { withdraw: options.coin } + borrow: async (options: { accountId: string; coin: BNCoin; borrowToWallet: boolean }) => { + const borrowAction: Action = { borrow: options.coin.toCoin() } + const withdrawAction: Action = { withdraw: options.coin.toActionCoin() } const actions = options.borrowToWallet ? [borrowAction, withdrawAction] : [borrowAction] const msg: CreditManagerExecuteMsg = { @@ -117,7 +117,7 @@ export default function createBroadcastSlice( handleResponseMessages( response, - `Borrowed ${formatAmountWithSymbol(options.coin)} to ${ + `Borrowed ${formatAmountWithSymbol(options.coin.toCoin())} to ${ options.borrowToWallet ? 'Wallet' : `Credit Account ${options.accountId}` }`, ) @@ -324,15 +324,15 @@ export default function createBroadcastSlice( }, withdraw: async (options: { accountId: string - coins: BNCoin[] + coins: Array<{ coin: BNCoin; isMax?: boolean }> borrow: BNCoin[] reclaims: ActionCoin[] }) => { const reclaimActions = options.reclaims.map((coin) => ({ reclaim: coin, })) - const withdrawActions = options.coins.map((coin) => ({ - withdraw: coin.toCoin(), + const withdrawActions = options.coins.map(({ coin, isMax }) => ({ + withdraw: coin.toActionCoin(isMax), })) const borrowActions = options.borrow.map((coin) => ({ borrow: coin.toCoin(), @@ -350,7 +350,7 @@ export default function createBroadcastSlice( }) const withdrawString = options.coins - .map((coin) => formatAmountWithSymbol(coin.toCoin())) + .map(({ coin }) => formatAmountWithSymbol(coin.toCoin())) .join('and ') handleResponseMessages( response, diff --git a/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts b/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts index 8bded465..8f2d0a9a 100644 --- a/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts +++ b/src/types/generated/mars-credit-manager/MarsCreditManager.types.ts @@ -62,7 +62,7 @@ export type Action = deposit: Coin } | { - withdraw: Coin + withdraw: ActionCoin } | { borrow: Coin @@ -176,7 +176,7 @@ export type CallbackMsg = | { withdraw: { account_id: string - coin: Coin + coin: ActionCoin recipient: Addr } } diff --git a/src/types/interfaces/store/broadcast.d.ts b/src/types/interfaces/store/broadcast.d.ts index 01d7fa23..384e0320 100644 --- a/src/types/interfaces/store/broadcast.d.ts +++ b/src/types/interfaces/store/broadcast.d.ts @@ -12,7 +12,11 @@ interface ExecutableTx { } interface BroadcastSlice { - borrow: (options: { accountId: string; coin: Coin; borrowToWallet: boolean }) => Promise + borrow: (options: { + accountId: string + coin: BNCoin + borrowToWallet: boolean + }) => Promise claimRewards: (options: { accountId: string }) => ExecutableTx createAccount: () => Promise deleteAccount: (options: { accountId: string; lends: BNCoin[] }) => Promise @@ -44,7 +48,7 @@ interface BroadcastSlice { withdrawFromVaults: (options: { accountId: string; vaults: DepositedVault[] }) => Promise withdraw: (options: { accountId: string - coins: BNCoin[] + coins: Array<{ coin: BNCoin; isMax?: boolean }> borrow: BNCoin[] reclaims: ActionCoin[] }) => Promise diff --git a/src/utils/accounts.ts b/src/utils/accounts.ts index 3e4c995a..bcc800a2 100644 --- a/src/utils/accounts.ts +++ b/src/utils/accounts.ts @@ -205,7 +205,7 @@ export function cloneAccount(account: Account): Account { } } -export function removeDepostisAndLends(account: Account, denom: string) { +export function removeDepositsAndLends(account: Account, denom: string) { const deposits = account.deposits.filter((deposit) => deposit.denom !== denom) const lends = account.lends.filter((lend) => lend.denom !== denom)