first cut, still WIP

This commit is contained in:
alexcos20 2023-05-14 15:42:09 +03:00
parent 2ca1280328
commit 43283e5935
6 changed files with 2314 additions and 28 deletions

2038
abis/ERC20Template3.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -98,6 +98,9 @@ type Nft @entity{
"template address"
template: String!
"template ID of the datatoken"
templateId: Int
"set if NFT is transferable"
transferable: Boolean!
@ -630,3 +633,113 @@ type NftTransferHistory @entity {
timestamp: Int!
block: Int!
}
type PredictSubscription @entity{
"id = {contract address}-{txid}-{eventIndex}"
id: ID!
predictContract: PredictContract!
user: User!
expireTime: BigInt!
"txId"
txId: String
eventIndex:Int!
timestamp: Int!
block: Int!
}
enum PredictSlotStatus {
Pending,
Paying,
Canceled
}
type PredictPayout @entity{
"id = {contract address}-{slot}-{user}"
id: ID!
prediction: PredictPrediction!
payout: BigDecimal!
predictedValue: Boolean!
trueValue: Boolean!
aggregatedPredictedValue: BigDecimal!
"txId"
txId: String
eventIndex:Int!
timestamp: Int!
block: Int!
}
type PredictPrediction @entity{
"id = {contract address}-{slot}-{user}"
id: ID!
slot: PredictSlot!
user: User!
stake: BigDecimal!
payout: PredictPayout!
"txId"
txId: String
eventIndex:Int!
timestamp: Int!
block: Int!
}
type PredictTrueVals @entity{
"id = {contract address}-{slot}"
id: ID!
slot: PredictSlot!
trueValue: Boolean!
"txId"
txId: String
eventIndex:Int!
timestamp: Int!
block: Int!
submiter: String
}
type PredictSlot @entity{
"id = {contract address}-{slot}"
id: ID!
predictContract: PredictContract!
slot: BigInt!
predictions:[PredictPrediction!] @derivedFrom(field: "slot")
trueValues: [PredictTrueVals!] @derivedFrom(field: "slot")
revenue: BigDecimal!
status:PredictSlotStatus!
}
type PredictSettingUpdate @entity{
"id = {contract address}-{txId}-{eventIndex}"
id: ID!
predictContract: PredictContract!
blocksPerEpoch: BigInt!
blocksPerSubscription: BigInt!
truevalSubmitTimeoutBlock: BigInt!
stakeToken: Token
"txId"
txId: String
eventIndex:Int!
timestamp: Int!
block: Int!
}
type PredictContract @entity{
"id = {contract address}"
id: ID!
token: Token!
slots: [PredictSlot!] @derivedFrom(field: "predictContract")
settingUpdates: [PredictSettingUpdate!] @derivedFrom(field: "predictContract")
subscriptions: [PredictSubscription!] @derivedFrom(field: "predictContract")
blocksPerEpoch: BigInt!
blocksPerSubscription: BigInt!
truevalSubmitTimeoutBlock: BigInt!
stakeToken: Token
"creation txId"
txId: String
"timestamp of creation tx"
timestamp: Int!
"block of creation tx"
block: Int!
eventIndex: Int!
}

View File

@ -0,0 +1,74 @@
import {
PredictSubscription,
PredictPayout,
PredictPrediction,
PredictTrueVals,
PredictSlot,
PredictSettingUpdate,
PredictContract
} from '../@types/schema'
import { BigInt, BigDecimal, Address, log } from '@graphprotocol/graph-ts'
import {
PredictionSubmitted,
PredictionPayout,
NewSubscription,
TruevalSubmitted,
SettingChanged,
RevenueAdded
} from '../@types/templates/ERC20Template3/ERC20Template3'
import { integer } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import { getPredictContract, getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
export function handlePredictionSubmitted(event: PredictionSubmitted): void {
// TODO
}
export function handlePredictionPayout(event: PredictionPayout): void {
// TODO
}
export function handleNewSubscription(event: NewSubscription): void {
// TODO
}
export function handleTruevalSubmitted(event: TruevalSubmitted): void {
// TODO
}
export function handleSettingChanged(event: SettingChanged): void {
const predictContract = getPredictContract(event.address)
predictContract.blocksPerEpoch = event.params.blocksPerEpoch
predictContract.blocksPerSubscription = event.params.blocksPerSubscription
predictContract.truevalSubmitTimeoutBlock =
event.params.trueValueSubmitTimeoutBlock
const stakeToken = getToken(event.params.stakeToken, false)
predictContract.stakeToken = stakeToken.id
predictContract.save()
const predictSettingsUpdate = new PredictSettingUpdate(
event.address.toHexString() +
'- ' +
event.transaction.hash.toHexString() +
'-' +
event.logIndex.toHexString()
)
predictSettingsUpdate.block = event.block.number.toI32()
predictSettingsUpdate.txId = event.transaction.hash.toHexString()
predictSettingsUpdate.eventIndex = event.logIndex.toI32()
predictSettingsUpdate.timestamp = event.block.timestamp.toI32()
predictSettingsUpdate.predictContract = predictContract.id
predictSettingsUpdate.blocksPerEpoch = event.params.blocksPerEpoch
predictSettingsUpdate.blocksPerSubscription =
event.params.blocksPerSubscription
predictSettingsUpdate.truevalSubmitTimeoutBlock =
event.params.trueValueSubmitTimeoutBlock
predictSettingsUpdate.stakeToken = stakeToken.id
predictSettingsUpdate.save()
}
export function handleRevenueAdded(event: RevenueAdded): void {
// TODO
}

View File

@ -1,15 +1,12 @@
import {
NFTCreated,
TokenCreated,
ERC721Factory
} from '../@types/ERC721Factory/ERC721Factory'
import { NFTCreated, TokenCreated } from '../@types/ERC721Factory/ERC721Factory'
import { ERC721Template } from '../@types/templates/ERC721Template/ERC721Template'
import { ERC20Template } from '../@types/templates/ERC20Template/ERC20Template'
import { decimal } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import { getUser } from './utils/userUtils'
import { getToken, getNftToken } from './utils/tokenUtils'
import { getToken, getNftToken, getPredictContract } from './utils/tokenUtils'
import { addDatatoken } from './utils/globalUtils'
import { BigInt } from '@graphprotocol/graph-ts'
export function handleNftCreated(event: NFTCreated): void {
// const nft = new Nft(event.params.newTokenAddress.toHexString())
@ -27,7 +24,12 @@ export function handleNftCreated(event: NFTCreated): void {
nft.block = event.block.number.toI32()
nft.eventIndex = event.logIndex.toI32()
nft.transferable = event.params.transferable
// get token id
const contract = ERC721Template.bind(event.params.newTokenAddress)
const contractTemplate = contract.try_getId()
if (!contractTemplate.reverted) {
nft.templateId = contractTemplate.value
}
nft.save()
}
@ -49,25 +51,20 @@ export function handleNewToken(event: TokenCreated): void {
token.decimals = 18
token.supply = decimal.ZERO
token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18)
const eventTemplateAddress = event.params.templateAddress
.toHexString()
.toLowerCase()
const contract = ERC721Factory.bind(event.address)
const templateCount = contract.try_getCurrentTemplateCount()
if (templateCount.reverted) return
const templateCountNum = templateCount.value.toI32()
for (let i = 0; i < templateCountNum; i++) {
const template = contract.try_getTokenTemplate(BigInt.fromI32(1 + i))
if (template.reverted) return
const templateAddress = template.value.templateAddress
.toHexString()
.toLowerCase()
if (templateAddress == eventTemplateAddress) {
token.templateId = 1 + i
}
// get token id
const contract = ERC20Template.bind(event.params.newTokenAddress)
const contractTemplate = contract.try_getId()
if (!contractTemplate.reverted) {
token.templateId = contractTemplate.value
}
token.save()
addDatatoken()
if (token.templateId == 3) {
const predictContract = getPredictContract(event.params.newTokenAddress)
predictContract.timestamp = event.block.timestamp.toI32()
predictContract.txId = event.transaction.hash.toHex()
predictContract.block = event.block.number.toI32()
predictContract.eventIndex = event.logIndex.toI32()
predictContract.save()
}
}

View File

@ -1,7 +1,11 @@
import { Address, log, BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { Nft, Token } from '../../@types/schema'
import { Nft, Token, PredictContract } from '../../@types/schema'
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
import { ERC20Template, ERC721Template } from '../../@types/templates'
import {
ERC20Template,
ERC721Template,
ERC20Template3
} from '../../@types/templates'
import { addNft } from './globalUtils'
import { ZERO_ADDRESS } from './constants'
@ -31,6 +35,7 @@ export function createToken(address: Address, isDatatoken: boolean): Token {
token.block = 0
token.tx = ''
token.eventIndex = 0
token.templateId = 0
token.save()
return token
}
@ -55,6 +60,7 @@ export function createNftToken(address: Address): Nft {
token.creator = ''
token.assetState = 0
token.template = ''
token.templateId = 0
token.transferable = true
token.createdTimestamp = 0
token.block = 0
@ -109,3 +115,28 @@ export function getUSDValue(
): BigDecimal {
return BigDecimal.zero()
}
export function createPredictContract(address: Address): PredictContract {
ERC20Template3.create(address)
const predictContract = new PredictContract(address.toHexString())
const token = getToken(address, true)
predictContract.token = token.id
predictContract.blocksPerEpoch = BigInt.zero()
predictContract.blocksPerSubscription = BigInt.zero()
predictContract.truevalSubmitTimeoutBlock = BigInt.zero()
predictContract.stakeToken = null
predictContract.txId = ''
predictContract.timestamp = 0
predictContract.block = 0
predictContract.eventIndex = 0
predictContract.save()
return predictContract
}
export function getPredictContract(address: Address): PredictContract {
let newPredictContract = PredictContract.load(address.toHexString())
if (newPredictContract === null) {
newPredictContract = createPredictContract(address)
}
return newPredictContract
}

View File

@ -54,6 +54,39 @@ templates:
handler: handleProviderFee
receipt: true
- name: ERC20Template3
kind: ethereum/contract
network: __NETWORK__
source:
abi: ERC20Template3
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
file: ./src/mappings/erc20Template3.ts
entities:
- ERC20Template3
abis:
- name: ERC20Template3
file: ./abis/ERC20Template3.json
- name: ERC20
file: ./abis/ERC20.json
- name: ERC20Roles
file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/utils/ERC20Roles.sol/ERC20Roles.json
eventHandlers:
- event: PredictionSubmitted(indexed address,indexed uint256,uint256)
handler: handlePredictionSubmitted
- event: PredictionPayout(indexed address,indexed uint256,uint256,uint256,bool,bool,uint256,uint8)
handler: handlePredictionPayout
- event: NewSubscription(indexed address,uint256,uint256)
handler: handleNewSubscription
- event: TruevalSubmitted(indexed uint256,bool,uint8)
handler: handleTruevalSubmitted
- event: SettingChanged(uint256,uint256,uint256,address)
handler: handleSettingChanged
- event: RevenueAdded(uint256,uint256,uint256,uint256,uint256)
handler: handleRevenueAdded
- name: ERC721Template
kind: ethereum/contract
network: __NETWORK__