Skip to content

Commit

Permalink
Merge pull request #102 from CheesecakeLabs/fix_contracts_restore
Browse files Browse the repository at this point in the history
fix restore contract data
  • Loading branch information
lucasmagnus authored Feb 1, 2024
2 parents 1e01588 + 9255413 commit e48061f
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 136 deletions.
301 changes: 185 additions & 116 deletions frontend/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"react-slick": "^0.29.0",
"react-timeago": "^7.1.0",
"sass": "~1.59.2",
"stellar-plus": "^0.5.6",
"stellar-plus": "^0.5.7",
"swiper": "^10.2.0",
"typescript": "~4.9.5",
"web-vitals": "~3.3.0",
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/core/pages/contracts-detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { useNavigate, useParams } from 'react-router-dom'
import { useAuth } from 'hooks/useAuth'
import { useContracts } from 'hooks/useContracts'
import { useTransactions } from 'hooks/useTransactions'
import { ContractsService } from 'soroban/contracts-service'
import { MessagesError } from 'utils/constants/messages-error'

import { PathRoute } from 'components/enums/path-route'
import { Sidebar } from 'components/organisms/sidebar'
import { ContractsDetailTemplate } from 'components/templates/contracts-detail'
import { ContractsService } from 'soroban/contracts-service'

export const ContractsDetail: React.FC = () => {
const {
Expand Down Expand Up @@ -107,7 +107,7 @@ export const ContractsDetail: React.FC = () => {
let timeLeft = contractData?.timeLeft

await ContractsService.validateContract(sponsor, contract.address)

const position =
Number(
(await getPosition(wallet, contract.address, wallet, sponsor)) || 0
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/components/templates/contracts-detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const ContractsDetailTemplate: React.FC<IContractsDetailTemplate> = ({
currentInVault,
hasAssetInVault,
hasWallet,
timerCounter
timerCounter,
}) => {
return (
<Flex flexDir="column" w="full">
Expand Down Expand Up @@ -111,7 +111,9 @@ export const ContractsDetailTemplate: React.FC<IContractsDetailTemplate> = ({
onSubmit={onSubmitWithdraw}
contract={contract}
loading={isWithdrawing}
isDone={!timerCounter || timerCounter === 0}
isDone={
contractData.timeLeft ? contractData.timeLeft < 0 : false
}
withdrawValue={
contractData.timeLeft && contractData.timeLeft > 0
? Number(contractData.estimatedPrematureWithdraw)
Expand Down
93 changes: 79 additions & 14 deletions frontend/src/hooks/useContracts/context.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { createContext, useCallback, useState } from 'react'

import freighter from '@stellar/freighter-api'
Expand All @@ -7,6 +8,12 @@ import { CustomAccountHandler } from 'soroban'
import { STELLAR_NETWORK, WASM_HASH, vcRpcHandler } from 'soroban/constants'
import { BUMP_FEE, ContractsService } from 'soroban/contracts-service'
import { StellarPlus } from 'stellar-plus'
import { ContractEngineErrorCodes } from 'stellar-plus/lib/stellar-plus/core/contract-engine/errors'
import { StellarPlusError } from 'stellar-plus/lib/stellar-plus/error'
import {
SimulationErrorInfo,
restoreData,
} from 'stellar-plus/lib/stellar-plus/error/helpers/soroban-rpc'
import { CertificateOfDepositClient } from 'stellar-plus/lib/stellar-plus/soroban/contracts/certificate-of-deposit'
import {
FeeBumpTransaction,
Expand Down Expand Up @@ -145,7 +152,10 @@ export const ContractsProvider: React.FC<IProps> = ({ children }) => {
[getAccountData]
)

const getContractData = (contractId: string, sponsorPK: string): CertificateOfDepositClient => {
const getContractData = (
contractId: string,
sponsorPK: string
): CertificateOfDepositClient => {
const opex = ContractsService.loadAccount(sponsorPK)
const restoreTxInvocation = ContractsService.getTxInvocation(opex, BUMP_FEE)

Expand Down Expand Up @@ -211,21 +221,46 @@ export const ContractsProvider: React.FC<IProps> = ({ children }) => {
}
}

const restoreData = async (
sponsorPk: string,
stellarPlusError: StellarPlusError,
contract: CertificateOfDepositClient
): Promise<void> => {
const opex = ContractsService.loadAccount(sponsorPk)
const restoreTxInvocation = ContractsService.getTxInvocation(opex, BUMP_FEE)
const restoreData = stellarPlusError.meta
?.sorobanSimulationData as restoreData

await contract.restoreFootprint({
...restoreTxInvocation,
restorePreamble: restoreData.restorePreamble,
})
}

const getPosition = async (
address: string,
contractId: string,
sourcePk: string,
sponsorPk: string
): Promise<number | undefined> => {
const contract = getContractData(contractId, sponsorPk)
try {
const contract = getContractData(contractId, sponsorPk)

return contract.getPosition({
const result = await contract.getPosition({
address: address,
...userTxInvocation(sourcePk),
})
return result
} catch (e) {
return
const stellarPlusError = e as StellarPlusError
if (stellarPlusError.code === ContractEngineErrorCodes.CE102) {
restoreData(sponsorPk, stellarPlusError, contract)
}

const result = await contract.getPosition({
address: address,
...userTxInvocation(sourcePk),
})
return result
}
}

Expand All @@ -235,14 +270,24 @@ export const ContractsProvider: React.FC<IProps> = ({ children }) => {
sourcePk: string,
sponsorPk: string
): Promise<number | undefined> => {
const contract = getContractData(contractId, sponsorPk)
try {
const contract = getContractData(contractId, sponsorPk)
return contract.getEstimatedYield({
const result = await contract.getEstimatedYield({
address: address,
...userTxInvocation(sourcePk),
})
return result
} catch (e) {
return
const stellarPlusError = e as StellarPlusError
if (stellarPlusError.code === ContractEngineErrorCodes.CE102) {
restoreData(sponsorPk, stellarPlusError, contract)
}

const result = await contract.getEstimatedYield({
address: address,
...userTxInvocation(sourcePk),
})
return result
}
}

Expand All @@ -252,14 +297,24 @@ export const ContractsProvider: React.FC<IProps> = ({ children }) => {
sourcePk: string,
sponsorPk: string
): Promise<number | undefined> => {
const contract = getContractData(contractId, sponsorPk)
try {
const contract = getContractData(contractId, sponsorPk)
return contract.getEstimatedPrematureWithdraw({
const result = await contract.getEstimatedPrematureWithdraw({
address: address,
...userTxInvocation(sourcePk),
})
return result
} catch (e) {
return
const stellarPlusError = e as StellarPlusError
if (stellarPlusError.code === ContractEngineErrorCodes.CE102) {
restoreData(sponsorPk, stellarPlusError, contract)
}

const result = await contract.getEstimatedPrematureWithdraw({
address: address,
...userTxInvocation(sourcePk),
})
return result
}
}

Expand All @@ -269,14 +324,24 @@ export const ContractsProvider: React.FC<IProps> = ({ children }) => {
sourcePk: string,
sponsorPk: string
): Promise<number | undefined> => {
const contract = getContractData(contractId, sponsorPk)
try {
const contract = getContractData(contractId, sponsorPk)
return contract.getTimeLeft({
const result = await contract.getTimeLeft({
address: address,
...userTxInvocation(sourcePk),
})
return result
} catch (e) {
return
const stellarPlusError = e as StellarPlusError
if (stellarPlusError.code === ContractEngineErrorCodes.CE102) {
restoreData(sponsorPk, stellarPlusError, contract)
}

const result = await contract.getTimeLeft({
address: address,
...userTxInvocation(sourcePk),
})
return result
}
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/soroban/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StellarPlus } from "stellar-plus";
export const STELLAR_NETWORK = StellarPlus.Constants.testnet

export const WASM_HASH =
'6cc0156febf6f2b83fa46f5b5ec58f516a05e7921dbe4d33398e735e3af203d8'
'3e74bccc06fba85a5e0f25cb0488754ef4224d1cd44ed1c409e032abc46fe4fd'

export const vcRpcHandler = new StellarPlus.RPC.ValidationCloudRpcHandler(
STELLAR_NETWORK,
Expand Down

0 comments on commit e48061f

Please sign in to comment.