Skip to content

Commit

Permalink
Fix token setup
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Sep 5, 2024
1 parent e81c4c3 commit e474a14
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
41 changes: 21 additions & 20 deletions src/common/entity/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@ import { ZERO_BI } from "../utils/decimal"
export function isNullToken(token: Token): boolean {
return token.id.equals(ADDRESS_ZERO)
}

export function getNullToken(): Token {
let token = Token.load(ADDRESS_ZERO)
if (!token) {
token = new Token(ADDRESS_ZERO)
token.symbol = "NULL"
token.name = "NULL"
token.decimals = BigInt.fromI32(18)
token.save()
}
function createNullTokenObject(): Token {
const token = new Token(ADDRESS_ZERO)
token.symbol = "NULL"
token.name = "NULL"
token.decimals = BigInt.fromI32(18)
return token
}

export function getToken(tokenAddress: Bytes): Token {
if (tokenAddress == ADDRESS_ZERO) {
return getNullToken()
}
let token = Token.load(tokenAddress)
let token = getTokenIfExists(tokenAddress)
if (!token) {
token = new Token(tokenAddress)
token.symbol = ""
token.name = ""
token.decimals = BigInt.fromI32(18)
token = createNullTokenObject()
}
return token
}

export function isNewToken(token: Token): boolean {
return token.symbol == "" && token.name == ""
export function getTokenIfExists(tokenAddress: Bytes): Token | null {
return Token.load(tokenAddress)
}

export function createTokenObject(tokenAddress: Bytes): Token {
if (tokenAddress == ADDRESS_ZERO) {
return createNullTokenObject()
}

const token = new Token(tokenAddress)
token.symbol = ""
token.name = ""
token.decimals = BigInt.fromI32(18)
return token
}

export function getTokenStatistic(tokenAddress: Bytes): TokenStatistic {
Expand Down
7 changes: 4 additions & 3 deletions src/common/utils/token.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IERC20 as IERC20Contract } from "../../../generated/templates/BeefyERC20Product/IERC20"
import { Token } from "../../../generated/schema"
import { Address, BigInt, Bytes } from "@graphprotocol/graph-ts"
import { getToken, isNewToken } from "../entity/token"
import { createTokenObject, getToken, getTokenIfExists } from "../entity/token"

export function fetchAndSaveTokenData(tokenAddress: Bytes): Token {
const token = getToken(tokenAddress)
if (!isNewToken(token)) {
let token = getTokenIfExists(tokenAddress)
if (token) {
return token
}

Expand All @@ -22,6 +22,7 @@ export function fetchAndSaveTokenData(tokenAddress: Bytes): Token {
const tokenName = tokenNameRes.reverted ? "Unknown" : tokenNameRes.value
const tokenSymbol = tokenSymbolRes.reverted ? "UNKNOWN" : tokenSymbolRes.value

token = createTokenObject(tokenAddress)
token.name = tokenName
token.symbol = tokenSymbol
token.decimals = BigInt.fromI32(tokenDecimals)
Expand Down

0 comments on commit e474a14

Please sign in to comment.