import { BigInt , BigDecimal , Bytes , ByteArray , crypto , log , Value } from '@graphprotocol/graph-ts' ; export function zeroBD ( ) { return BigDecimal . fromString ( '0' ) ; } export function zeroBI ( ) { return BigInt . fromI32 ( 0 ) ; } export function zeroAddress ( ) { return Bytes . fromHexString ( '0x0000000000000000000000000000000000000000' ) as Bytes ; } export function exponentToBigDecimal ( decimals ) { let bd = BigDecimal . fromString ( '1' ) ; let bd10 = BigDecimal . fromString ( '10' ) ; for ( let i = 0 ; i < decimals ; i ++ ) { bd = bd . times ( bd10 ) ; } return bd ; } export function exponentToBigInt ( decimals ) { let bi = BigInt . fromI32 ( 1 ) ; let bi10 = BigInt . fromI32 ( 10 ) ; for ( let i = 0 ; i < decimals ; i ++ ) { bi = bi . times ( bi10 ) ; } return bi ; } export function convertTokenAmountToDecimals ( amount , decimals ) { return amount . toBigDecimal ( ) . div ( exponentToBigDecimal ( decimals ) ) ; } export function convertValueFromRay ( value ) { return convertTokenAmountToDecimals ( value , 27 ) ; } export function format18 ( price ) { if ( price == BigInt . fromI32 ( 0 ) ) return price ; return exponentToBigInt ( 18 ) . div ( price ) ; } export function formatUsdEthChainlinkPrice ( price ) { if ( price == BigInt . fromI32 ( 0 ) ) return price ; return exponentToBigInt ( 18 + 8 ) . div ( price ) ; } export const BORROW_MODE_STABLE = 'Stable' ; export const BORROW_MODE_VARIABLE = 'Variable' ; export const BORROW_MODE_NONE = 'None' ; export function getBorrowRateMode ( _mode ) { let mode = _mode . toI32 ( ) ; if ( mode == 0 ) { return BORROW_MODE_NONE ; } else if ( mode == 1 ) { return BORROW_MODE_STABLE ; } else if ( mode == 2 ) { return BORROW_MODE_VARIABLE ; } throw new Error ( 'invalid borrow rate mode' ) ; } export function getBorrowRateModeFromString ( _mode ) { if ( _mode == BORROW_MODE_NONE ) { return zeroBI ( ) ; } else if ( _mode == BORROW_MODE_STABLE ) { return BigInt . fromI32 ( 1 ) ; } else if ( _mode == BORROW_MODE_VARIABLE ) { return BigInt . fromI32 ( 2 ) ; } throw new Error ( 'invalid borrow rate mode' ) ; } export const PRICE_ORACLE_ASSET_TYPE_SIMPLE = 'Simple' ; export const PRICE_ORACLE_ASSET_TYPE_COMPOSITE = 'Composite' ; export function getPriceOracleAssetType ( _type ) { let type = _type . toI32 ( ) ; if ( type == 1 ) { return PRICE_ORACLE_ASSET_TYPE_SIMPLE ; } else if ( type == 2 ) { return PRICE_ORACLE_ASSET_TYPE_COMPOSITE ; } throw new Error ( 'invalid price oracle asset type' ) ; } export const PRICE_ORACLE_ASSET_PLATFORM_SIMPLE = 'Simple' ; export const PRICE_ORACLE_ASSET_PLATFORM_UNISWAP = 'Uniswap' ; export const PRICE_ORACLE_ASSET_PLATFORM_BALANCER = 'Balancer' ; export const PRICE_ORACLE_ASSET_PLATFORM_GELATO = 'Gelato' ; export const PRICE_ORACLE_ASSET_PLATFORM_ERROR = 'Error' ; export function getPriceOracleAssetPlatform ( _type ) { let type = _type . toI32 ( ) ; if ( type == 1 ) { return PRICE_ORACLE_ASSET_PLATFORM_SIMPLE ; } else if ( type == 2 ) { return PRICE_ORACLE_ASSET_PLATFORM_UNISWAP ; } else if ( type == 3 ) { return PRICE_ORACLE_ASSET_PLATFORM_BALANCER ; } else if ( type == 4 ) { return PRICE_ORACLE_ASSET_PLATFORM_GELATO ; } else { log . error ( 'This type is not tracked:: {}' , [ type . toString ( ) ] ) ; return PRICE_ORACLE_ASSET_PLATFORM_ERROR ; } } export function byteArrayFromHex ( s ) { if ( s . length % 2 !== 0 ) { throw new TypeError ( 'Hex string must have an even number of characters' ) ; } let out = new Uint8Array ( s . length / 2 ) ; for ( var i = 0 ; i < s . length ; i += 2 ) { out [ i / 2 ] = parseInt ( s . substring ( i , i + 2 ) , 16 ) as u32 ; } return out as ByteArray ; } export function concat ( a , b ) { let out = new Uint8Array ( a . length + b . length ) ; for ( let i = 0 ; i < a . length ; i ++ ) { out [ i ] = a [ i ] ; } for ( let j = 0 ; j < b . length ; j ++ ) { out [ a . length + j ] = b [ j ] ; } return out as ByteArray ; } const Zeros = new ByteArray ( 32 ) ; Zeros . fill ( 0 ) ; export function namehash ( partition ) { let result = Zeros ; while ( partition . length > 0 ) { let data = partition [ partition . length - 1 ] ; let label = ByteArray . fromUTF8 ( data ) ; result = crypto . keccak256 ( concat ( result , crypto . keccak256 ( label ) ) ) ; partition . pop ( ) ; } return result . toHexString ( ) ; } export function convertToLowerCase ( str ) { let result = '' ; for ( let i = 0 ; i < str . length ; i ++ ) { let code = str . charCodeAt ( i ) ; if ( code > 64 && code < 91 ) { result += String . fromCharCode ( code + 32 ) ; } else { result += str . charAt ( i ) ; } } return result ; } export function generateSymbol ( description ) { let symbolArr = description . split ( ' / ' ) ; return convertToLowerCase ( symbolArr [ 0 ] + '-' + symbolArr [ 1 ] ) ; }