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

70 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Address, log, BigDecimal } from '@graphprotocol/graph-ts'
2022-02-18 12:09:18 +01:00
import { Nft, Token } from '../../@types/schema'
2021-11-10 13:47:44 +01:00
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
2022-02-18 12:09:18 +01:00
import { ERC20Template, ERC721Template } from '../../@types/templates'
import { addNft } from './globalUtils'
import { ZERO_ADDRESS } from './constants'
2021-11-10 13:47:44 +01:00
2022-02-18 12:09:18 +01:00
export function createToken(address: Address, isDatatoken: boolean): Token {
log.debug('started creating token with address: {}', [address.toHexString()])
if (isDatatoken) {
ERC20Template.create(address)
}
const token = new Token(address.toHexString())
const contract = ERC20.bind(address)
2022-02-21 12:06:20 +01:00
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
2022-02-18 12:09:18 +01:00
token.address = address.toHexString()
token.isDatatoken = isDatatoken
2022-02-21 12:06:20 +01:00
const decimals = contract.try_decimals()
if (decimals.reverted) token.decimals = 18
else token.decimals = decimals.value
token.lastPriceToken = ZERO_ADDRESS
token.lastPriceValue = BigDecimal.zero()
2021-11-10 13:47:44 +01:00
token.save()
return token
}
2022-02-18 12:09:18 +01:00
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 {
log.debug('started creating nft token with address: {}', [
address.toHexString()
])
ERC721Template.create(address)
const token = new Nft(address.toHexString())
// const contract = ERC721Template.bind(address)
token.name = ''
token.symbol = ''
token.address = address.toHexString()
token.save()
addNft()
return token
}
export function getNftToken(address: Address): Nft {
let newToken = Nft.load(address.toHexString())
2021-11-10 13:47:44 +01:00
if (newToken === null) {
2022-02-18 12:09:18 +01:00
newToken = createNftToken(address)
2021-11-10 13:47:44 +01:00
}
return newToken
}
export function getUSDValue(
address: string,
value: BigDecimal,
timestamp: number
): BigDecimal {
return BigDecimal.zero()
}