9
9
"github.com/gagliardetto/solana-go"
10
10
"github.com/gagliardetto/solana-go/rpc"
11
11
12
+ types "github.com/smartcontractkit/chainlink-common/pkg/types/solana"
12
13
"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
13
14
"github.com/smartcontractkit/chainlink-solana/pkg/solana/codec"
14
15
)
19
20
ErrGettingSeedAtLocation = fmt .Errorf ("error getting address seed for location" )
20
21
)
21
22
23
+ var _ = Lookup (types.Lookup {})
24
+
25
+ // Lookup is not an alias like the others, because it has a widely used method with solana types that cannot be imported
26
+ // in to chainlink-common. However, it can be directly converted from [types.Lookup].
22
27
type Lookup struct {
23
28
Optional bool
24
29
AccountConstant * AccountConstant `json:"accountConstant,omitempty"`
@@ -27,83 +32,35 @@ type Lookup struct {
27
32
AccountsFromLookupTable * AccountsFromLookupTable `json:"accountsFromLookupTable,omitempty"`
28
33
}
29
34
30
- // AccountConstant represents a fixed address, provided in Base58 format, converted into a `solana.PublicKey`.
31
- type AccountConstant struct {
32
- Name string `json:"name,omitempty"`
33
- Address string `json:"address"`
34
- IsSigner bool `json:"isSigner,omitempty"`
35
- IsWritable bool `json:"isWritable,omitempty"`
36
- }
35
+ // Deprecated
36
+ type AccountConstant = types.AccountConstant
37
37
38
- // AccountLookup dynamically derives an account address from args using a specified location path.
39
- type AccountLookup struct {
40
- Name string `json:"name,omitempty"`
41
- Location string `json:"location"`
42
- // IsSigner and IsWritable can either be a constant bool or a location to a bitmap which decides the bools
43
- IsSigner MetaBool `json:"isSigner,omitempty"`
44
- IsWritable MetaBool `json:"isWritable,omitempty"`
45
- }
38
+ // Deprecated
39
+ type AccountLookup = types.AccountLookup
46
40
47
- type MetaBool struct {
48
- Value bool `json:"value,omitempty"` // bool value
49
- BitmapLocation string `json:"bitmapLocation,omitempty"` // dot separated location of the bitmap
50
- }
41
+ // Deprecated
42
+ type MetaBool = types.MetaBool
51
43
52
- type Seed struct {
53
- Static []byte `json:"static,omitempty"` // Static seed value
54
- Dynamic Lookup `json:"dynamic,omitempty"` // Dynamic lookup for seed
55
- }
44
+ // Deprecated
45
+ type Seed = types.Seed
56
46
57
- // PDALookups generates Program Derived Addresses (PDA) by combining a derived public key with one or more seeds.
58
- type PDALookups struct {
59
- Name string `json:"name,omitempty"`
60
- // The public key of the PDA to be combined with seeds. If there are multiple PublicKeys
61
- // there will be multiple PDAs generated by combining each PublicKey with the seeds.
62
- PublicKey Lookup `json:"publicKey"`
63
- // Seeds to be derived from an additional lookup
64
- Seeds []Seed `json:"seeds"`
65
- IsSigner bool `json:"isSigner,omitempty"`
66
- IsWritable bool `json:"isWritable,omitempty"`
67
- // OPTIONAL: On-chain location and type of desired data from PDA (e.g. a sub-account of the data account)
68
- InternalField InternalField `json:"internalField,omitempty"`
69
- }
47
+ // Deprecated
48
+ type PDALookups = types.PDALookups
70
49
71
- type InternalField struct {
72
- // must map directly to IDL type
73
- TypeName string `json:"typeName"`
74
- Location string `json:"location"`
75
- IDL string `json:"idl"`
76
- }
50
+ // Deprecated
51
+ type InternalField = types.InternalField
77
52
78
- // LookupTables represents a list of lookup tables that are used to derive addresses for a program.
79
- type LookupTables struct {
80
- DerivedLookupTables []DerivedLookupTable `json:"derivedLookupTables,omitempty"`
81
- StaticLookupTables []solana.PublicKey `json:"staticLookupTables,omitempty"`
82
- }
53
+ // Deprecated
54
+ type LookupTables = types.LookupTables
83
55
84
- // DerivedLookupTable represents a lookup table that is used to derive addresses for a program.
85
- type DerivedLookupTable struct {
86
- Name string `json:"name,omitempty"`
87
- Accounts Lookup `json:"accounts"`
88
- Optional bool `json:"optional"`
89
- }
56
+ // Deprecated
57
+ type DerivedLookupTable = types.DerivedLookupTable
90
58
91
- // AccountsFromLookupTable extracts accounts from a lookup table that was previously read and stored in memory.
92
- type AccountsFromLookupTable struct {
93
- LookupTableName string `json:"lookupTableName"`
94
- IncludeIndexes []int `json:"includeIndexes"`
95
- }
59
+ // Deprecated
60
+ type AccountsFromLookupTable = types.AccountsFromLookupTable
96
61
97
- type ATALookup struct {
98
- // Field that determines whether the ATA lookup is necessary. Basically
99
- // just need to check this field exists. Dot separated location.
100
- Location string
101
- // If the field exists, initialize a ATA account using the Wallet, Token Program, and Mint addresses below
102
- WalletAddress Lookup
103
- TokenProgram Lookup
104
- MintAddress Lookup
105
- Optional bool
106
- }
62
+ // Deprecated
63
+ type ATALookup = types.ATALookup
107
64
108
65
func (l Lookup ) validate () error {
109
66
count := 0
@@ -125,18 +82,18 @@ func (l Lookup) Resolve(ctx context.Context, args any, derivedTableMap map[strin
125
82
return nil , err
126
83
}
127
84
if l .AccountConstant != nil {
128
- return l .AccountConstant . Resolve ( )
85
+ return ResolveAccountConstant ( l .AccountConstant )
129
86
} else if l .AccountLookup != nil {
130
- return l .AccountLookup . Resolve ( args )
87
+ return ResolveAccountLookup ( l .AccountLookup , args )
131
88
} else if l .PDALookups != nil {
132
- return l .PDALookups . Resolve ( ctx , args , derivedTableMap , client )
89
+ return ResolvePDALookups ( ctx , l .PDALookups , args , derivedTableMap , client )
133
90
} else if l .AccountsFromLookupTable != nil {
134
- return l .AccountsFromLookupTable . Resolve ( derivedTableMap )
91
+ return ResolveAccountsFromLookupTable ( l .AccountsFromLookupTable , derivedTableMap )
135
92
}
136
93
return nil , fmt .Errorf ("no lookup type specified" )
137
94
}
138
95
139
- func (ac AccountConstant ) Resolve ( ) ([]* solana.AccountMeta , error ) {
96
+ func ResolveAccountConstant (ac * AccountConstant ) ([]* solana.AccountMeta , error ) {
140
97
address , err := solana .PublicKeyFromBase58 (ac .Address )
141
98
if err != nil {
142
99
return nil , lookupErrWithName (ac .Name , fmt .Errorf ("error getting account from constant: %w" , err ))
@@ -150,7 +107,7 @@ func (ac AccountConstant) Resolve() ([]*solana.AccountMeta, error) {
150
107
}, nil
151
108
}
152
109
153
- func (al AccountLookup ) Resolve ( args any ) ([]* solana.AccountMeta , error ) {
110
+ func ResolveAccountLookup (al * AccountLookup , args any ) ([]* solana.AccountMeta , error ) {
154
111
derivedValues , err := GetValuesAtLocation (args , al .Location )
155
112
if err != nil {
156
113
return nil , lookupErrWithName (al .Name , fmt .Errorf ("%w: %v" , ErrLookupNotFoundAtLocation , err ))
@@ -222,7 +179,7 @@ func resolveBitMap(mb MetaBool, args any, length int) ([]bool, error) {
222
179
return result , nil
223
180
}
224
181
225
- func (alt AccountsFromLookupTable ) Resolve ( derivedTableMap map [string ]map [string ][]* solana.AccountMeta ) ([]* solana.AccountMeta , error ) {
182
+ func ResolveAccountsFromLookupTable (alt * AccountsFromLookupTable , derivedTableMap map [string ]map [string ][]* solana.AccountMeta ) ([]* solana.AccountMeta , error ) {
226
183
// Fetch the inner map for the specified lookup table name
227
184
innerMap , ok := derivedTableMap [alt .LookupTableName ]
228
185
if ! ok {
@@ -252,8 +209,8 @@ func (alt AccountsFromLookupTable) Resolve(derivedTableMap map[string]map[string
252
209
return result , nil
253
210
}
254
211
255
- func ( pda PDALookups ) Resolve ( ctx context.Context , args any , derivedTableMap map [string ]map [string ][]* solana.AccountMeta , client client.MultiClient ) ([]* solana.AccountMeta , error ) {
256
- publicKeys , err := GetAddresses (ctx , args , []Lookup {pda .PublicKey }, derivedTableMap , client )
212
+ func ResolvePDALookups ( ctx context.Context , pda * PDALookups , args any , derivedTableMap map [string ]map [string ][]* solana.AccountMeta , client client.MultiClient ) ([]* solana.AccountMeta , error ) {
213
+ publicKeys , err := GetAddresses (ctx , args , []Lookup {Lookup ( pda .PublicKey ) }, derivedTableMap , client )
257
214
if err != nil {
258
215
return nil , lookupErrWithName (pda .Name , fmt .Errorf ("error getting public key for PDALookups: %w" , err ))
259
216
}
@@ -329,7 +286,7 @@ func (pda PDALookups) Resolve(ctx context.Context, args any, derivedTableMap map
329
286
// to multiple addresses, multiplying the combinations accordingly.
330
287
func getSeedBytesCombinations (
331
288
ctx context.Context ,
332
- lookup PDALookups ,
289
+ lookup * PDALookups ,
333
290
args any ,
334
291
derivedTableMap map [string ]map [string ][]* solana.AccountMeta ,
335
292
client client.MultiClient ,
@@ -364,7 +321,7 @@ func getSeedBytesCombinations(
364
321
}
365
322
} else {
366
323
// Get address seeds from the lookup
367
- seedAddresses , err := GetAddresses (ctx , args , []Lookup {dynamicSeed }, derivedTableMap , client )
324
+ seedAddresses , err := GetAddresses (ctx , args , []Lookup {Lookup ( dynamicSeed ) }, derivedTableMap , client )
368
325
if err != nil {
369
326
return nil , fmt .Errorf ("error getting address seed: %w" , err )
370
327
}
@@ -397,17 +354,13 @@ func getSeedBytesCombinations(
397
354
return allCombinations , nil
398
355
}
399
356
400
- func (l Lookup ) IsNil () bool {
401
- return l .AccountConstant == nil && l .AccountLookup == nil && l .PDALookups == nil && l .AccountsFromLookupTable == nil
402
- }
403
-
404
357
// generatePDAs generates program-derived addresses (PDAs) from public keys and seeds.
405
358
// it will result in a list of PDAs whose length is the product of the number of public keys
406
359
// and the number of seed combinations.
407
360
func generatePDAs (
408
361
publicKeys []* solana.AccountMeta ,
409
362
seedCombos [][][]byte ,
410
- lookup PDALookups ,
363
+ lookup * PDALookups ,
411
364
) ([]* solana.AccountMeta , error ) {
412
365
var results []* solana.AccountMeta
413
366
for _ , publicKeyMeta := range publicKeys {
0 commit comments