-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Batch contract reads are added and multiple address support was removed. All tests now pass.
- Loading branch information
1 parent
65ae137
commit 27a3c1b
Showing
9 changed files
with
305 additions
and
717 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package chainreader | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/gagliardetto/solana-go" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/values" | ||
) | ||
|
||
type call struct { | ||
ContractName, ReadName string | ||
Params, ReturnVal any | ||
} | ||
|
||
type batchResultWithErr struct { | ||
address string | ||
contractName, readName string | ||
returnVal any | ||
err error | ||
} | ||
|
||
var ( | ||
ErrMissingAccountData = errors.New("account data not found") | ||
) | ||
|
||
type MultipleAccountGetter interface { | ||
GetMultipleAccountData(context.Context, ...solana.PublicKey) ([][]byte, error) | ||
} | ||
|
||
func doMethodBatchCall(ctx context.Context, client MultipleAccountGetter, bindings namespaceBindings, batch []call) ([]batchResultWithErr, error) { | ||
// Create the list of public keys to fetch | ||
keys := make([]solana.PublicKey, len(batch)) | ||
for idx, call := range batch { | ||
binding, err := bindings.GetReadBinding(call.ContractName, call.ReadName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keys[idx] = binding.GetAddress() | ||
} | ||
|
||
// Fetch the account data | ||
data, err := client.GetMultipleAccountData(ctx, keys...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
results := make([]batchResultWithErr, len(batch)) | ||
|
||
// decode batch call results | ||
for idx, call := range batch { | ||
results[idx] = batchResultWithErr{ | ||
address: keys[idx].String(), | ||
contractName: call.ContractName, | ||
readName: call.ReadName, | ||
returnVal: call.ReturnVal, | ||
} | ||
|
||
if data[idx] == nil || len(data[idx]) == 0 { | ||
results[idx].err = ErrMissingAccountData | ||
|
||
continue | ||
} | ||
|
||
binding, err := bindings.GetReadBinding(results[idx].contractName, results[idx].readName) | ||
if err != nil { | ||
results[idx].err = err | ||
|
||
continue | ||
} | ||
|
||
ptrToValue, isValue := call.ReturnVal.(*values.Value) | ||
if !isValue { | ||
results[idx].err = errors.Join( | ||
results[idx].err, | ||
binding.Decode(ctx, data[idx], results[idx].returnVal), | ||
) | ||
|
||
continue | ||
} | ||
|
||
contractType, err := binding.CreateType(false) | ||
if err != nil { | ||
results[idx].err = err | ||
|
||
continue | ||
} | ||
|
||
results[idx].err = errors.Join( | ||
results[idx].err, | ||
binding.Decode(ctx, data[idx], contractType), | ||
) | ||
|
||
value, err := values.Wrap(contractType) | ||
if err != nil { | ||
results[idx].err = errors.Join(results[idx].err, err) | ||
|
||
continue | ||
} | ||
|
||
*ptrToValue = value | ||
} | ||
|
||
return results, nil | ||
} |
Oops, something went wrong.