Skip to content

Commit

Permalink
fix: add autorestore to sac
Browse files Browse the repository at this point in the history
  • Loading branch information
fazzatti committed Aug 15, 2024
1 parent 9774613 commit 96dd9af
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
19 changes: 9 additions & 10 deletions frontend/src/app/core/pages/contracts-create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
INNER_FEE,
TOKEN_DECIMALS,
} from 'soroban/contracts-service'
import { StellarPlusError } from 'stellar-plus/lib/stellar-plus/error'
import { CertificateOfDepositClient } from 'stellar-plus/lib/stellar-plus/soroban/contracts/certificate-of-deposit'
import { AutoRestorePlugin } from 'stellar-plus/lib/stellar-plus/utils/pipeline/plugins/simulate-transaction'

Expand Down Expand Up @@ -46,15 +47,18 @@ export const ContractsCreate: React.FC = () => {
try {
setCreatingContract(true)

const token = ContractsService.loadToken(asset)
let contractId = asset.contract_id

const sponsorPK = await getSponsorPK()
if (!sponsorPK) throw new Error('Invalid sponsor')

const opex = ContractsService.loadAccount(sponsorPK)
const opexTxInvocation = ContractsService.getTxInvocation(opex, BUMP_FEE)

const token = ContractsService.loadToken(
asset,
ContractsService.getAutoRestorePlugin(opex)
)
let contractId = asset.contract_id

if (!contractId) {
await token.wrapAndDeploy(opexTxInvocation)
contractId = token.sorobanTokenHandler.getContractId()
Expand Down Expand Up @@ -85,13 +89,7 @@ export const ContractsCreate: React.FC = () => {
options: {
sorobanTransactionPipeline: {
customRpcHandler: vcRpcHandler,
plugins: [
new AutoRestorePlugin(
opexTxInvocation,
STELLAR_NETWORK,
vcRpcHandler
),
],
plugins: [ContractsService.getAutoRestorePlugin(opex)],
},
},
/* wasmHash: WASM_HASH,
Expand All @@ -112,6 +110,7 @@ export const ContractsCreate: React.FC = () => {

await codClient.deploy(opexTxInvocation).catch(error => {
console.error('Error deploying contract', error)
console.error('Details', (error as StellarPlusError).meta)
throw new Error('Error deploying contract')
})
await codClient
Expand Down
34 changes: 24 additions & 10 deletions frontend/src/soroban/contracts-service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import axios from 'axios'
import { CustomAccountHandler } from 'soroban'
import { StellarPlus } from 'stellar-plus'
import { SACHandler } from 'stellar-plus/lib/stellar-plus/asset'
import { DefaultRpcHandler } from 'stellar-plus/lib/stellar-plus/rpc'
import { CertificateOfDepositClient } from 'stellar-plus/lib/stellar-plus/soroban/contracts/certificate-of-deposit'
import {
FeeBumpHeader,
FeeBumpTransaction,
Transaction,
TransactionInvocation,
TransactionXdr,
} from 'stellar-plus/lib/stellar-plus/types'
import { AutoRestorePlugin } from 'stellar-plus/lib/stellar-plus/utils/pipeline/plugins/simulate-transaction'
import { MessagesError } from 'utils/constants/messages-error'

import { TSelectCompoundType } from 'components/templates/contracts-create/components/select-compound-type'

import { http } from 'interfaces/http'

import { STELLAR_NETWORK, WASM_HASH, vcRpcHandler } from './constants'
import { DefaultRpcHandler } from 'stellar-plus/lib/stellar-plus/rpc'
import { CertificateOfDepositClient } from 'stellar-plus/lib/stellar-plus/soroban/contracts/certificate-of-deposit'
import { AutoRestorePlugin } from 'stellar-plus/lib/stellar-plus/utils/pipeline/plugins/simulate-transaction'

export const TOKEN_DECIMALS = 10000000
export const BUMP_FEE = '10000000'
export const BUMP_FEE = '100000000'
export const INNER_FEE = '1000000'
const SECONDS_IN_DAY = 86400
const VALUE_TO_PERCENTAGE = 100
Expand Down Expand Up @@ -73,19 +73,33 @@ const sign = async (
}
}

const loadToken = (asset: Hooks.UseAssetsTypes.IAssetDto): SACHandler => {
const loadToken = (
asset: Hooks.UseAssetsTypes.IAssetDto,
autoRestorePlugin: AutoRestorePlugin
): SACHandler => {
return new StellarPlus.Asset.SACHandler({
code: asset.code,
issuerAccount: asset.issuer.key.publicKey,
networkConfig: STELLAR_NETWORK,
options: {
sorobanTransactionPipeline: {
customRpcHandler: vcRpcHandler
}
}
customRpcHandler: vcRpcHandler,
plugins: [autoRestorePlugin],
},
},
})
}

const getAutoRestorePlugin = (
opex: CustomAccountHandler
): AutoRestorePlugin => {
return new AutoRestorePlugin(
getTxInvocation(opex, BUMP_FEE),
STELLAR_NETWORK,
vcRpcHandler
)
}

const getExpirationLedger = async (): Promise<number> => {
const sorobanHandler = new DefaultRpcHandler(STELLAR_NETWORK)
const latestLedger = await sorobanHandler.getLatestLedger()
Expand Down Expand Up @@ -160,11 +174,11 @@ const validateParamsCOD = async (
return codParams
}


export const ContractsService = {
customSign,
loadToken,
validateParamsCOD,
loadAccount,
getTxInvocation
getTxInvocation,
getAutoRestorePlugin,
}

0 comments on commit 96dd9af

Please sign in to comment.