Skip to content

Commit

Permalink
Add reuse order in start compute flow (#1352)
Browse files Browse the repository at this point in the history
* wip add reuse order logic

* add reuse order in start job

* added missing check if no jobs found

* update lib

Signed-off-by: mihaisc <[email protected]>

* fix lint

Signed-off-by: mihaisc <[email protected]>

Co-authored-by: mihaisc <[email protected]>
  • Loading branch information
bogdanfazakas and mihaisc authored Apr 21, 2022
1 parent a0558ef commit ef744c1
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 10 deletions.
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@coingecko/cryptoformat": "^0.4.4",
"@loadable/component": "^5.15.2",
"@oceanprotocol/art": "^3.2.0",
"@oceanprotocol/lib": "^1.0.0-next.36",
"@oceanprotocol/lib": "^1.0.0-next.37",
"@oceanprotocol/typographies": "^0.1.0",
"@portis/web3": "^4.0.7",
"@tippyjs/react": "^4.2.6",
Expand Down
28 changes: 28 additions & 0 deletions src/@utils/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,31 @@ export async function transformComputeFormToServiceComputeOptions(

return privacy
}

export async function checkComputeResourcesValidity(
asset: Asset,
accountId: string,
computeEnvMaxJobDuration: number,
datasetTimeout?: number,
algorithmTimeout?: number,
cancelToken?: CancelToken
): Promise<boolean> {
const jobs = await getComputeJobs(
[asset?.chainId],
accountId,
asset,
cancelToken
)
if (jobs.computeJobs.length <= 0) return false
const inputValues = []
computeEnvMaxJobDuration && inputValues.push(computeEnvMaxJobDuration * 60)
datasetTimeout && inputValues.push(datasetTimeout)
algorithmTimeout && inputValues.push(algorithmTimeout)
const minValue = Math.min(...inputValues)
const jobStartDate = new Date(
parseInt(jobs.computeJobs[0].dateCreated) * 1000
)
jobStartDate.setMinutes(jobStartDate.getMinutes() + Math.floor(minValue / 60))
const currentTime = new Date().getTime() / 1000
return Math.floor(jobStartDate.getTime() / 1000) > currentTime
}
54 changes: 54 additions & 0 deletions src/@utils/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,57 @@ export async function order(
}
}
}

/**
* called when having a valid order, but with expired provider access, requires approval of the provider fee
* @param web3
* @param asset
* @param accountId
* @param accountId validOrderTx
* @param computeEnv
* @param computeValidUntil
* @param computeConsumerAddress
* @returns {TransactionReceipt} receipt of the order
*/
export async function reuseOrder(
web3: Web3,
asset: AssetExtended,
accountId: string,
validOrderTx: string,
computeEnv: string = null,
computeValidUntil: number = null,
computeConsumerAddress?: string
) {
const datatoken = new Datatoken(web3)
const initializeData = await ProviderInstance.initialize(
asset.id,
asset.services[0].id,
0,
accountId,
asset.services[0].serviceEndpoint,
null,
null,
computeEnv,
computeValidUntil
)
const txApprove = await approve(
web3,
accountId,
initializeData.providerFee.providerFeeToken,
asset.accessDetails.datatoken.address,
initializeData.providerFee.providerFeeAmount,
false
)
if (!txApprove) {
return
}

const tx = await datatoken.reuseOrder(
asset.accessDetails.datatoken.address,
accountId,
validOrderTx,
initializeData.providerFee
)

return tx
}
35 changes: 33 additions & 2 deletions src/components/Asset/AssetActions/Compute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
getAlgorithmAssetSelectionList,
getAlgorithmsForAsset,
getValidUntilTime,
getComputeEnviroment
getComputeEnviroment,
checkComputeResourcesValidity
} from '@utils/compute'
import { AssetSelectionAsset } from '@shared/FormFields/AssetSelection'
import AlgorithmDatasetsListForCompute from './AlgorithmDatasetsListForCompute'
Expand All @@ -41,7 +42,7 @@ import { useAbortController } from '@hooks/useAbortController'
import { getOrderPriceAndFees } from '@utils/accessDetailsAndPricing'
import { OrderPriceAndFees } from 'src/@types/Price'
import { buyDtFromPool } from '@utils/pool'
import { order } from '@utils/order'
import { order, reuseOrder } from '@utils/order'
import { AssetExtended } from 'src/@types/AssetExtended'
import { getComputeFeedback } from '@utils/feedback'

Expand Down Expand Up @@ -94,6 +95,7 @@ export default function Compute({
useState<OrderPriceAndFees>()
const [isRequestingAlgoOrderPrice, setIsRequestingAlgoOrderPrice] =
useState(false)
const [isProviderFeeValid, setIsProviderFeeValid] = useState(false)
const isComputeButtonDisabled =
isJobStarting === true ||
file === null ||
Expand All @@ -116,6 +118,15 @@ export default function Compute({
async function initPriceAndFees() {
const computeEnv = await getComputeEnviroment(asset)
setComputeEnv(computeEnv)
setIsProviderFeeValid(
await checkComputeResourcesValidity(
asset,
accountId,
computeEnv?.maxJobDuration,
asset.services[0].timeout,
selectedAlgorithmAsset.services[0].timeout
)
)
const validUntil = getValidUntilTime(
computeEnv?.maxJobDuration,
asset.services[0].timeout,
Expand Down Expand Up @@ -377,6 +388,26 @@ export default function Compute({
}
}

if (isOwned && !isProviderFeeValid) {
const reuseOrderTx = await reuseOrder(
web3,
asset,
accountId,
validOrderTx,
computeEnv?.id,
computeValidUntil
)
if (!reuseOrderTx) {
toast.error('Failed to pay provider fees!')
return
}
LoggerInstance.log(
'[compute] Reused order: ',
reuseOrderTx.transactionHash
)
datasetOrderTx = reuseOrderTx.transactionHash
}

LoggerInstance.log('[compute] Starting compute job.')
const computeAsset: ComputeAsset = {
documentId: asset.id,
Expand Down

0 comments on commit ef744c1

Please sign in to comment.