Use generated types in message execution function (#282)

* fix: lend transaction message

* feat: use generated types
This commit is contained in:
Yusuf Seyrek 2023-07-03 18:49:22 +03:00 committed by GitHub
parent 40d2950bf4
commit 85e36897a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 17 deletions

View File

@ -18,6 +18,7 @@ import { hardcodedFee } from 'utils/constants'
import { formatPercent, formatValue } from 'utils/formatters' import { formatPercent, formatValue } from 'utils/formatters'
import { BN } from 'utils/helpers' import { BN } from 'utils/helpers'
import AssetImage from 'components/AssetImage' import AssetImage from 'components/AssetImage'
import { BNCoin } from 'types/classes/BNCoin'
function getDebtAmount(modal: BorrowModal | null) { function getDebtAmount(modal: BorrowModal | null) {
if (!(modal?.marketData as BorrowAssetActive)?.debt) return '0' if (!(modal?.marketData as BorrowAssetActive)?.debt) return '0'
@ -60,7 +61,7 @@ export default function BorrowModal() {
result = await repay({ result = await repay({
fee: hardcodedFee, fee: hardcodedFee,
accountId: selectedAccount?.id ?? '0', accountId: selectedAccount?.id ?? '0',
coin: { denom: modal.asset.denom, amount: amount.toString() }, coin: BNCoin.fromDenomAndBigNumber(modal.asset.denom, amount),
accountBalance: percentage === 100, accountBalance: percentage === 100,
}) })
} else { } else {

View File

@ -8,6 +8,11 @@ import { getSingleValueFromBroadcastResult } from 'utils/broadcast'
import { formatAmountWithSymbol } from 'utils/formatters' import { formatAmountWithSymbol } from 'utils/formatters'
import { Action } from 'types/generated/mars-credit-manager/MarsCreditManager.types' import { Action } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
import { BNCoin } from 'types/classes/BNCoin' import { BNCoin } from 'types/classes/BNCoin'
import {
Action as CreditManagerAction,
ExecuteMsg as CreditManagerExecuteMsg,
} from 'types/generated/mars-credit-manager/MarsCreditManager.types'
import { ExecuteMsg as AccountNftExecuteMsg } from 'types/generated/mars-account-nft/MarsAccountNft.types'
export default function createBroadcastSlice( export default function createBroadcastSlice(
set: SetState<Store>, set: SetState<Store>,
@ -38,7 +43,7 @@ export default function createBroadcastSlice(
return { return {
toast: null, toast: null,
borrow: async (options: { fee: StdFee; accountId: string; coin: Coin }) => { borrow: async (options: { fee: StdFee; accountId: string; coin: Coin }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
account_id: options.accountId, account_id: options.accountId,
actions: [{ borrow: options.coin }], actions: [{ borrow: options.coin }],
@ -54,7 +59,7 @@ export default function createBroadcastSlice(
return !!response.result return !!response.result
}, },
createAccount: async (options: { fee: StdFee }) => { createAccount: async (options: { fee: StdFee }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
create_credit_account: 'default', create_credit_account: 'default',
} }
set({ createAccountModal: true }) set({ createAccountModal: true })
@ -77,7 +82,7 @@ export default function createBroadcastSlice(
} }
}, },
deleteAccount: async (options: { fee: StdFee; accountId: string }) => { deleteAccount: async (options: { fee: StdFee; accountId: string }) => {
const msg = { const msg: AccountNftExecuteMsg = {
burn: { burn: {
token_id: options.accountId, token_id: options.accountId,
}, },
@ -92,7 +97,7 @@ export default function createBroadcastSlice(
return !!response.result return !!response.result
}, },
deposit: async (options: { fee: StdFee; accountId: string; coin: Coin }) => { deposit: async (options: { fee: StdFee; accountId: string; coin: Coin }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
account_id: options.accountId, account_id: options.accountId,
actions: [ actions: [
@ -112,7 +117,7 @@ export default function createBroadcastSlice(
return !!response.result return !!response.result
}, },
unlock: async (options: { fee: StdFee; vault: Vault; amount: string }) => { unlock: async (options: { fee: StdFee; vault: Vault; amount: string }) => {
const msg = { const msg: CreditManagerAction = {
request_vault_unlock: { request_vault_unlock: {
vault: { address: options.vault.address }, vault: { address: options.vault.address },
amount: options.amount, amount: options.amount,
@ -129,7 +134,7 @@ export default function createBroadcastSlice(
return !!response.result return !!response.result
}, },
depositIntoVault: async (options: { fee: StdFee; accountId: string; actions: Action[] }) => { depositIntoVault: async (options: { fee: StdFee; accountId: string; actions: Action[] }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
account_id: options.accountId, account_id: options.accountId,
actions: options.actions, actions: options.actions,
@ -141,7 +146,7 @@ export default function createBroadcastSlice(
return !!response.result return !!response.result
}, },
withdraw: async (options: { fee: StdFee; accountId: string; coin: Coin }) => { withdraw: async (options: { fee: StdFee; accountId: string; coin: Coin }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
account_id: options.accountId, account_id: options.accountId,
actions: [ actions: [
@ -201,17 +206,16 @@ export default function createBroadcastSlice(
repay: async (options: { repay: async (options: {
fee: StdFee fee: StdFee
accountId: string accountId: string
coin: Coin coin: BNCoin
accountBalance?: boolean accountBalance?: boolean
}) => { }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
account_id: options.accountId, account_id: options.accountId,
actions: [ actions: [
{ {
repay: { repay: {
denom: options.coin.denom, coin: options.coin.toActionCoin(options.accountBalance),
amount: options.accountBalance ? 'account_balance' : { exact: options.coin.amount },
}, },
}, },
], ],
@ -222,17 +226,17 @@ export default function createBroadcastSlice(
handleResponseMessages( handleResponseMessages(
response, response,
`Repayed ${formatAmountWithSymbol(options.coin)} to Account ${options.accountId}`, `Repayed ${formatAmountWithSymbol(options.coin.toCoin())} to Account ${options.accountId}`,
) )
return !!response.result return !!response.result
}, },
lend: async (options: { fee: StdFee; accountId: string; coin: BNCoin; isMax?: boolean }) => { lend: async (options: { fee: StdFee; accountId: string; coin: BNCoin; isMax?: boolean }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
account_id: options.accountId, account_id: options.accountId,
actions: [ actions: [
{ {
lend: options.coin.toActionCoin(options.isMax), lend: options.coin.toCoin(),
}, },
], ],
}, },
@ -247,7 +251,7 @@ export default function createBroadcastSlice(
return !!response.result return !!response.result
}, },
reclaim: async (options: { fee: StdFee; accountId: string; coin: BNCoin; isMax?: boolean }) => { reclaim: async (options: { fee: StdFee; accountId: string; coin: BNCoin; isMax?: boolean }) => {
const msg = { const msg: CreditManagerExecuteMsg = {
update_credit_account: { update_credit_account: {
account_id: options.accountId, account_id: options.accountId,
actions: [ actions: [

View File

@ -1,3 +1,5 @@
const BNCoin = import('types/classes/BNCoin').BNCoin
interface BroadcastResult { interface BroadcastResult {
result?: import('@marsprotocol/wallet-connector').TxBroadcastResult result?: import('@marsprotocol/wallet-connector').TxBroadcastResult
error?: string error?: string
@ -36,7 +38,7 @@ interface BroadcastSlice {
repay: (options: { repay: (options: {
fee: StdFee fee: StdFee
accountId: string accountId: string
coin: Coin coin: BNCoin
accountBalance?: boolean accountBalance?: boolean
}) => Promise<boolean> }) => Promise<boolean>
} }