datai-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/@graphprotocol/graph-ts/README.md +13 -0
  2. package/@graphprotocol/graph-ts/chain/arweave.ts +82 -0
  3. package/@graphprotocol/graph-ts/chain/cosmos.ts +426 -0
  4. package/@graphprotocol/graph-ts/chain/ethereum.ts +727 -0
  5. package/@graphprotocol/graph-ts/chain/near.ts +420 -0
  6. package/@graphprotocol/graph-ts/chain/starknet.ts +39 -0
  7. package/@graphprotocol/graph-ts/common/collections.ts +495 -0
  8. package/@graphprotocol/graph-ts/common/conversion.ts +3 -0
  9. package/@graphprotocol/graph-ts/common/datasource.ts +41 -0
  10. package/@graphprotocol/graph-ts/common/eager_offset.ts +42 -0
  11. package/@graphprotocol/graph-ts/common/json.ts +28 -0
  12. package/@graphprotocol/graph-ts/common/numbers.ts +407 -0
  13. package/@graphprotocol/graph-ts/common/value.ts +585 -0
  14. package/@graphprotocol/graph-ts/global/global.ts +4 -0
  15. package/@graphprotocol/graph-ts/helper-functions.ts +79 -0
  16. package/@graphprotocol/graph-ts/index.ts +156 -0
  17. package/@graphprotocol/graph-ts/package.json +3 -0
  18. package/@graphprotocol/graph-ts/tsconfig.json +4 -0
  19. package/@graphprotocol/graph-ts/types/tsconfig.base.json +3 -0
  20. package/API/index.ts +1 -0
  21. package/API/v1/activePositions/ActivePositionsResult.ts +147 -0
  22. package/API/v1/activePositions/TokenBalance.ts +25 -0
  23. package/API/v1/activePositions/activePositions.ts +37 -0
  24. package/API/v1/bigDecimal/BigDecimalPb.ts +40 -0
  25. package/API/v1/bigDecimal/bigDecimal.ts +75 -0
  26. package/API/v1/bigInt/bigInt.ts +123 -0
  27. package/API/v1/crypto/crypto.ts +16 -0
  28. package/API/v1/ethereum/SmartContractCallPb.ts +84 -0
  29. package/API/v1/ethereum/ValuePb.ts +113 -0
  30. package/API/v1/ethereum/ValuesPb.ts +39 -0
  31. package/API/v1/ethereum/ethereum.ts +76 -0
  32. package/API/v1/index.ts +10 -0
  33. package/API/v1/log/log.ts +18 -0
  34. package/API/v1/proto/activePositions/ActivePositionsResultPb.ts +139 -0
  35. package/API/v1/proto/activePositions/TokenBalancePb.ts +69 -0
  36. package/API/v1/proto/bigDecimal/BigDecimalPb.ts +56 -0
  37. package/API/v1/proto/ethereum/SmartContractCallPb.ts +102 -0
  38. package/API/v1/proto/ethereum/ValueKindPb.ts +17 -0
  39. package/API/v1/proto/ethereum/ValuePb.ts +96 -0
  40. package/API/v1/proto/ethereum/ValuesPb.ts +53 -0
  41. package/API/v1/proto/google/protobuf/Any.ts +56 -0
  42. package/API/v1/proto/google/protobuf/Timestamp.ts +56 -0
  43. package/API/v1/proto/store/EntitiesPb.ts +53 -0
  44. package/API/v1/proto/store/EntityPb.ts +99 -0
  45. package/API/v1/proto/store/EntityWithMetaPb.ts +75 -0
  46. package/API/v1/proto/store/ValueKindPb.ts +17 -0
  47. package/API/v1/proto/store/ValuePb.ts +132 -0
  48. package/API/v1/proto/store/ValuesPb.ts +53 -0
  49. package/API/v1/proto/watcher/WatcherResultPb.ts +62 -0
  50. package/API/v1/store/EntitiesPb.ts +39 -0
  51. package/API/v1/store/EntityPb.ts +75 -0
  52. package/API/v1/store/ValuePb.ts +154 -0
  53. package/API/v1/store/ValuesPb.ts +39 -0
  54. package/API/v1/store/store.ts +78 -0
  55. package/API/v1/typeConversion/typeConversion.ts +57 -0
  56. package/API/v1/watcher/WatcherInput.ts +9 -0
  57. package/API/v1/watcher/watcher.ts +33 -0
  58. package/index.ts +5 -0
  59. package/package.json +11 -0
  60. package/utils/constants.ts +16 -0
  61. package/utils/mathUtils.ts +77 -0
  62. package/utils/pdk.ts +171 -0
  63. package/utils/subgraphUtils.ts +27 -0
@@ -0,0 +1,156 @@
1
+ // Side-effect to evaluate eagerly the offset of stub AS runtime
2
+ import './common/eager_offset';
3
+ import { ByteArray, Bytes, Entity } from './common/collections';
4
+ import { Value } from './common/value';
5
+
6
+ // Arweave support
7
+ export * from './chain/arweave';
8
+ // Ethereum support
9
+ export * from './chain/ethereum';
10
+ // NEAR support
11
+ export * from './chain/near';
12
+ // Cosmos support
13
+ export * from './chain/cosmos';
14
+ // Starknet support
15
+ export * from './chain/starknet';
16
+ // Regular re-exports
17
+ export * from './common/collections';
18
+ export * from './common/conversion';
19
+ export * from './common/datasource';
20
+ export * from './common/json';
21
+ export * from './common/numbers';
22
+ export * from './common/value';
23
+ import * as API from '../../API'
24
+ export { store, crypto } from '../../API';
25
+
26
+
27
+ // /** Host IPFS interface */
28
+ // export declare namespace ipfs {
29
+ // function cat(hash: string): Bytes | null;
30
+ // function map(hash: string, callback: string, userData: Value, flags: string[]): void;
31
+ // }
32
+
33
+ // export namespace ipfs {
34
+ // export function mapJSON(hash: string, callback: string, userData: Value): void {
35
+ // ipfs.map(hash, callback, userData, ['json']);
36
+ // }
37
+ // }
38
+
39
+ // /**
40
+ // * Special function for ENS name lookups, not meant for general purpose use.
41
+ // * This function will only be useful if the graph-node instance has additional
42
+ // * data loaded **
43
+ // */
44
+ // export declare namespace ens {
45
+ // function nameByHash(hash: string): string | null;
46
+ // }
47
+
48
+ function format(fmt: string, args: string[]): string {
49
+ let out = '';
50
+ let argIndex = 0;
51
+ for (let i: i32 = 0, len: i32 = fmt.length; i < len; i++) {
52
+ if (
53
+ i < len - 1 &&
54
+ fmt.charCodeAt(i) == 0x7b /* '{' */ &&
55
+ fmt.charCodeAt(i + 1) == 0x7d /* '}' */
56
+ ) {
57
+ if (argIndex >= args.length) {
58
+ throw new Error('Too few arguments for format string: ' + fmt);
59
+ } else {
60
+ out += args[argIndex++];
61
+ i++;
62
+ }
63
+ } else {
64
+ out += fmt.charAt(i);
65
+ }
66
+ }
67
+ return out;
68
+ }
69
+
70
+ export namespace log {
71
+ export const log = API.log.log;
72
+
73
+ export enum Level {
74
+ CRITICAL = 0,
75
+ ERROR = 1,
76
+ WARNING = 2,
77
+ INFO = 3,
78
+ DEBUG = 4,
79
+ }
80
+
81
+ /**
82
+ * Logs a critical message that terminates the subgraph.
83
+ *
84
+ * @param msg Format string a la "Value = {}, other = {}".
85
+ * @param args Format string arguments.
86
+ */
87
+ export function critical(msg: string, args: Array<string>): void {
88
+ log(Level.CRITICAL, format(msg, args));
89
+ }
90
+
91
+ /**
92
+ * Logs an error message.
93
+ *
94
+ * @param msg Format string a la "Value = {}, other = {}".
95
+ * @param args Format string arguments.
96
+ */
97
+ export function error(msg: string, args: Array<string>): void {
98
+ log(Level.ERROR, format(msg, args));
99
+ }
100
+
101
+ /** Logs a warning message.
102
+ *
103
+ * @param msg Format string a la "Value = {}, other = {}".
104
+ * @param args Format string arguments.
105
+ */
106
+ export function warning(msg: string, args: Array<string>): void {
107
+ log(Level.WARNING, format(msg, args));
108
+ }
109
+
110
+ /** Logs an info message.
111
+ *
112
+ * @param msg Format string a la "Value = {}, other = {}".
113
+ * @param args Format string arguments.
114
+ */
115
+ export function info(msg: string, args: Array<string>): void {
116
+ log(Level.INFO, format(msg, args));
117
+ }
118
+
119
+ /** Logs a debug message.
120
+ *
121
+ * @param msg Format string a la "Value = {}, other = {}".
122
+ * @param args Format string arguments.
123
+ */
124
+ export function debug(msg: string, args: Array<string>): void {
125
+ log(Level.DEBUG, format(msg, args));
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Helper functions for Ethereum.
131
+ */
132
+ export namespace EthereumUtils {
133
+ /**
134
+ * Returns the contract address that would result from the given CREATE2 call.
135
+ * @param from The Ethereum address of the account that is initiating the contract creation.
136
+ * @param salt A 32-byte value that is used to create a deterministic address for the contract. This can be any arbitrary value, but it should be unique to the contract being created.
137
+ * @param initCodeHash he compiled code that will be executed when the contract is created. This should be a hex-encoded string that represents the compiled bytecode.
138
+ * @returns Address of the contract that would be created.
139
+ */
140
+ export function getCreate2Address(from: Bytes, salt: Bytes, initCodeHash: Bytes): Bytes {
141
+ return Bytes.fromHexString(
142
+ Bytes.fromByteArray(
143
+ API.crypto.keccak256(
144
+ Bytes.fromHexString(
145
+ '0xff' +
146
+ from.toHexString().slice(2) +
147
+ salt.toHexString().slice(2) +
148
+ initCodeHash.toHexString().slice(2),
149
+ ),
150
+ ),
151
+ )
152
+ .toHexString()
153
+ .slice(26),
154
+ );
155
+ }
156
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ascMain": "index.ts"
3
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../../../../node_modules/assemblyscript/std/assembly.json",
3
+ "include": ["index.ts", "helper-functions.ts", "global/global.ts", "test"]
4
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "assemblyscript/std/assembly"
3
+ }
package/API/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './v1'
@@ -0,0 +1,147 @@
1
+ import { Address, BigInt } from '../../../@graphprotocol/graph-ts'
2
+ import { Timestamp } from '../proto/google/protobuf/Timestamp'
3
+ import { ActivePositionsResultPb } from '../proto/activePositions/ActivePositionsResultPb'
4
+ import { TokenBalancePb } from '../proto/activePositions/TokenBalancePb'
5
+ import { TokenBalance } from './TokenBalance'
6
+
7
+ export class ActivePositionsResult extends ActivePositionsResultPb {
8
+ tokenBalance(tokenBalances: Array<TokenBalancePb>, index: i32): TokenBalance {
9
+ return changetype<TokenBalance>(tokenBalances[index])
10
+ }
11
+
12
+ findTokenBalance(
13
+ tokenBalances: Array<TokenBalancePb>,
14
+ tokenAddress: Address,
15
+ tokenId: BigInt | null = null
16
+ ): TokenBalance | null {
17
+ for (let i = 0; i < tokenBalances.length; i++) {
18
+ const tokenBalance = this.tokenBalance(tokenBalances, i)
19
+ if (
20
+ tokenBalance.tokenAddress === tokenAddress &&
21
+ (!tokenId || tokenBalance.tokenId == tokenId)
22
+ ) {
23
+ return tokenBalance
24
+ }
25
+ }
26
+ return null
27
+ }
28
+
29
+ findBalance(
30
+ tokenBalances: Array<TokenBalancePb>,
31
+ tokenAddress: Address,
32
+ tokenId: BigInt | null = null
33
+ ): BigInt | null {
34
+ const tokenBalance = this.findTokenBalance(
35
+ tokenBalances,
36
+ tokenAddress,
37
+ tokenId
38
+ )
39
+ if (tokenBalance == null) {
40
+ return null
41
+ }
42
+ return tokenBalance.balance
43
+ }
44
+
45
+ setBalance(
46
+ tokenBalances: Array<TokenBalancePb>,
47
+ balance: BigInt,
48
+ tokenAddress: Address,
49
+ tokenId: BigInt | null = null
50
+ ): void {
51
+ let tokenBalance = this.findTokenBalance(
52
+ tokenBalances,
53
+ tokenAddress,
54
+ tokenId
55
+ )
56
+ if (tokenBalance === null) {
57
+ tokenBalance = new TokenBalance()
58
+ tokenBalances.push(tokenBalance)
59
+ }
60
+ tokenBalance.tokenAddress = tokenAddress
61
+ tokenBalance.balance = balance
62
+ if (tokenId) tokenBalance.tokenId = tokenId
63
+ }
64
+
65
+ supplyBalance(
66
+ tokenAddress: Address,
67
+ tokenId: BigInt | null = null
68
+ ): BigInt | null {
69
+ return this.findBalance(this.supplyTokens, tokenAddress, tokenId)
70
+ }
71
+
72
+ setSupplyBalance(
73
+ tokenAddress: Address,
74
+ balance: BigInt,
75
+ tokenId: BigInt | null = null
76
+ ): void {
77
+ this.setBalance(this.supplyTokens, balance, tokenAddress, tokenId)
78
+ }
79
+
80
+ borrowBalance(
81
+ tokenAddress: Address,
82
+ tokenId: BigInt | null = null
83
+ ): BigInt | null {
84
+ return this.findBalance(this.borrowTokens, tokenAddress, tokenId)
85
+ }
86
+
87
+ setBorrowBalance(
88
+ tokenAddress: Address,
89
+ balance: BigInt,
90
+ tokenId: BigInt | null = null
91
+ ): void {
92
+ this.setBalance(this.borrowTokens, balance, tokenAddress, tokenId)
93
+ }
94
+
95
+ rewardBalance(
96
+ tokenAddress: Address,
97
+ tokenId: BigInt | null = null
98
+ ): BigInt | null {
99
+ return this.findBalance(this.rewardTokens, tokenAddress, tokenId)
100
+ }
101
+
102
+ setRewardBalance(
103
+ tokenAddress: Address,
104
+ balance: BigInt,
105
+ tokenId: BigInt | null = null
106
+ ): void {
107
+ this.setBalance(this.rewardTokens, balance, tokenAddress, tokenId)
108
+ }
109
+
110
+ set poolAddress(poolAddress: Address) {
111
+ this.poolAddressBytes = poolAddress
112
+ }
113
+
114
+ get poolAddress(): Address {
115
+ return changetype<Address>(this.poolAddressBytes)
116
+ }
117
+
118
+ set unlockTime(seconds: i64) {
119
+ if (seconds == 0) {
120
+ this.unlockTimestamp = null
121
+ return
122
+ }
123
+ if (!this.unlockTimestamp) {
124
+ this.unlockTimestamp = new Timestamp()
125
+ }
126
+ this.unlockTimestamp!.seconds = seconds
127
+ }
128
+
129
+ get unlockTime(): i64 {
130
+ if (!this.unlockTimestamp) {
131
+ return 0
132
+ }
133
+ return this.unlockTimestamp.seconds
134
+ }
135
+
136
+ setPositionTokenBalance(
137
+ tokenAddress: Address,
138
+ balance: BigInt,
139
+ tokenId: BigInt | null = null
140
+ ): void {
141
+ const positionToken = new TokenBalance()
142
+ this.positionToken = positionToken
143
+ positionToken.tokenAddressBytes = tokenAddress
144
+ positionToken.balanceBytes = balance
145
+ if (tokenId) positionToken.tokenId = tokenId
146
+ }
147
+ }
@@ -0,0 +1,25 @@
1
+ import { Address, BigInt } from '../../../@graphprotocol/graph-ts'
2
+ import { TokenBalancePb } from '../proto/activePositions/TokenBalancePb'
3
+
4
+ export class TokenBalance extends TokenBalancePb {
5
+ get tokenAddress(): Address {
6
+ return changetype<Address>(this.tokenAddressBytes)
7
+ }
8
+ set tokenAddress(value: Address) {
9
+ this.tokenAddressBytes = value
10
+ }
11
+
12
+ get balance(): BigInt {
13
+ return changetype<BigInt>(this.balanceBytes)
14
+ }
15
+ set balance(value: BigInt) {
16
+ this.balanceBytes = value
17
+ }
18
+
19
+ get tokenId(): BigInt {
20
+ return changetype<BigInt>(this.tokenIdBytes)
21
+ }
22
+ set tokenId(value: BigInt) {
23
+ this.tokenIdBytes = value
24
+ }
25
+ }
@@ -0,0 +1,37 @@
1
+ import { Protobuf } from 'as-proto/assembly'
2
+ import { watcher } from '../watcher/watcher'
3
+ import { ActivePositionsResult } from './ActivePositionsResult'
4
+ import { ActivePositionsResultPb } from '../proto/activePositions/ActivePositionsResultPb'
5
+
6
+ export namespace activePositions {
7
+ export function inputPosition<T>(): T {
8
+ return changetype<T>(watcher.input().position)
9
+ }
10
+
11
+ const resultTypeUrl = 'API/v1/activePositions/ActivePositionsResultPb'
12
+
13
+ export function output(
14
+ result: ActivePositionsResult,
15
+ forceInactive: bool = false
16
+ ): void {
17
+ let isActive = result.supplyTokens.some((token) =>
18
+ token.balanceBytes.some((byte) => byte != 0)
19
+ )
20
+ isActive =
21
+ isActive ||
22
+ result.borrowTokens.some((token) =>
23
+ token.balanceBytes.some((byte) => byte != 0)
24
+ )
25
+ isActive = isActive && !forceInactive
26
+
27
+ const resultMsg = Protobuf.encode(
28
+ result as ActivePositionsResultPb,
29
+ ActivePositionsResultPb.encode
30
+ )
31
+ watcher.output(
32
+ resultTypeUrl,
33
+ resultMsg,
34
+ isActive ? watcher.SCHEDULE_DEFAULT : ''
35
+ )
36
+ }
37
+ }
@@ -0,0 +1,40 @@
1
+ // Code is NOT generated
2
+ // but the generated code from proto folder was used as reference
3
+ // it is done this way to directly encode/decode into graph-ts types
4
+
5
+ import { Writer, Reader } from 'as-proto/assembly'
6
+ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts/'
7
+
8
+ export namespace BigDecimalPb {
9
+ export function encode(message: BigDecimal, writer: Writer): void {
10
+ writer.uint32(10)
11
+ writer.bytes(message.digits)
12
+
13
+ writer.uint32(16)
14
+ writer.int32(message.exp.toI32())
15
+ }
16
+
17
+ export function decode(reader: Reader, length: i32): BigDecimal {
18
+ const end: usize = length < 0 ? reader.end : reader.ptr + length
19
+ const message = new BigDecimal(BigInt.zero())
20
+
21
+ while (reader.ptr < end) {
22
+ const tag = reader.uint32()
23
+ switch (tag >>> 3) {
24
+ case 1:
25
+ message.digits = changetype<BigInt>(reader.bytes())
26
+ break
27
+
28
+ case 2:
29
+ message.exp = BigInt.fromI32(reader.int32())
30
+ break
31
+
32
+ default:
33
+ reader.skipType(tag & 7)
34
+ break
35
+ }
36
+ }
37
+
38
+ return message
39
+ }
40
+ }
@@ -0,0 +1,75 @@
1
+ import { BigDecimal } from '@graphprotocol/graph-ts/'
2
+ import {
3
+ findString,
4
+ storeString,
5
+ storeProtobuf,
6
+ findProtobuf
7
+ } from '../../../utils/pdk'
8
+ import { BigDecimalPb } from '../bigDecimal/BigDecimalPb'
9
+
10
+ /** Host interface for BigDecimal */
11
+ export declare namespace bigDecimal {
12
+ function hostPlus(x: u64, y: u64): u64
13
+ function hostMinus(x: u64, y: u64): u64
14
+ function hostTimes(x: u64, y: u64): u64
15
+ function hostDividedBy(x: u64, y: u64): u64
16
+ function hostCompare(x: u64, y: u64): u32
17
+ function hostToString(bigDecimal: u64): u64
18
+ function hostFromString(s: u64): u64
19
+ }
20
+
21
+ export namespace bigDecimal {
22
+ export function plus(x: BigDecimal, y: BigDecimal): BigDecimal {
23
+ const xMem = storeProtobuf(x, BigDecimalPb.encode)
24
+ const yMem = storeProtobuf(y, BigDecimalPb.encode)
25
+ const resultOffset = bigDecimal.hostPlus(xMem.offset, yMem.offset)
26
+ xMem.free()
27
+ yMem.free()
28
+ return findProtobuf<BigDecimal>(resultOffset, BigDecimalPb.decode)
29
+ }
30
+ export function minus(x: BigDecimal, y: BigDecimal): BigDecimal {
31
+ const xMem = storeProtobuf(x, BigDecimalPb.encode)
32
+ const yMem = storeProtobuf(y, BigDecimalPb.encode)
33
+ const resultOffset = bigDecimal.hostMinus(xMem.offset, yMem.offset)
34
+ return findProtobuf<BigDecimal>(resultOffset, BigDecimalPb.decode)
35
+ }
36
+ export function times(x: BigDecimal, y: BigDecimal): BigDecimal {
37
+ const xMem = storeProtobuf(x, BigDecimalPb.encode)
38
+ const yMem = storeProtobuf(y, BigDecimalPb.encode)
39
+ const resultOffset = bigDecimal.hostTimes(xMem.offset, yMem.offset)
40
+ xMem.free()
41
+ yMem.free()
42
+ return findProtobuf<BigDecimal>(resultOffset, BigDecimalPb.decode)
43
+ }
44
+ export function dividedBy(x: BigDecimal, y: BigDecimal): BigDecimal {
45
+ const xMem = storeProtobuf(x, BigDecimalPb.encode)
46
+ const yMem = storeProtobuf(y, BigDecimalPb.encode)
47
+ const resultOffset = bigDecimal.hostDividedBy(xMem.offset, yMem.offset)
48
+ xMem.free()
49
+ yMem.free()
50
+ return findProtobuf<BigDecimal>(resultOffset, BigDecimalPb.decode)
51
+ }
52
+ export function compare(x: BigDecimal, y: BigDecimal): i32 {
53
+ const xMem = storeProtobuf(x, BigDecimalPb.encode)
54
+ const yMem = storeProtobuf(y, BigDecimalPb.encode)
55
+ const result = bigDecimal.hostCompare(xMem.offset, yMem.offset)
56
+ xMem.free()
57
+ yMem.free()
58
+ return result
59
+ }
60
+ export function equals(x: BigDecimal, y: BigDecimal): boolean {
61
+ return compare(x, y) == 0
62
+ }
63
+ export function toString(n: BigDecimal): string {
64
+ const nMem = storeProtobuf(n, BigDecimalPb.encode)
65
+ const resultOffset = bigDecimal.hostToString(nMem.offset)
66
+ nMem.free()
67
+ return findString(resultOffset)
68
+ }
69
+ export function fromString(s: string): BigDecimal {
70
+ const sMem = storeString(s)
71
+ const resultOffset = bigDecimal.hostFromString(sMem.offset)
72
+ sMem.free()
73
+ return findProtobuf<BigDecimal>(resultOffset, BigDecimalPb.decode)
74
+ }
75
+ }
@@ -0,0 +1,123 @@
1
+ import { BigInt, BigDecimal } from '@graphprotocol/graph-ts/'
2
+ import {
3
+ findUint8Array,
4
+ storeString,
5
+ storeUint8Array,
6
+ storeProtobuf,
7
+ findProtobuf
8
+ } from '../../../utils/pdk'
9
+ import { BigDecimalPb } from '../bigDecimal/BigDecimalPb'
10
+
11
+ /** Host interface for BigInt arithmetic */
12
+ export declare namespace bigInt {
13
+ function hostPlus(x: u64, y: u64): u64
14
+ function hostMinus(x: u64, y: u64): u64
15
+ function hostTimes(x: u64, y: u64): u64
16
+ function hostDividedBy(x: u64, y: u64): u64
17
+ function hostDividedByDecimal(x: u64, y: u64): u64
18
+ function hostMod(x: u64, y: u64): u64
19
+ function hostPow(x: u64, exp: u32): u64
20
+ function hostFromString(s: u64): u64
21
+ function hostBitOr(x: u64, y: u64): u64
22
+ function hostBitAnd(x: u64, y: u64): u64
23
+ function hostLeftShift(x: u64, bits: u32): u64
24
+ function hostRightShift(x: u64, bits: u32): u64
25
+ function hostSqrt(x: u64): u64
26
+ }
27
+
28
+ export namespace bigInt {
29
+ export function plus(x: BigInt, y: BigInt): BigInt {
30
+ const xMem = storeUint8Array(x)
31
+ const yMem = storeUint8Array(y)
32
+ const resultOffset = bigInt.hostPlus(xMem.offset, yMem.offset)
33
+ xMem.free()
34
+ yMem.free()
35
+ return changetype<BigInt>(findUint8Array(resultOffset))
36
+ }
37
+ export function minus(x: BigInt, y: BigInt): BigInt {
38
+ const xMem = storeUint8Array(x)
39
+ const yMem = storeUint8Array(y)
40
+ const resultOffset = bigInt.hostMinus(xMem.offset, yMem.offset)
41
+ xMem.free()
42
+ yMem.free()
43
+ return changetype<BigInt>(findUint8Array(resultOffset))
44
+ }
45
+ export function times(x: BigInt, y: BigInt): BigInt {
46
+ const xMem = storeUint8Array(x)
47
+ const yMem = storeUint8Array(y)
48
+ const resultOffset = bigInt.hostTimes(xMem.offset, yMem.offset)
49
+ xMem.free()
50
+ yMem.free()
51
+ return changetype<BigInt>(findUint8Array(resultOffset))
52
+ }
53
+ export function dividedBy(x: BigInt, y: BigInt): BigInt {
54
+ const xMem = storeUint8Array(x)
55
+ const yMem = storeUint8Array(y)
56
+ const resultOffset = bigInt.hostDividedBy(xMem.offset, yMem.offset)
57
+ xMem.free()
58
+ yMem.free()
59
+ return changetype<BigInt>(findUint8Array(resultOffset))
60
+ }
61
+ export function dividedByDecimal(x: BigInt, y: BigDecimal): BigDecimal {
62
+ const xMem = storeUint8Array(x)
63
+ const yMem = storeProtobuf(y, BigDecimalPb.encode)
64
+ const resultOffset = bigInt.hostMod(xMem.offset, yMem.offset)
65
+ xMem.free()
66
+ yMem.free()
67
+ return findProtobuf(resultOffset, BigDecimalPb.decode)
68
+ }
69
+ export function mod(x: BigInt, y: BigInt): BigInt {
70
+ const xMem = storeUint8Array(x)
71
+ const yMem = storeUint8Array(y)
72
+ const resultOffset = bigInt.hostMod(xMem.offset, yMem.offset)
73
+ xMem.free()
74
+ yMem.free()
75
+ return changetype<BigInt>(findUint8Array(resultOffset))
76
+ }
77
+ export function pow(x: BigInt, exp: u8): BigInt {
78
+ const xMem = storeUint8Array(x)
79
+ const resultOffset = bigInt.hostPow(xMem.offset, exp)
80
+ xMem.free()
81
+ return changetype<BigInt>(findUint8Array(resultOffset))
82
+ }
83
+ export function fromString(s: string): BigInt {
84
+ const sMem = storeString(s)
85
+ const resultOffset = bigInt.hostFromString(sMem.offset)
86
+ sMem.free()
87
+ return changetype<BigInt>(findUint8Array(resultOffset))
88
+ }
89
+ export function bitOr(x: BigInt, y: BigInt): BigInt {
90
+ const xMem = storeUint8Array(x)
91
+ const yMem = storeUint8Array(y)
92
+ const resultOffset = bigInt.hostBitOr(xMem.offset, yMem.offset)
93
+ xMem.free()
94
+ yMem.free()
95
+ return changetype<BigInt>(findUint8Array(resultOffset))
96
+ }
97
+ export function bitAnd(x: BigInt, y: BigInt): BigInt {
98
+ const xMem = storeUint8Array(x)
99
+ const yMem = storeUint8Array(y)
100
+ const resultOffset = bigInt.hostBitAnd(xMem.offset, yMem.offset)
101
+ xMem.free()
102
+ yMem.free()
103
+ return changetype<BigInt>(findUint8Array(resultOffset))
104
+ }
105
+ export function leftShift(x: BigInt, bits: u8): BigInt {
106
+ const xMem = storeUint8Array(x)
107
+ const resultOffset = bigInt.hostLeftShift(xMem.offset, bits)
108
+ xMem.free()
109
+ return changetype<BigInt>(findUint8Array(resultOffset))
110
+ }
111
+ export function rightShift(x: BigInt, bits: u8): BigInt {
112
+ const xMem = storeUint8Array(x)
113
+ const resultOffset = bigInt.hostRightShift(xMem.offset, bits)
114
+ xMem.free()
115
+ return changetype<BigInt>(findUint8Array(resultOffset))
116
+ }
117
+ export function sqrt(x: BigInt): BigInt {
118
+ const xMem = storeUint8Array(x)
119
+ const resultOffset = bigInt.hostSqrt(xMem.offset)
120
+ xMem.free()
121
+ return changetype<BigInt>(findUint8Array(resultOffset))
122
+ }
123
+ }
@@ -0,0 +1,16 @@
1
+ import { ByteArray } from '@graphprotocol/graph-ts/'
2
+ import { findUint8Array, storeUint8Array } from '../../../utils/pdk'
3
+
4
+ /** Host crypto utilities interface */
5
+ export declare namespace crypto {
6
+ function hostKeccak256(input: u64): u64
7
+ }
8
+
9
+ export namespace crypto {
10
+ export function keccak256(input: ByteArray): ByteArray {
11
+ const inputMem = storeUint8Array(input)
12
+ const resultOffset = crypto.hostKeccak256(inputMem.offset)
13
+ inputMem.free()
14
+ return changetype<ByteArray>(findUint8Array(resultOffset))
15
+ }
16
+ }