ocean-subgraph/src/mappings/utils/tokenUtils.ts

157 lines
4.4 KiB
TypeScript

import { Address, log, BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import {
Nft,
Token,
PredictContract,
Erc721Template,
Erc20Template
} from '../../@types/schema'
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
import { ERC721Template } from '../../@types/templates'
import { addNft } from './globalUtils'
import { ZERO_ADDRESS } from './constants'
export function createToken(address: Address, isDatatoken: boolean): Token {
log.debug('started creating token with address: {}', [address.toHexString()])
const token = new Token(address.toHexString())
const contract = ERC20.bind(address)
const name = contract.try_name()
if (name.reverted) token.name = ''
else token.name = name.value
const symbol = contract.try_symbol()
if (name.reverted) token.symbol = ''
else token.symbol = symbol.value
token.address = address.toHexString()
token.isDatatoken = isDatatoken
const decimals = contract.try_decimals()
if (decimals.reverted) token.decimals = 18
else token.decimals = decimals.value
token.lastPriceToken = ZERO_ADDRESS
token.lastPriceValue = BigDecimal.zero()
token.orderCount = BigInt.zero()
token.holderCount = BigInt.zero()
token.createdTimestamp = 0
token.block = 0
token.tx = ''
token.eventIndex = 0
token.templateId = BigInt.zero()
token.save()
return token
}
export function getToken(address: Address, isDatatoken: boolean): Token {
let newToken = Token.load(address.toHexString())
if (newToken === null) {
newToken = createToken(address, isDatatoken)
}
return newToken
}
export function createNftToken(address: Address): Nft {
ERC721Template.create(address)
const token = new Nft(address.toHexString())
token.name = ''
token.symbol = ''
token.address = address.toHexString()
token.providerUrl = ''
token.tokenUri = ''
token.owner = ''
token.creator = ''
token.assetState = 0
token.template = ''
token.transferable = true
token.createdTimestamp = 0
token.block = 0
token.tx = ''
token.orderCount = BigInt.zero()
token.hasMetadata = false
token.eventIndex = 0
token.save()
addNft()
return token
}
export function getNftToken(address: Address): Nft {
let newToken = Nft.load(address.toHexString())
if (newToken === null) {
newToken = createNftToken(address)
}
return newToken
}
export function getNftTokenWithID(tokenId: string): Nft {
let nftToken = Nft.load(tokenId)
if (nftToken === null) {
nftToken = new Nft(tokenId)
// const contract = ERC721Template.bind(address)
nftToken.name = ''
nftToken.symbol = ''
nftToken.address = tokenId
nftToken.providerUrl = ''
nftToken.tokenUri = ''
nftToken.owner = ''
nftToken.creator = ''
nftToken.assetState = 0
nftToken.template = ''
nftToken.transferable = true
nftToken.createdTimestamp = 0
nftToken.block = 0
nftToken.tx = ''
nftToken.orderCount = BigInt.zero()
nftToken.hasMetadata = false
nftToken.eventIndex = 0
nftToken.save()
addNft()
}
return nftToken
}
export function getUSDValue(
address: string,
value: BigDecimal,
timestamp: number
): BigDecimal {
return BigDecimal.zero()
}
export function getErc721TemplateId(address: Address): BigInt {
const template = Erc721Template.load(address.toHexString())
if (template) {
return template.templateId
}
return BigInt.zero()
}
export function getErc20TemplateId(address: Address): BigInt {
const template = Erc20Template.load(address.toHexString())
if (template) {
return template.templateId
}
return BigInt.zero()
}
export function createPredictContract(address: Address): PredictContract {
const predictContract = new PredictContract(address.toHexString())
const token = getToken(address, true)
predictContract.token = token.id
predictContract.secondsPerEpoch = BigInt.zero()
predictContract.secondsPerSubscription = BigInt.zero()
predictContract.truevalSubmitTimeout = BigInt.zero()
predictContract.stakeToken = null
predictContract.txId = ''
predictContract.timestamp = 0
predictContract.block = 0
predictContract.eventIndex = 0
predictContract.paused = false
predictContract.save()
return predictContract
}
export function getPredictContract(address: Address): PredictContract {
let newPredictContract = PredictContract.load(address.toHexString())
if (newPredictContract === null) {
newPredictContract = createPredictContract(address)
}
return newPredictContract
}