import { Address , BigDecimal , BigInt , Bytes } from '@graphprotocol/graph-ts' ; import { ADDRESS_LENGTH , DEFAULT_DECIMALS , ZERO_ADDRESS } from './constants' ; export namespace address { export function isZeroAddress ( address ) { return address . toHexString ( ) == ZERO_ADDRESS ; } } export namespace bytes { export function toAddress ( address ) { return Address . fromHexString ( address . toHexString ( ) ) . subarray ( - ADDRESS_LENGTH ) as Address ; } export function toSignedInt ( value , bigEndian = true ) { return BigInt . fromSignedBytes ( bigEndian ? ( value . reverse ( ) as Bytes ) : value ) ; } export function toUnsignedInt ( value , bigEndian = true ) { return BigInt . fromUnsignedBytes ( bigEndian ? ( value . reverse ( ) as Bytes ) : value ) ; } export function hexZeroPad ( value , length = 32 ) { let hexstring = value . toHexString ( ) ; return hexstring . substr ( 0 , 2 ) + hexstring . substr ( 2 ) . padStart ( length * 2 , '0' ) ; } } export namespace decimal { export let NEGATIVE_ONE = BigDecimal . fromString ( '-1' ) ; export let ZERO = BigDecimal . fromString ( '0' ) ; export let ONE = BigDecimal . fromString ( '1' ) ; export let TWO = BigDecimal . fromString ( '2' ) ; export let ONE_HUNDRED = BigDecimal . fromString ( '100' ) ; export function fromBigInt ( value , decimals = DEFAULT_DECIMALS ) { let precision = BigInt . fromI32 ( 10 ) . pow ( < u8 > decimals ) . toBigDecimal ( ) ; return value . divDecimal ( precision ) ; } export function fromNumber ( value ) { return fromString ( value . toString ( ) ) ; } export function fromString ( value ) { return BigDecimal . fromString ( value ) ; } export function toBigInt ( value , decimals = DEFAULT_DECIMALS ) { return value . times ( getPrecision ( decimals ) . toBigDecimal ( ) ) . truncate ( 0 ) . digits ; } export function getPrecision ( decimals = DEFAULT_DECIMALS ) { return BigInt . fromI32 ( 10 ) . pow ( decimals ) ; } export function min ( a , b ) { return BigDecimal . compare ( a , b ) < 1 ? a : b ; } export function max ( a , b ) { return BigDecimal . compare ( a , b ) > 1 ? a : b ; } } export namespace integer { export let NEGATIVE_ONE = BigInt . fromI32 ( - 1 ) ; export let ZERO = BigInt . fromI32 ( 0 ) ; export let ONE = BigInt . fromI32 ( 1 ) ; export let TWO = BigInt . fromI32 ( 2 ) ; export let ONE_THOUSAND = BigInt . fromI32 ( 1000 ) ; export let WEI_PER_ETHER = BigInt . fromI32 ( < i32 > 1000000000000000000 ) ; export function fromNumber ( value ) { return BigInt . fromI32 ( value ) ; } export function fromString ( value ) { return fromNumber ( parseI32 ( value ) ) ; } export function toBytes ( value ) { return ( value as Uint8Array ) as Bytes ; } export function decrement ( value , amount = ONE ) { return value . minus ( amount ) ; } export function increment ( value , amount = ONE ) { return value . plus ( amount ) ; } export function min ( a , b ) { return BigInt . compare ( a , b ) < 1 ? a : b ; } export function max ( a , b ) { return BigInt . compare ( a , b ) > 1 ? a : b ; } } export namespace units { export let WAD = BigDecimal . fromString ( '1000000000000000000' ) ; export let RAY = BigDecimal . fromString ( '1000000000000000000000000000' ) ; export let RAD = BigDecimal . fromString ( '1000000000000000000000000000000000000000000000' ) ; export function fromRad ( value ) { return value . divDecimal ( RAD ) ; } export function fromRay ( value ) { return value . divDecimal ( RAY ) ; } export function fromWad ( value ) { return value . divDecimal ( WAD ) ; } export function toRad ( value ) { return value . times ( RAD ) . truncate ( 0 ) . digits ; } export function toRay ( value ) { return value . times ( RAY ) . truncate ( 0 ) . digits ; } export function toWad ( value ) { return value . times ( WAD ) . truncate ( 0 ) . digits ; } }