Add signer to transaction struct
This commit is contained in:
@@ -2,17 +2,18 @@ package transaction
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/block-vision/sui-go-sdk/models"
|
"github.com/block-vision/sui-go-sdk/models"
|
||||||
"github.com/block-vision/sui-go-sdk/models/sui_types"
|
"github.com/block-vision/sui-go-sdk/models/sui_types"
|
||||||
"github.com/block-vision/sui-go-sdk/mystenbcs"
|
"github.com/block-vision/sui-go-sdk/mystenbcs"
|
||||||
|
"github.com/block-vision/sui-go-sdk/signer"
|
||||||
"github.com/block-vision/sui-go-sdk/utils"
|
"github.com/block-vision/sui-go-sdk/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
Data TransactionData
|
Data TransactionData
|
||||||
|
Signer *signer.Signer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTransaction() *Transaction {
|
func NewTransaction() *Transaction {
|
||||||
@@ -23,6 +24,11 @@ func NewTransaction() *Transaction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *Transaction) SetSigner(signer *signer.Signer) *Transaction {
|
||||||
|
tx.Signer = signer
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Transaction) SetSender(sender models.SuiAddress) *Transaction {
|
func (tx *Transaction) SetSender(sender models.SuiAddress) *Transaction {
|
||||||
tx.Data.Sender = &sender
|
tx.Data.Sender = &sender
|
||||||
return tx
|
return tx
|
||||||
@@ -151,7 +157,7 @@ func (tx *Transaction) Object(inputObject InputObject) (Argument, error) {
|
|||||||
if inputObject.Value == nil {
|
if inputObject.Value == nil {
|
||||||
id = inputObject.ObjectId
|
id = inputObject.ObjectId
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return nil, errors.New("object id is empty")
|
return nil, ErrObjectIdNotSet
|
||||||
}
|
}
|
||||||
|
|
||||||
callArg = UnresolvedObject{
|
callArg = UnresolvedObject{
|
||||||
@@ -169,7 +175,7 @@ func (tx *Transaction) Object(inputObject InputObject) (Argument, error) {
|
|||||||
case Receiving:
|
case Receiving:
|
||||||
id = objArg.(Receiving).Value.ObjectId
|
id = objArg.(Receiving).Value.ObjectId
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("object value is not supported")
|
return nil, ErrObjectTypeNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
callArg = Object{
|
callArg = Object{
|
||||||
@@ -227,12 +233,55 @@ func (tx *Transaction) Pure(inputPure InputPure) (Argument, error) {
|
|||||||
return arg, nil
|
return arg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTransactionResult(index uint16, length *uint16) Argument {
|
func (tx *Transaction) ToSuiExecuteTransactionBlockRequest(
|
||||||
// TODO: Support multiple results
|
options models.SuiTransactionBlockOptions,
|
||||||
if length == nil {
|
requestType string,
|
||||||
length = math.MaxInt
|
) (*models.SuiExecuteTransactionBlockRequest, error) {
|
||||||
|
if tx.Signer == nil {
|
||||||
|
return nil, ErrSignerNotSet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
txBytes, err := tx.buildTransaction()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
signedTransaction, err := tx.Signer.SignTransaction(txBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.SuiExecuteTransactionBlockRequest{
|
||||||
|
TxBytes: signedTransaction.TxBytes,
|
||||||
|
Signature: []string{signedTransaction.Signature},
|
||||||
|
Options: options,
|
||||||
|
RequestType: requestType,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Transaction) buildTransaction() (string, error) {
|
||||||
|
if tx.Signer == nil {
|
||||||
|
return "", ErrSignerNotSet
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.SetGasBudgetIfNotSet(defaultGasBudget)
|
||||||
|
tx.SetSenderIfNotSet(models.SuiAddress(tx.Signer.Address))
|
||||||
|
|
||||||
|
return tx.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Transaction) build() (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTransactionResult(index uint16, length *uint16) Argument {
|
||||||
|
if length == nil {
|
||||||
|
max := uint16(math.MaxUint16)
|
||||||
|
length = &max
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Support multiple results
|
||||||
|
|
||||||
return NestedResult{
|
return NestedResult{
|
||||||
Value: NestedResultValue{
|
Value: NestedResultValue{
|
||||||
Index: index,
|
Index: index,
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ type TransactionData struct {
|
|||||||
Sender *models.SuiAddress
|
Sender *models.SuiAddress
|
||||||
Expiration TransactionExpiration
|
Expiration TransactionExpiration
|
||||||
GasData GasData
|
GasData GasData
|
||||||
Inputs []CallArg
|
ProgrammableTransaction
|
||||||
Commands []Command
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (td *TransactionData) AddCommand(command Command) (index uint16) {
|
func (td *TransactionData) AddCommand(command Command) (index uint16) {
|
||||||
|
|||||||
13
transaction/utils.go
Normal file
13
transaction/utils.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package transaction
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultGasBudget = 50000000
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrSignerNotSet = errors.New("signer not set")
|
||||||
|
ErrObjectIdNotSet = errors.New("object id not set")
|
||||||
|
ErrObjectTypeNotSupported = errors.New("object type not supported")
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user