-
Notifications
You must be signed in to change notification settings - Fork 1
feat: transaction serializer #42
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
Merged
Merged
Changes from all commits
Commits
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package txm | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/fbsobreira/gotron-sdk/pkg/abi" | ||
| "github.com/fbsobreira/gotron-sdk/pkg/address" | ||
| "github.com/fbsobreira/gotron-sdk/pkg/http/common" | ||
| "github.com/fbsobreira/gotron-sdk/pkg/proto/core" | ||
| "google.golang.org/protobuf/proto" | ||
| "google.golang.org/protobuf/types/known/anypb" | ||
| ) | ||
|
|
||
| type Serializer struct { | ||
| TransactionType core.Transaction_Contract_ContractType | ||
| FromAddress address.Address | ||
| ContractAddress address.Address | ||
| Method string | ||
| Params []any | ||
| CallValueSun int64 | ||
| FeeLimitSun int64 | ||
| RefBlockBytes []byte | ||
| RefBlockHash []byte | ||
| ExpirationMillis int64 | ||
| TimestampMillis int64 | ||
| } | ||
|
|
||
| const DefaultExpirationMillis = 30_000 // 30 seconds | ||
|
|
||
| func (p *Serializer) BuildTransaction() (*common.Transaction, error) { | ||
| if p.TransactionType != core.Transaction_Contract_TriggerSmartContract { | ||
| return nil, fmt.Errorf("invalid transaction type: %d", p.TransactionType) | ||
| } | ||
|
|
||
| if len(p.RefBlockBytes) != 2 && len(p.RefBlockHash) != 8 { | ||
| return nil, fmt.Errorf("invalid ref block bytes or hash") | ||
| } | ||
|
|
||
| callData, err := p.buildCallData() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to build call data: %+w", err) | ||
| } | ||
|
|
||
| smartContractCall := &core.TriggerSmartContract{ | ||
| OwnerAddress: p.FromAddress.Bytes(), | ||
| ContractAddress: p.ContractAddress.Bytes(), | ||
| Data: callData, | ||
| CallValue: p.CallValueSun, | ||
| } | ||
|
|
||
| callPayload, err := anypb.New(smartContractCall) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create call payload: %+w", err) | ||
| } | ||
|
|
||
| contract := &core.Transaction_Contract{ | ||
| Parameter: callPayload, | ||
| Type: core.Transaction_Contract_TriggerSmartContract, | ||
| } | ||
|
|
||
| now := time.Now().UnixMilli() | ||
| timestamp := p.TimestampMillis | ||
| if timestamp == 0 { | ||
| timestamp = now | ||
| } | ||
|
|
||
| expiration := p.ExpirationMillis | ||
| if expiration == 0 { | ||
| expiration = now + DefaultExpirationMillis | ||
| } | ||
|
|
||
| rawData := &core.TransactionRaw{ | ||
| Contract: []*core.Transaction_Contract{contract}, | ||
| FeeLimit: p.FeeLimitSun, | ||
| Expiration: expiration, | ||
| Timestamp: timestamp, | ||
| RefBlockBytes: p.RefBlockBytes, | ||
| RefBlockHash: p.RefBlockHash, | ||
| } | ||
|
|
||
| rawBytes, err := proto.Marshal(rawData) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal raw data: %+w", err) | ||
| } | ||
|
|
||
| hash := sha256.Sum256(rawBytes) | ||
| txIDHex := hex.EncodeToString(hash[:]) | ||
| rawDataHex := hex.EncodeToString(rawBytes) | ||
|
|
||
| commonRawData := common.RawData{ | ||
| Contract: []common.Contract{ | ||
| { | ||
| Parameter: common.Parameter{ | ||
| Value: common.ParameterValue{ | ||
| OwnerAddress: p.FromAddress.String(), | ||
| ContractAddress: p.ContractAddress.String(), | ||
| Data: hex.EncodeToString(smartContractCall.Data), | ||
| Amount: p.CallValueSun, | ||
| }, | ||
| TypeUrl: "type.googleapis.com/protocol.TriggerSmartContract", | ||
| }, | ||
| Type: "TriggerSmartContract", | ||
| }, | ||
| }, | ||
| RefBlockBytes: hex.EncodeToString(p.RefBlockBytes), | ||
| RefBlockHash: hex.EncodeToString(p.RefBlockHash), | ||
| Expiration: expiration, | ||
| FeeLimit: p.FeeLimitSun, | ||
| Timestamp: timestamp, | ||
| } | ||
|
|
||
| return &common.Transaction{ | ||
| Visible: true, | ||
| TxID: txIDHex, | ||
| RawData: commonRawData, | ||
| RawDataHex: rawDataHex, | ||
| }, nil | ||
| } | ||
|
|
||
| func (p *Serializer) buildCallData() ([]byte, error) { | ||
| parsed, err := abi.Pack(p.Method, p.Params) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to pack params: %+w", err) | ||
| } | ||
| return parsed, 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
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.
this can be implicit and we don't need the core import in this file or the != TriggerSmartContract check in serializer.go
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.
agreed, aim here was to ensure that if this changes in the future to catch this