-
Notifications
You must be signed in to change notification settings - Fork 4
Add Aptos ViewRequest Capability #117
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
Open
cawthorne
wants to merge
2
commits into
capabilities-development
Choose a base branch
from
feature/aptos-local-cre-minimal
base: capabilities-development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,197 @@ | ||
| // Hand-written Aptos SDK client (mirrors evm client_sdk_gen pattern). | ||
| // Capability ID: aptos:ChainSelector:<chainSelector>@1.0.0, method "View". | ||
|
|
||
| package aptos | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
|
|
||
| "google.golang.org/protobuf/encoding/protowire" | ||
| "google.golang.org/protobuf/proto" | ||
| "google.golang.org/protobuf/types/known/anypb" | ||
|
|
||
| sdkpb "github.com/smartcontractkit/chainlink-protos/cre/go/sdk" | ||
| "github.com/smartcontractkit/cre-sdk-go/cre" | ||
| ) | ||
|
|
||
| type Client struct { | ||
| ChainSelector uint64 | ||
| } | ||
|
|
||
| func (c *Client) WriteReport(runtime cre.Runtime, input *WriteReportRequest) cre.Promise[*WriteReportReply] { | ||
| if input == nil { | ||
| return cre.PromiseFromResult[*WriteReportReply](nil, errors.New("nil WriteReportRequest")) | ||
| } | ||
|
|
||
| wrapped := &anypb.Any{} | ||
| err := anypb.MarshalFrom(wrapped, input, proto.MarshalOptions{Deterministic: true}) | ||
| if err != nil { | ||
| return cre.PromiseFromResult[*WriteReportReply](nil, err) | ||
| } | ||
|
|
||
| capID := "aptos:ChainSelector:" + strconv.FormatUint(c.ChainSelector, 10) + "@1.0.0" | ||
| return cre.Then(runtime.CallCapability(&sdkpb.CapabilityRequest{ | ||
| Id: capID, | ||
| Payload: wrapped, | ||
| Method: "WriteReport", | ||
| }), func(i *sdkpb.CapabilityResponse) (_ *WriteReportReply, err error) { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| err = fmt.Errorf("aptos write decode panic: %v", r) | ||
| } | ||
| }() | ||
| if i == nil { | ||
| return nil, errors.New("nil capability response") | ||
| } | ||
| switch payload := i.Response.(type) { | ||
| case *sdkpb.CapabilityResponse_Error: | ||
| return nil, errors.New(payload.Error) | ||
| case *sdkpb.CapabilityResponse_Payload: | ||
| if payload.Payload == nil { | ||
| return nil, errors.New("nil capability payload") | ||
| } | ||
| return decodeWriteReportReply(payload.Payload.Value) | ||
| default: | ||
| return nil, errors.New("unexpected response type") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func (c *Client) View(runtime cre.Runtime, input *ViewRequest) cre.Promise[*ViewReply] { | ||
| if input == nil { | ||
| return cre.PromiseFromResult[*ViewReply](nil, errors.New("nil ViewRequest")) | ||
| } | ||
|
|
||
| wrapped := &anypb.Any{} | ||
| err := anypb.MarshalFrom(wrapped, input, proto.MarshalOptions{Deterministic: true}) | ||
| if err != nil { | ||
| return cre.PromiseFromResult[*ViewReply](nil, err) | ||
| } | ||
| capID := "aptos:ChainSelector:" + strconv.FormatUint(c.ChainSelector, 10) + "@1.0.0" | ||
| capCallResponse := cre.Then(runtime.CallCapability(&sdkpb.CapabilityRequest{ | ||
| Id: capID, | ||
| Payload: wrapped, | ||
| Method: "View", | ||
| }), func(i *sdkpb.CapabilityResponse) (_ *ViewReply, err error) { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| err = fmt.Errorf("aptos view decode panic: %v", r) | ||
| } | ||
| }() | ||
| if i == nil { | ||
| return nil, errors.New("nil capability response") | ||
| } | ||
| switch payload := i.Response.(type) { | ||
| case *sdkpb.CapabilityResponse_Error: | ||
| return nil, errors.New(payload.Error) | ||
| case *sdkpb.CapabilityResponse_Payload: | ||
| if payload.Payload == nil { | ||
| return nil, errors.New("nil capability payload") | ||
| } | ||
| return decodeViewReply(payload.Payload.Value) | ||
| default: | ||
| return nil, errors.New("unexpected response type") | ||
| } | ||
| }) | ||
| return capCallResponse | ||
| } | ||
|
|
||
| // decodeWriteReportReply decodes capabilities.blockchain.aptos.v1alpha.WriteReportReply | ||
| // via protobuf wire parsing to avoid runtime reflection panics under WASM. | ||
| func decodeWriteReportReply(b []byte) (*WriteReportReply, error) { | ||
| out := &WriteReportReply{} | ||
| for len(b) > 0 { | ||
| num, typ, n := protowire.ConsumeTag(b) | ||
| if n < 0 { | ||
| return nil, fmt.Errorf("decode WriteReportReply tag: %v", protowire.ParseError(n)) | ||
| } | ||
| b = b[n:] | ||
| switch num { | ||
| case 1: // tx_status enum (varint) | ||
| if typ != protowire.VarintType { | ||
| return nil, fmt.Errorf("decode WriteReportReply.tx_status: unexpected wire type %d", typ) | ||
| } | ||
| v, m := protowire.ConsumeVarint(b) | ||
| if m < 0 { | ||
| return nil, fmt.Errorf("decode WriteReportReply.tx_status varint: %v", protowire.ParseError(m)) | ||
| } | ||
| out.TxStatus = TxStatus(v) | ||
| b = b[m:] | ||
| case 2: // tx_hash string | ||
| if typ != protowire.BytesType { | ||
| return nil, fmt.Errorf("decode WriteReportReply.tx_hash: unexpected wire type %d", typ) | ||
| } | ||
| v, m := protowire.ConsumeBytes(b) | ||
| if m < 0 { | ||
| return nil, fmt.Errorf("decode WriteReportReply.tx_hash bytes: %v", protowire.ParseError(m)) | ||
| } | ||
| txHash := string(v) | ||
| out.TxHash = &txHash | ||
| b = b[m:] | ||
| case 3: // transaction_fee varint | ||
| if typ != protowire.VarintType { | ||
| return nil, fmt.Errorf("decode WriteReportReply.transaction_fee: unexpected wire type %d", typ) | ||
| } | ||
| v, m := protowire.ConsumeVarint(b) | ||
| if m < 0 { | ||
| return nil, fmt.Errorf("decode WriteReportReply.transaction_fee varint: %v", protowire.ParseError(m)) | ||
| } | ||
| fee := uint64(v) | ||
| out.TransactionFee = &fee | ||
| b = b[m:] | ||
| case 4: // error_message string | ||
| if typ != protowire.BytesType { | ||
| return nil, fmt.Errorf("decode WriteReportReply.error_message: unexpected wire type %d", typ) | ||
| } | ||
| v, m := protowire.ConsumeBytes(b) | ||
| if m < 0 { | ||
| return nil, fmt.Errorf("decode WriteReportReply.error_message bytes: %v", protowire.ParseError(m)) | ||
| } | ||
| msg := string(v) | ||
| out.ErrorMessage = &msg | ||
| b = b[m:] | ||
| default: | ||
| m := protowire.ConsumeFieldValue(num, typ, b) | ||
| if m < 0 { | ||
| return nil, fmt.Errorf("decode WriteReportReply skip field %d: %v", num, protowire.ParseError(m)) | ||
| } | ||
| b = b[m:] | ||
| } | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // decodeViewReply decodes capabilities.blockchain.aptos.v1alpha.ViewReply using | ||
| // protobuf wire parsing to avoid runtime reflection panics seen under WASM. | ||
| // ViewReply currently contains a single bytes field: data = 1. | ||
| func decodeViewReply(b []byte) (*ViewReply, error) { | ||
| out := &ViewReply{} | ||
| for len(b) > 0 { | ||
| num, typ, n := protowire.ConsumeTag(b) | ||
| if n < 0 { | ||
| return nil, fmt.Errorf("decode ViewReply tag: %v", protowire.ParseError(n)) | ||
| } | ||
| b = b[n:] | ||
| switch num { | ||
| case 1: // data bytes | ||
| if typ != protowire.BytesType { | ||
| return nil, fmt.Errorf("decode ViewReply.data: unexpected wire type %d", typ) | ||
| } | ||
| v, m := protowire.ConsumeBytes(b) | ||
| if m < 0 { | ||
| return nil, fmt.Errorf("decode ViewReply.data bytes: %v", protowire.ParseError(m)) | ||
| } | ||
| out.Data = append([]byte(nil), v...) | ||
| b = b[m:] | ||
| default: | ||
| m := protowire.ConsumeFieldValue(num, typ, b) | ||
| if m < 0 { | ||
| return nil, fmt.Errorf("decode ViewReply skip field %d: %v", num, protowire.ParseError(m)) | ||
| } | ||
| b = b[m:] | ||
| } | ||
| } | ||
| return out, nil | ||
| } | ||
This file contains hidden or 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,2 @@ | ||
| //go:generate go run ./generate | ||
| package aptos |
This file contains hidden or 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,43 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-protos/cre/go/installer/pkg" | ||
| "github.com/smartcontractkit/cre-sdk-go/generator/protos" | ||
| ) | ||
|
|
||
| func main() { | ||
| cwd, _ := os.Getwd() | ||
| root := cwd | ||
| for { | ||
| goMod := filepath.Join(root, "go.mod") | ||
| aptosDir := filepath.Join(root, "capabilities", "blockchain", "aptos") | ||
| if _, err := os.Stat(goMod); err == nil { | ||
| if _, err := os.Stat(aptosDir); err == nil { | ||
| break | ||
| } | ||
| } | ||
| parent := filepath.Dir(root) | ||
| if parent == root { | ||
| panic("run from cre-sdk-go repo root or from capabilities/blockchain/aptos") | ||
| } | ||
| root = parent | ||
| } | ||
| _ = os.Chdir(root) | ||
| gen, err := protos.NewGeneratorAndInstallToolsForCapability() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| config := &pkg.CapabilityConfig{ | ||
| Category: "blockchain", | ||
| Pkg: "aptos", | ||
| MajorVersion: 1, | ||
| PreReleaseTag: "alpha", | ||
| Files: []string{"client.proto"}, | ||
| } | ||
| if err := gen.GenerateMany(map[string]*pkg.CapabilityConfig{"capabilities/blockchain/aptos": config}); err != nil { | ||
| panic(err) | ||
| } | ||
| } |
This file contains hidden or 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,16 @@ | ||
| module github.com/smartcontractkit/cre-sdk-go/capabilities/blockchain/aptos | ||
|
|
||
| go 1.25.3 | ||
|
|
||
| require ( | ||
| github.com/smartcontractkit/chainlink-protos/cre/go v0.0.0-20260310142526-ecda729848f6 | ||
| github.com/smartcontractkit/cre-sdk-go v1.5.0 | ||
| google.golang.org/protobuf v1.36.8 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/go-viper/mapstructure/v2 v2.4.0 // indirect | ||
| github.com/shopspring/decimal v1.4.0 // indirect | ||
| ) | ||
|
|
||
| replace github.com/smartcontractkit/cre-sdk-go => ../../.. |
This file contains hidden or 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,18 @@ | ||
| github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= | ||
| github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= | ||
| github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= | ||
| github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= | ||
| github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= | ||
| github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= | ||
| github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k= | ||
| github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME= | ||
| github.com/smartcontractkit/chainlink-protos/cre/go v0.0.0-20260310142526-ecda729848f6 h1:i15omvye/jtFlD48hqR88xt4bFcxMJqevNTJU1dgOQQ= | ||
| github.com/smartcontractkit/chainlink-protos/cre/go v0.0.0-20260310142526-ecda729848f6/go.mod h1:Jqt53s27Tr0jDl8mdBXg1xhu6F8Fci8JOuq43tgHOM8= | ||
| github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= | ||
| github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= | ||
| google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc= | ||
| google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this hand written ?