Skip to content

Commit a12e04f

Browse files
committed
Initial version
1 parent 5643d61 commit a12e04f

File tree

11 files changed

+385
-0
lines changed

11 files changed

+385
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
build/
3+
package/
4+
.idea/
5+
.vscode/

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib'

lib/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//
2+
// Constants
3+
//
4+
5+
// Addresses
6+
export const GENESIS_ADDRESS = '0x0000000000000000000000000000000000000000'
7+
export const ZERO_ADDRESS = GENESIS_ADDRESS

lib/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './constants'
2+
export * from './metrics'
3+
export * from './utils'

lib/metrics.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { BigDecimal, BigInt, Entity, Value, ValueKind, store } from '@graphprotocol/graph-ts'
2+
3+
import { decimal, integer } from './utils'
4+
5+
export namespace metrics {
6+
class Accumulator extends Entity {
7+
constructor(id: string) {
8+
super()
9+
this.set('id', Value.fromString(id))
10+
}
11+
12+
save(): void {
13+
let id = this.get('id')
14+
assert(id !== null, 'Cannot save Accumulator entity without an ID')
15+
assert(
16+
id.kind == ValueKind.STRING,
17+
'Cannot save Accumulator entity with non-string ID. ' +
18+
'Considering using .toHex() to convert the "id" to a string.',
19+
)
20+
store.set('Accumulator', id.toString(), this)
21+
}
22+
23+
static load(id: string): Accumulator | null {
24+
return store.get('Accumulator', id) as Accumulator | null
25+
}
26+
27+
get id(): string {
28+
let value = this.get('id')
29+
return value.toString()
30+
}
31+
32+
set id(value: string) {
33+
this.set('id', Value.fromString(value))
34+
}
35+
36+
get total(): BigDecimal {
37+
let value = this.get('total')
38+
return value.toBigDecimal()
39+
}
40+
41+
set total(value: BigDecimal) {
42+
this.set('total', Value.fromBigDecimal(value))
43+
}
44+
45+
add(value: BigDecimal): Accumulator {
46+
this.total = this.total.plus(value)
47+
return this
48+
}
49+
50+
subtract(value: BigDecimal): Accumulator {
51+
this.total = this.total.minus(value)
52+
return this
53+
}
54+
}
55+
56+
class Counter extends Entity {
57+
constructor(id: string) {
58+
super()
59+
this.set('id', Value.fromString(id))
60+
}
61+
62+
save(): void {
63+
let id = this.get('id')
64+
assert(id !== null, 'Cannot save Counter entity without an ID')
65+
assert(
66+
id.kind == ValueKind.STRING,
67+
'Cannot save Counter entity with non-string ID. ' +
68+
'Considering using .toHex() to convert the "id" to a string.',
69+
)
70+
store.set('Counter', id.toString(), this)
71+
}
72+
73+
static load(id: string): Counter | null {
74+
return store.get('Counter', id) as Counter | null
75+
}
76+
77+
get id(): string {
78+
let value = this.get('id')
79+
return value.toString()
80+
}
81+
82+
set id(value: string) {
83+
this.set('id', Value.fromString(value))
84+
}
85+
86+
get count(): BigInt {
87+
let value = this.get('count')
88+
return value.toBigInt()
89+
}
90+
91+
set count(value: BigInt) {
92+
this.set('count', Value.fromBigInt(value))
93+
}
94+
95+
add(value: BigInt): Counter {
96+
this.count = this.count.plus(value)
97+
return this
98+
}
99+
100+
subtract(value: BigInt): Counter {
101+
this.count = this.count.minus(value)
102+
return this
103+
}
104+
105+
increase(): Counter {
106+
return this.add(integer.ONE)
107+
}
108+
109+
decrease(): Counter {
110+
return this.subtract(integer.ONE)
111+
}
112+
}
113+
114+
export function getAccumulator(id: string): Accumulator {
115+
let acc = Accumulator.load(id)
116+
117+
if (acc == null) {
118+
acc = new Accumulator(id)
119+
acc.total = decimal.ZERO
120+
}
121+
122+
return acc as Accumulator
123+
}
124+
125+
export function getCounter(counterName: string): Counter {
126+
let counter = Counter.load(counterName)
127+
128+
if (counter == null) {
129+
counter = new Counter(counterName)
130+
counter.count = integer.ZERO
131+
}
132+
133+
return counter as Counter
134+
}
135+
}

lib/utils.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Address, BigDecimal, BigInt, Bytes } from '@graphprotocol/graph-ts'
2+
3+
export namespace bytes {
4+
const ADDRESS_LENGTH = 20
5+
6+
export function toAddress(address: Bytes): Address {
7+
return Address.fromHexString(address.toHex()).subarray(-ADDRESS_LENGTH) as Address
8+
}
9+
10+
export function toSignedInt(value: Bytes, signed: boolean = false, bigEndian: boolean = true): BigInt {
11+
return BigInt.fromSignedBytes(bigEndian ? (value.reverse() as Bytes) : value)
12+
}
13+
14+
export function toUnsignedInt(value: Bytes, bigEndian: boolean = true): BigInt {
15+
return BigInt.fromUnsignedBytes(bigEndian ? (value.reverse() as Bytes) : value)
16+
}
17+
}
18+
19+
export namespace decimal {
20+
export const DEFAULT_DECIMALS = 18
21+
22+
export let ZERO = BigDecimal.fromString('0')
23+
export let ONE = BigDecimal.fromString('1')
24+
25+
export function fromNumber(value: f64): BigDecimal {
26+
return fromString(value.toString())
27+
}
28+
29+
export function fromString(value: string): BigDecimal {
30+
return BigDecimal.fromString(value)
31+
}
32+
33+
export function convert(value: BigInt, decimals: number = DEFAULT_DECIMALS): BigDecimal {
34+
let precision = BigInt.fromI32(10)
35+
.pow(<u8>decimals)
36+
.toBigDecimal()
37+
38+
return value.divDecimal(precision)
39+
}
40+
}
41+
42+
export namespace integer {
43+
export let ZERO = BigInt.fromI32(0)
44+
export let ONE = BigInt.fromI32(1)
45+
46+
export function fromNumber(value: i32): BigInt {
47+
return BigInt.fromI32(value)
48+
}
49+
50+
export function fromString(value: string): BigInt {
51+
return fromNumber(parseI32(value))
52+
}
53+
}

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@protofire/subgraph-toolkit",
3+
"version": "0.0.0",
4+
"description": "AssemblyScript helpers for writing subgraph mappings for The Graph",
5+
"repository": "https://github.com/protofire/subgraph-toolkit.git",
6+
"license": "GPL-3.0-only",
7+
"author": "Sebastián Galiano",
8+
"module": "index.ts",
9+
"types": "index.ts",
10+
"dependencies": {
11+
"@protofire/subgraph-prettier-config": "^0.1.1"
12+
},
13+
"devDependencies": {
14+
"@graphprotocol/graph-ts": ">=0.18.0",
15+
"prettier": "2.0.5"
16+
},
17+
"peerDependencies": {
18+
"@graphprotocol/graph-ts": ">=0.18.0"
19+
}
20+
}

prettier.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@protofire/subgraph-prettier-config')

schema.graphql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Metrics Module
2+
interface Metric @entity {
3+
id: ID!
4+
}
5+
6+
type Accumulator implements Metric @entity {
7+
id: ID!
8+
total: BigDecimal!
9+
}
10+
11+
type Counter implements Metric @entity {
12+
id: ID!
13+
count: BigInt!
14+
}

tsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "./node_modules/assemblyscript/src/tsconfig.json",
3+
"include": ["index.ts"],
4+
"exclude": ["node_modules"]
5+
}

yarn.lock

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@graphprotocol/graph-ts@>=0.18.0":
6+
version "0.18.1"
7+
resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.18.1.tgz#6aadeb3e6c01f4373978bd7dd4409305ba64ffce"
8+
integrity sha512-vwDZjsnHlRKg8vdR+bfPdHwDixf0ozY6FU9CyVjrGLQpsEEISoNwsSCriUXE4Wv57LhmZEl8Ce3dhiPoqsv9tg==
9+
dependencies:
10+
assemblyscript "https://github.com/AssemblyScript/assemblyscript#36040d5b5312f19a025782b5e36663823494c2f3"
11+
12+
"@protobufjs/utf8@^1.1.0":
13+
version "1.1.0"
14+
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
15+
integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=
16+
17+
"@protofire/subgraph-prettier-config@^0.1.1":
18+
version "0.1.1"
19+
resolved "https://registry.yarnpkg.com/@protofire/subgraph-prettier-config/-/subgraph-prettier-config-0.1.1.tgz#da616048f8d77f27446ac0298d8fdd9bbc4eb16a"
20+
integrity sha512-XJNXRYotEcywkCMsUMsbpzbC5xZUwQGwdk2NS+NNMhwNhm24i3SSO2dqJgnWYscmBGwpVZJTGVYK6f8g/Gn16g==
21+
22+
"assemblyscript@https://github.com/AssemblyScript/assemblyscript#36040d5b5312f19a025782b5e36663823494c2f3":
23+
version "0.6.0"
24+
resolved "https://github.com/AssemblyScript/assemblyscript#36040d5b5312f19a025782b5e36663823494c2f3"
25+
dependencies:
26+
"@protobufjs/utf8" "^1.1.0"
27+
binaryen "77.0.0-nightly.20190407"
28+
glob "^7.1.3"
29+
long "^4.0.0"
30+
opencollective-postinstall "^2.0.0"
31+
source-map-support "^0.5.11"
32+
33+
balanced-match@^1.0.0:
34+
version "1.0.0"
35+
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
36+
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=
37+
38+
binaryen@77.0.0-nightly.20190407:
39+
version "77.0.0-nightly.20190407"
40+
resolved "https://registry.yarnpkg.com/binaryen/-/binaryen-77.0.0-nightly.20190407.tgz#fbe4f8ba0d6bd0809a84eb519d2d5b5ddff3a7d1"
41+
integrity sha512-1mxYNvQ0xywMe582K7V6Vo2zzhZZxMTeGHH8aE/+/AND8f64D8Q1GThVY3RVRwGY/4p+p95ccw9Xbw2ovFXRIg==
42+
43+
brace-expansion@^1.1.7:
44+
version "1.1.11"
45+
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
46+
integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==
47+
dependencies:
48+
balanced-match "^1.0.0"
49+
concat-map "0.0.1"
50+
51+
buffer-from@^1.0.0:
52+
version "1.1.1"
53+
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
54+
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
55+
56+
concat-map@0.0.1:
57+
version "0.0.1"
58+
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
59+
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
60+
61+
fs.realpath@^1.0.0:
62+
version "1.0.0"
63+
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
64+
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=
65+
66+
glob@^7.1.3:
67+
version "7.1.6"
68+
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
69+
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
70+
dependencies:
71+
fs.realpath "^1.0.0"
72+
inflight "^1.0.4"
73+
inherits "2"
74+
minimatch "^3.0.4"
75+
once "^1.3.0"
76+
path-is-absolute "^1.0.0"
77+
78+
inflight@^1.0.4:
79+
version "1.0.6"
80+
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
81+
integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
82+
dependencies:
83+
once "^1.3.0"
84+
wrappy "1"
85+
86+
inherits@2:
87+
version "2.0.4"
88+
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
89+
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
90+
91+
long@^4.0.0:
92+
version "4.0.0"
93+
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
94+
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
95+
96+
minimatch@^3.0.4:
97+
version "3.0.4"
98+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
99+
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
100+
dependencies:
101+
brace-expansion "^1.1.7"
102+
103+
once@^1.3.0:
104+
version "1.4.0"
105+
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
106+
integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
107+
dependencies:
108+
wrappy "1"
109+
110+
opencollective-postinstall@^2.0.0:
111+
version "2.0.3"
112+
resolved "https://registry.yarnpkg.com/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz#7a0fff978f6dbfa4d006238fbac98ed4198c3259"
113+
integrity sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==
114+
115+
path-is-absolute@^1.0.0:
116+
version "1.0.1"
117+
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
118+
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
119+
120+
prettier@2.0.5:
121+
version "2.0.5"
122+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.5.tgz#d6d56282455243f2f92cc1716692c08aa31522d4"
123+
integrity sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==
124+
125+
source-map-support@^0.5.11:
126+
version "0.5.19"
127+
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
128+
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
129+
dependencies:
130+
buffer-from "^1.0.0"
131+
source-map "^0.6.0"
132+
133+
source-map@^0.6.0:
134+
version "0.6.1"
135+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
136+
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
137+
138+
wrappy@1:
139+
version "1.0.2"
140+
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
141+
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=

0 commit comments

Comments
 (0)