Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solana: add HashMap and HashSet types support #367

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/changes/@subsquid/borsh/borsh-hashmap_2025-01-15-14-29.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@subsquid/borsh",
"comment": "implement HashMap and HashSet codecs",
"type": "minor"
}
],
"packageName": "@subsquid/borsh"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@subsquid/solana-typegen",
"comment": "add HashMap and HashSet types support",
"type": "minor"
}
],
"packageName": "@subsquid/solana-typegen"
}
27 changes: 27 additions & 0 deletions solana/borsh/src/codecs/hash-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Codec} from '../codec'
import {Sink} from '../sink'
import {Src} from '../src'


export class HashMapCodec<K, V> implements Codec<Map<K, V>> {
constructor(public readonly key: Codec<K>, public readonly value: Codec<V>) {}

encode(sink: Sink, val: Map<K, V>): void {
sink.u32(val.size)
for (let [key, value] of val) {
this.key.encode(sink, key)
this.value.encode(sink, value)
}
}

decode(src: Src): Map<K, V> {
let len = src.u32()
let res = new Map<K, V>()
for (let i = 0; i < len; i++) {
let key = this.key.decode(src)
let value = this.value.decode(src)
res.set(key, value)
}
return res
}
}
25 changes: 25 additions & 0 deletions solana/borsh/src/codecs/hash-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Codec} from '../codec'
import {Sink} from '../sink'
import {Src} from '../src'


export class HashSetCodec<T> implements Codec<Set<T>> {
constructor(public readonly item: Codec<T>) {}

encode(sink: Sink, val: Set<T>): void {
sink.u32(val.size)
for (let value of val) {
this.item.encode(sink, value)
}
}

decode(src: Src): Set<T> {
let len = src.u32()
let res = new Set<T>()
for (let i = 0; i < len; i++) {
let value = this.item.decode(src)
res.add(value)
}
return res
}
}
14 changes: 14 additions & 0 deletions solana/borsh/src/dsl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {Codec, GetCodecType} from './codec'
import {ArrayCodec, FixedArrayCodec} from './codecs/array'
import {SumCodec, Variant} from './codecs/enum'
import {HashMapCodec} from './codecs/hash-map'
import {HashSetCodec} from './codecs/hash-set'
import {OptionCodec} from './codecs/option'
import {RefCodec} from './codecs/ref'
import {GetStructType, StructCodec} from './codecs/struct'
Expand Down Expand Up @@ -29,6 +31,7 @@ export function struct<Props extends Record<string, Codec<any>>>(
return new StructCodec(props as any)
}


export function tuple(tuple: []): TupleCodec<[]>
export function tuple<T>(tuple: [T]): TupleCodec<GetTupleType<[T]>>
export function tuple<T1, T2>(tuple: [T1, T2]): TupleCodec<GetTupleType<[T1, T2]>>
Expand All @@ -43,6 +46,17 @@ export function tuple<T extends any[]>(tuple: T): TupleCodec<T> {
return new TupleCodec(tuple as any)
}


export function hashMap<KC extends Codec<any>, VC extends Codec<any>>(key: KC, value: VC): HashMapCodec<GetCodecType<KC>, GetCodecType<VC>> {
return new HashMapCodec(key, value)
}


export function hashSet<IC extends Codec<any>>(item: IC): HashSetCodec<GetCodecType<IC>> {
return new HashSetCodec(item)
}


export function option<IC extends Codec<any>>(item: IC): OptionCodec<GetCodecType<IC>> {
return new OptionCodec(item)
}
Expand Down
21 changes: 21 additions & 0 deletions solana/solana-typegen/src/program/anchor/old.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export type IdlType =
| IdlTypeCOption
| IdlTypeVec
| IdlTypeArray
| IdlTypeHashMap
| IdlTypeHashSet

export type IdlTypeDefined = {
defined: string
Expand All @@ -171,6 +173,14 @@ export type IdlTypeArray = {
array: [idlType: IdlType, size: number]
}

export type IdlTypeHashMap = {
hashMap: [idlType: IdlType, idlType: IdlType]
}

export type IdlTypeHashSet = {
hashSet: IdlType
}

export type IdlEnumVariant = {
name: string
fields?: IdlEnumFields
Expand Down Expand Up @@ -355,6 +365,17 @@ function fromType(type: IdlType): Type {
kind: TypeKind.Array,
type: fromType(type.vec),
}
} else if ('hashMap' in type) {
return {
kind: TypeKind.HashMap,
key: fromType(type.hashMap[0]),
value: fromType(type.hashMap[1]),
}
} else if ('hashSet' in type) {
return {
kind: TypeKind.HashSet,
type: fromType(type.hashSet),
}
} else if ('defined' in type) {
return {
kind: TypeKind.Defined,
Expand Down
21 changes: 21 additions & 0 deletions solana/solana-typegen/src/program/anchor/v0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ export type IdlType =
| IdlTypeCOption
| IdlTypeVec
| IdlTypeArray
| IdlTypeHashMap
| IdlTypeHashSet
| IdlTypeDefined
| IdlTypeGeneric

Expand All @@ -258,6 +260,14 @@ export type IdlTypeArray = {
array: [idlType: IdlType, size: IdlArrayLen]
}

export type IdlTypeHashMap = {
hashMap: [idlType: IdlType, idlType: IdlType]
}

export type IdlTypeHashSet = {
hashSet: IdlType
}

export type IdlTypeDefined = {
defined: {
name: string
Expand Down Expand Up @@ -395,6 +405,17 @@ function fromType(type: IdlType): Type {
kind: TypeKind.Array,
type: fromType(type.vec),
}
} else if ('hashMap' in type) {
return {
kind: TypeKind.HashMap,
key: fromType(type.hashMap[0]),
value: fromType(type.hashMap[1]),
}
} else if ('hashSet' in type) {
return {
kind: TypeKind.HashSet,
type: fromType(type.hashSet),
}
} else if ('defined' in type) {
return {
kind: TypeKind.Defined,
Expand Down
15 changes: 15 additions & 0 deletions solana/solana-typegen/src/program/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export enum TypeKind {
Struct,
Defined,
Generic,
HashMap,
HashSet
}

export interface PrimitiveType {
Expand All @@ -52,6 +54,17 @@ export interface TupleType {
tuple: Type[]
}

export interface HashMapType {
kind: TypeKind.HashMap
key: Type
value: Type
}

export interface HashSetType {
kind: TypeKind.HashSet
type: Type
}

export interface StructType {
kind: TypeKind.Struct
fields: Field[]
Expand Down Expand Up @@ -114,6 +127,8 @@ export type Type =
| ArrayType
| FixedArrayType
| TupleType
| HashMapType
| HashSetType
| StructType
| EnumType
| OptionType
Expand Down
24 changes: 24 additions & 0 deletions solana/solana-typegen/src/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ export class TypeModuleOutput extends FileOutput {
})
this.line(`])` + end)
break
case TypeKind.HashMap:
this.borsh.add('hashMap')
this.line(start + `hashMap(`)
this.indentation(() => {
this.printDsl(``, {type: type.key}, ',')
this.printDsl(``, {type: type.value}, '')
})
this.line(`)` + end)
break
case TypeKind.HashSet:
this.borsh.add('hashSet')
this.printDsl(start + `hashSet(`, {type: type.type}, `)` + end)
break
case TypeKind.Defined:
const typeName = toTypeName(type.name)
this.types.add(typeName)
Expand Down Expand Up @@ -287,6 +300,17 @@ export class TypeModuleOutput extends FileOutput {
})
this.line(`]` + end)
break
case TypeKind.HashMap:
this.line(start + `Map<`)
this.indentation(() => {
this.printType(``, {type: type.key}, `,`)
this.printType(``, {type: type.value}, ``)
})
this.line(`>` + end)
break
case TypeKind.HashSet:
this.printType(start + `Set<`, {type: type.type}, `>` + end)
break
case TypeKind.Defined:
this.types.add(toTypeName(type.name))
this.line(start + toTypeName(type.name) + end)
Expand Down
Loading