Add signer to transaction struct
This commit is contained in:
@@ -2,17 +2,18 @@ package transaction
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"math"
|
||||
|
||||
"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/mystenbcs"
|
||||
"github.com/block-vision/sui-go-sdk/signer"
|
||||
"github.com/block-vision/sui-go-sdk/utils"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
Data TransactionData
|
||||
Signer *signer.Signer
|
||||
}
|
||||
|
||||
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 {
|
||||
tx.Data.Sender = &sender
|
||||
return tx
|
||||
@@ -151,7 +157,7 @@ func (tx *Transaction) Object(inputObject InputObject) (Argument, error) {
|
||||
if inputObject.Value == nil {
|
||||
id = inputObject.ObjectId
|
||||
if id == "" {
|
||||
return nil, errors.New("object id is empty")
|
||||
return nil, ErrObjectIdNotSet
|
||||
}
|
||||
|
||||
callArg = UnresolvedObject{
|
||||
@@ -169,7 +175,7 @@ func (tx *Transaction) Object(inputObject InputObject) (Argument, error) {
|
||||
case Receiving:
|
||||
id = objArg.(Receiving).Value.ObjectId
|
||||
default:
|
||||
return nil, errors.New("object value is not supported")
|
||||
return nil, ErrObjectTypeNotSupported
|
||||
}
|
||||
|
||||
callArg = Object{
|
||||
@@ -227,12 +233,55 @@ func (tx *Transaction) Pure(inputPure InputPure) (Argument, error) {
|
||||
return arg, nil
|
||||
}
|
||||
|
||||
func createTransactionResult(index uint16, length *uint16) Argument {
|
||||
// TODO: Support multiple results
|
||||
if length == nil {
|
||||
length = math.MaxInt
|
||||
func (tx *Transaction) ToSuiExecuteTransactionBlockRequest(
|
||||
options models.SuiTransactionBlockOptions,
|
||||
requestType string,
|
||||
) (*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{
|
||||
Value: NestedResultValue{
|
||||
Index: index,
|
||||
|
||||
@@ -12,8 +12,7 @@ type TransactionData struct {
|
||||
Sender *models.SuiAddress
|
||||
Expiration TransactionExpiration
|
||||
GasData GasData
|
||||
Inputs []CallArg
|
||||
Commands []Command
|
||||
ProgrammableTransaction
|
||||
}
|
||||
|
||||
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