oceanprotocol / ocean-node

Apache License 2.0
19 stars 8 forks source link

DDO: Stats and Prices for exchanges/dispensers #525

Open alexcos20 opened 2 months ago

alexcos20 commented 2 months ago

In order to proper display assets in dApps, we need all info regarding assets, including prices. Every datatoken can have one or more fixed rate exchanges or dispensers. But we need to be careful when adding this to a DDO, cause this can quickly result to hash missmatches.

To Do:

So , a DDO can look like:

{
  "@context": [
    "https://w3id.org/did/v1"
  ],
  "id": "did:op:37f710f82f73afcb86b66a32182c2d620d2edd9eb00abb5ba5a36e943bc7cd74",
  "version": "4.1.0",
  "chainId": 137,
  "nftAddress": "0x26C693AD67d45e339A5A50a5a64963892E14EFdD",
  "metadata": {
              ...
  },
  "services": [
    {
      "name": "Download last hour of data",
      "id": "0",
      "type": "access",
      "datatokenAddress": "0x456",
     .....
    },

    {
      "name": "Download last week of data",
      "id": "1",
      "type": "access",
      "datatokenAddress": "0x34e84f653Dcb291838aa8AF8Be1E1eF30e749ba0",
     .....
    },
    {
      "name": "Download last month of data",
      "id": "2",
      "type": "access",
      "datatokenAddress": "0x1234565",
     .....
    }

  ],
 offchain: {
    stats: {
        [
            {
                datatokenAddress: "0x456",
                name: "BDT1",
                symbol: "DT1",
                serviceId: "0",
                orders: 1,
                // this service has one dispenser available
                prices: [
                    {  type: "dispenser", price: "0", contract: "dispenserContractAddress"}]

            },
            {
                datatokenAddress: "0x34e84f653Dcb291838aa8AF8Be1E1eF30e749ba0",
                name: "BDT2",
                symbol: "DT2",
                serviceId: "1",
                orders: 5,
                // this service accepts OCEAN for payment, costs 1 token per access
                prices:[
                            {  type: "fixedrate", price: "1", token:"0x967da4048cD07aB37855c090aAF366e4ce1b9F48", contract: "freContractAddress",  exchangeId:  "23434" }
                        ]
            },
            {
                datatokenAddress: "0x1234565",
                name: "BDT3",
                symbol: "DT3",
                serviceId: "2",
                orders: 5,
                // this service accepts either 2 OCEAN or 1 FETCH for payment
                prices:[  
                   {  type: "fixedrate", price: "2", token:"0x967da4048cD07aB37855c090aAF366e4ce1b9F48", contract: "freContractAddress",  exchangeId:  "23434" },
                  {  type: "fixedrate", price: "1", token:"0xaea46A60368A7bD060eec7DF8CBa43b7EF41Ad85", contract: "freContractAddress",  exchangeId:  "4535" }
             ]
            }
        ]
    }
}            
}

To do:

-[ ] exclude "offchain" from every hash compute

-[ ] for MetaDataCreated:

- For each service:
    - get the list of dispensers (call getDispensers() - https://github.com/oceanprotocol/contracts/blob/main/contracts/templates/ERC20TemplateEnterprise.sol#L1107C14-L1107C27) from datatoken contract
        - for each dispenser, check if active. if yes, add it to the list
    - get the list of fixed rates (call getFixedRates())
        - for each fixed rate, check if active and grab the price, then add it to the list
- Update stats key

-[ ] for MetaDataUpdated:

- For each service:
    - get the list of dispensers (call getDispensers()) from datatoken contract
        - for each dispenser, check if active. if yes, add it to the list
    - get the list of fixed rates (call getFixedRates())
        - for each fixed rate, check if active and grab the price, then add it to the list
- Remove services from stats which are no longer present
- Update stats key

-[ ] indexer: add monitor for DispenserActivated and DispenserDeactivated events (https://github.com/oceanprotocol/contracts/blob/main/contracts/pools/dispenser/Dispenser.sol#L39-L45). If such an event is detected, get all ddos with a match and update it (remove dispenser/add dispenser)

-[ ] indexer: add monitor for ExchangeActivated and ExchangeDeactivated events (https://github.com/oceanprotocol/contracts/blob/main/contracts/pools/fixedRate/FixedRateExchange.sol#L103-L111). If such an event is detected, get all ddos with a match and update it (remove dispenser/add fixed rate)

-[ ] indexer: add monitor for ExchangeRateChanged event(https://github.com/oceanprotocol/contracts/blob/main/contracts/pools/fixedRate/FixedRateExchange.sol#L90C11-L94). If such an event is detected, get all ddos with a match and update them with the new rate

-[ ] indexer: add monitor for OrderStarted event(https://github.com/oceanprotocol/contracts/blob/main/contracts/templates/ERC20TemplateEnterprise.sol#L85-L93). If such an event is detected, get all ddos with a match and update orders count

FilipMasar commented 1 month ago

Updated DDO type (based on what we talked about at EOC meeting)

interface TokenInfo {
  address: string
  name: string
  symbol: string
  decimals?: number
}

interface ServicePrice {
  type: 'fixedrate' | 'dispenser'
  price: string
  contract: string
  token?: TokenInfo
  exchangeId?: string
}

interface ServiceStat {
  datatokenAddress: string
  name: string
  symbol: string
  serviceId: string
  orders: number
  prices: ServicePrice[]
}

interface OffChain {
  stats: {
    services: ServiceStat[]
  }
}

interface AssetExtended extends Asset {
  offchain: OffChain
}