Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
common: Add 'zero'/'empty' static methods
Browse files Browse the repository at this point in the history
These methods are being added for:

- Being helpful/useful to subgraph developers
- Ease some zero/empty value initialization in graph-cli
  • Loading branch information
evaporei committed Aug 23, 2021
1 parent 6fca885 commit 005eb02
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions common/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class ByteArray extends Uint8Array {
return self
}

static empty(): ByteArray {
return ByteArray.fromI32(0)
}

/**
* Input length must be even.
*/
Expand Down Expand Up @@ -146,6 +150,10 @@ export class Bytes extends ByteArray {
static fromUint8Array(uint8Array: Uint8Array): Bytes {
return changetype<Bytes>(uint8Array)
}

static empty(): Bytes {
return changetype<Bytes>(ByteArray.empty())
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions common/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export class Address extends Bytes {
static fromString(s: string): Address {
return changetype<Address>(typeConversion.stringToH160(s))
}

static zero(): Address {
let self = new ByteArray(20)

for (let i = 0; i < 20; i++) {
self[i] = 0
}

return changetype<Address>(self)
}
}

/** An arbitrary size integer represented as an array of bytes. */
Expand All @@ -43,6 +53,10 @@ export class BigInt extends Uint8Array {
return BigInt.fromByteArray(byteArray)
}

static zero(): BigInt {
return BigInt.fromI32(0)
}

/**
* `bytes` assumed to be little-endian. If your input is big-endian, call `.reverse()` first.
*/
Expand Down Expand Up @@ -280,6 +294,10 @@ export class BigDecimal {
return bigDecimal.fromString(s)
}

static zero(): BigDecimal {
return new BigDecimal(BigInt.zero())
}

toString(): string {
return bigDecimal.toString(this)
}
Expand Down

0 comments on commit 005eb02

Please sign in to comment.