oceanprotocol/ocean.js

Buying a datatoken from the marketplace not working, exchangeId throwing error

smejak opened this issue · 2 comments

Describe the bug
I am building a streamlit component that integrates MetaMask & Ocean Market with Streamlit apps. I have been following the CodeExamples & trying to extract the relevant code for buying a dataset from the marketplace, however, I ran into issues with the fixedRate exchange & the exchageId (also called freId in the CodeExamples).

My current ocean.js code is as follows:

    const web3 = new Web3(window.ethereum)

    const config: any = await getTestConfig(web3)
    console.log("config", config)

    const aquarius = new Aquarius(config.metadataCacheUri)
    const providerUrl = config.providerUri

    const datatoken = new Datatoken(web3)
    const DATATOKEN_AMOUNT = '1'

    console.log("userAddress", userAddress[0])

    const consumerETHBalance = await web3.eth.getBalance(userAddress[0])
    console.log(`Consumer ETH balance: ${consumerETHBalance}`)

    let consumerOCEANBalance = await balance(web3, config.oceanTokenAddress, userAddress[0])
    console.log(`Consumer OCEAN balance before swap: ${consumerOCEANBalance}`)

    console.log('Did', did)
    const dt = await aquarius.resolve(did)
    console.log("dt", dt)

    const dtAddress = dt.datatokens[0].address
    console.log("dtAddress", dtAddress)
    let consumerDTBalance = await balance(web3, dtAddress, userAddress[0])
    console.log(`Consumer ${dt.datatokens[0].symbol} balance before swap: ${consumerDTBalance}`)
    // const r: any = await query(dtAddress)
    // console.log("r", r)
    const fixedRate = new FixedRateExchange(web3, dtAddress)
    const oceanAmount = await (
      await fixedRate.calcBaseInGivenOutDT("0x00344ca3524adda1a0b331585d2bf6f759d4a4014dc6476c3c55176e8e4b2ce2", '1')
    ).baseTokenAmount
    console.log(`Ocean amount: ${oceanAmount}`)

    await fixedRate.buyDT(userAddress[0], "0x00344ca3524adda1a0b331585d2bf6f759d4a4014dc6476c3c55176e8e4b2ce2", '1', '2')

The freId is currently hardcoded as 0x00344ca3524adda1a0b331585d2bf6f759d4a4014dc6476c3c55176e8e4b2ce2 which I found using a GraphQL query described here: https://docs.oceanprotocol.com/building-with-ocean/using-ocean-subgraph/get-datatoken-information. The error I am getting at the oceanAmount line is:

Uncaught (in promise) Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.

All the code before that line works fine, the user address is correctly registered from MetaMask, but I'm not sure how to get the correct fixed rate exchange ID, because aquarius.resolve(did) does not give information on it and when I tried using a GraphQL query, that freId resulted in the error above.

Note: I was trying this on this dataset https://market.oceanprotocol.com/asset/did:op:dd4210c0b736b46d919c2fcaf2e6596936610669fab59d558b309a620b9a820f which I thought should be functioning.

To Reproduce
Steps to reproduce the behavior:

  1. In a web app where MetaMask is enabled & connected, run the code above
  2. See error

Expected behavior
A transaction is prompted in MetaMask to approve buying the datatoken.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: macOS 12.3
  • Browser Brave

Update: I got up to ProviderInstance.computeStart in the compute flow, there it crashes with a Compute start failed: 400 from the post request to https://v4.provider.rinkeby.oceanprotocol.com/api/services/compute. I'm not sure whether this is an issue with my code since both the data token & algorithm token get transferred out of my wallet and all the transactions have been successful.

I've also tried requesting the provider directly with requests.post('https://v4.provider.rinkeby.oceanprotocol.com/api/services/compute', payload).content where the payload was generated by ocean.js. I am getting the error b'{"errors":{"dataset.documentId":["The dataset.documentId field is required."]},"message":"Validation error","success":false}\n' even though my payload definitely has a dataset.documentId.

...
"dataset": {
        "documentId": "did:op:5083e25e54171b872b528ef59408b9b12a5c938994546ade55aa64dfeeb174de",
...

The code for the project is available here: https://github.com/AlgoveraAI/streamlit-ocean/blob/main/streamlit_ocean/frontend/src/OceanCompute.tsx#L160

@smejak are you still facing this issue? As far as I can tell it is working