1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

Add reuse order in start compute flow (#1352)

* wip add reuse order logic

* add reuse order in start job

* added missing check if no jobs found

* update lib

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* fix lint

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

Co-authored-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
Bogdan Fazakas 2022-04-21 17:30:12 +03:00 committed by GitHub
parent a0558efaab
commit ef744c1a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 125 additions and 10 deletions

16
package-lock.json generated
View File

@ -13,7 +13,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",
@ -2723,9 +2723,9 @@
"integrity": "sha512-M/yyKfpWmMRHWPTjvKlrRWUcIbfkRWyHhjHUI1kPggJPPX6ADxApTwtzwVXJ/+WyegcaYc7bqwuclqvg9XPqBQ=="
},
"node_modules/@oceanprotocol/lib": {
"version": "1.0.0-next.36",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-1.0.0-next.36.tgz",
"integrity": "sha512-Q0IoLGSRlxAJYBc4aF9eZRdIWigD0+W1we9EVQd7nn4MJBN290VEUVlE88+ZBkjrrE4Wq7E0YGyVlsDwzMQh7Q==",
"version": "1.0.0-next.37",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-1.0.0-next.37.tgz",
"integrity": "sha512-3d3JSSZbBjPnlqzx8LJw6g+Ti4LL96FH/TTOqFCP9D4kbux2gN0KaUdHzI66G6S6vhsxBKOmUyACJcOuH3eBDQ==",
"dependencies": {
"@oceanprotocol/contracts": "1.0.0-alpha.28",
"bignumber.js": "^9.0.2",
@ -21572,15 +21572,16 @@
"integrity": "sha512-M/yyKfpWmMRHWPTjvKlrRWUcIbfkRWyHhjHUI1kPggJPPX6ADxApTwtzwVXJ/+WyegcaYc7bqwuclqvg9XPqBQ=="
},
"@oceanprotocol/lib": {
"version": "1.0.0-next.36",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-1.0.0-next.36.tgz",
"integrity": "sha512-Q0IoLGSRlxAJYBc4aF9eZRdIWigD0+W1we9EVQd7nn4MJBN290VEUVlE88+ZBkjrrE4Wq7E0YGyVlsDwzMQh7Q==",
"version": "1.0.0-next.37",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-1.0.0-next.37.tgz",
"integrity": "sha512-3d3JSSZbBjPnlqzx8LJw6g+Ti4LL96FH/TTOqFCP9D4kbux2gN0KaUdHzI66G6S6vhsxBKOmUyACJcOuH3eBDQ==",
"requires": {
"@oceanprotocol/contracts": "1.0.0-alpha.28",
"bignumber.js": "^9.0.2",
"cross-fetch": "^3.1.5",
"crypto-js": "^4.1.1",
"decimal.js": "^10.3.1",
"web3": "^1.7.3",
"web3-core": "^1.7.1",
"web3-eth-contract": "^1.7.1"
}
@ -21683,6 +21684,7 @@
"integrity": "sha512-5vwpq6kbvwkQwKqAoOU3L72GZ3Ta8RRrewKj9OJRolx28KLJJ8Dg9Rf7obRwt5jQA9bkYd8gqzMTrI7H3xLfaw==",
"dev": true,
"requires": {
"@oclif/config": "^1.15.1",
"@oclif/errors": "^1.3.3",
"@oclif/parser": "^3.8.3",
"@oclif/plugin-help": "^3",

View File

@ -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",

View File

@ -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
}

View File

@ -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
}

View File

@ -28,7 +28,8 @@ import {
getAlgorithmAssetSelectionList,
getAlgorithmsForAsset,
getValidUntilTime,
getComputeEnviroment
getComputeEnviroment,
checkComputeResourcesValidity
} from '@utils/compute'
import { AssetSelectionAsset } from '@shared/FormFields/AssetSelection'
import AlgorithmDatasetsListForCompute from './AlgorithmDatasetsListForCompute'
@ -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'
@ -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 ||
@ -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,
@ -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,