Fix transaction bcs ptr fields
This commit is contained in:
@@ -18,6 +18,12 @@ type Transaction struct {
|
||||
|
||||
func NewTransaction() *Transaction {
|
||||
data := TransactionData{}
|
||||
data.V1.Kind = &TransactionKind{
|
||||
ProgrammableTransaction: &ProgrammableTransaction{},
|
||||
}
|
||||
data.V1.Expiration = &TransactionExpiration{
|
||||
None: lo.ToPtr(true),
|
||||
}
|
||||
|
||||
return &Transaction{
|
||||
Data: data,
|
||||
@@ -50,7 +56,7 @@ func (tx *Transaction) SetSenderIfNotSet(sender models.SuiAddress) *Transaction
|
||||
}
|
||||
|
||||
func (tx *Transaction) SetExpiration(expiration TransactionExpiration) *Transaction {
|
||||
tx.Data.V1.Expiration = expiration
|
||||
tx.Data.V1.Expiration = &expiration
|
||||
|
||||
return tx
|
||||
}
|
||||
@@ -66,26 +72,26 @@ func (tx *Transaction) SetGasOwner(owner models.SuiAddress) *Transaction {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tx.Data.V1.GasData.Owner = *addressBytes
|
||||
tx.Data.V1.GasData.Owner = addressBytes
|
||||
|
||||
return tx
|
||||
}
|
||||
|
||||
func (tx *Transaction) SetGasPrice(price uint64) *Transaction {
|
||||
tx.Data.V1.GasData.Price = price
|
||||
tx.Data.V1.GasData.Price = &price
|
||||
|
||||
return tx
|
||||
}
|
||||
|
||||
func (tx *Transaction) SetGasBudget(budget uint64) *Transaction {
|
||||
tx.Data.V1.GasData.Budget = budget
|
||||
tx.Data.V1.GasData.Budget = &budget
|
||||
|
||||
return tx
|
||||
}
|
||||
|
||||
func (tx *Transaction) SetGasBudgetIfNotSet(budget uint64) *Transaction {
|
||||
if tx.Data.V1.GasData.Budget == 0 {
|
||||
tx.Data.V1.GasData.Budget = budget
|
||||
if tx.Data.V1.GasData.Budget == nil {
|
||||
tx.Data.V1.GasData.Budget = &budget
|
||||
}
|
||||
|
||||
return tx
|
||||
@@ -104,15 +110,16 @@ func (tx *Transaction) Add(command Command) Argument {
|
||||
|
||||
func (tx *Transaction) SplitCoins(coin Argument, amount []Argument) Argument {
|
||||
return tx.Add(splitCoins(SplitCoins{
|
||||
Coin: coin,
|
||||
Amount: amount,
|
||||
Coin: &coin,
|
||||
Amount: convertArgumentsToArgumentPtrs(amount),
|
||||
}))
|
||||
}
|
||||
|
||||
func (tx *Transaction) MergeCoins(destination Argument, sources []Argument) Argument {
|
||||
|
||||
return tx.Add(mergeCoins(MergeCoins{
|
||||
Destination: destination,
|
||||
Sources: sources,
|
||||
Destination: &destination,
|
||||
Sources: convertArgumentsToArgumentPtrs(sources),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -183,7 +190,7 @@ func (tx *Transaction) Upgrade(
|
||||
Modules: moduleAddress,
|
||||
Dependencies: dependenciesAddress,
|
||||
Package: *packageIdBytes,
|
||||
Ticket: ticket,
|
||||
Ticket: &ticket,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -204,21 +211,21 @@ func (tx *Transaction) MoveCall(
|
||||
Module: module,
|
||||
Function: function,
|
||||
TypeArguments: typeArguments,
|
||||
Arguments: arguments,
|
||||
Arguments: convertArgumentsToArgumentPtrs(arguments),
|
||||
}))
|
||||
}
|
||||
|
||||
func (tx *Transaction) transferObjects(objects []Argument, address Argument) Argument {
|
||||
return tx.Add(transferObjects(TransferObjects{
|
||||
Objects: objects,
|
||||
Address: address,
|
||||
Objects: convertArgumentsToArgumentPtrs(objects),
|
||||
Address: &address,
|
||||
}))
|
||||
}
|
||||
|
||||
func (tx *Transaction) makeMoveVec(typeValue *string, elements []Argument) Argument {
|
||||
return tx.Add(makeMoveVec(MakeMoveVec{
|
||||
Type: typeValue,
|
||||
Elements: elements,
|
||||
Elements: convertArgumentsToArgumentPtrs(elements),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -298,6 +305,7 @@ func (tx *Transaction) Pure(input any) *Argument {
|
||||
}
|
||||
val = *bcsAddress
|
||||
}
|
||||
val = input
|
||||
|
||||
bcsEncodedMsg := bytes.Buffer{}
|
||||
bcsEncoder := mystenbcs.NewEncoder(&bcsEncodedMsg)
|
||||
@@ -306,7 +314,9 @@ func (tx *Transaction) Pure(input any) *Argument {
|
||||
tx.Data.V1.AddInput(CallArg{UnresolvedPure: lo.ToPtr(bcsEncodedMsg.Bytes())})
|
||||
}
|
||||
|
||||
arg := tx.Data.V1.AddInput(CallArg{Pure: lo.ToPtr(bcsEncodedMsg.Bytes())})
|
||||
arg := tx.Data.V1.AddInput(CallArg{Pure: &Pure{
|
||||
Bytes: bcsEncodedMsg.Bytes(),
|
||||
}})
|
||||
|
||||
return &arg
|
||||
}
|
||||
@@ -385,3 +395,12 @@ func createTransactionResult(index uint16, length *uint16) Argument {
|
||||
Result: lo.ToPtr(index),
|
||||
}
|
||||
}
|
||||
|
||||
func convertArgumentsToArgumentPtrs(args []Argument) []*Argument {
|
||||
argPtrs := make([]*Argument, len(args))
|
||||
for i, arg := range args {
|
||||
argPtrs[i] = &arg
|
||||
}
|
||||
|
||||
return argPtrs
|
||||
}
|
||||
|
||||
@@ -7,24 +7,39 @@ import (
|
||||
"github.com/block-vision/sui-go-sdk/mystenbcs"
|
||||
)
|
||||
|
||||
type TransactionData struct {
|
||||
V1 TransactionDataV1
|
||||
}
|
||||
|
||||
func (td *TransactionData) Marshal() ([]byte, error) {
|
||||
bcsEncodedMsg := bytes.Buffer{}
|
||||
bcsEncoder := mystenbcs.NewEncoder(&bcsEncodedMsg)
|
||||
err := bcsEncoder.Encode(td)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bcsEncodedMsg.Bytes(), nil
|
||||
}
|
||||
|
||||
// TransactionDataV1 https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L1625
|
||||
type TransactionDataV1 struct {
|
||||
Kind *TransactionKind
|
||||
Sender models.SuiAddressBytes
|
||||
Expiration TransactionExpiration
|
||||
GasData GasData
|
||||
Kind TransactionKind
|
||||
Expiration *TransactionExpiration
|
||||
}
|
||||
|
||||
func (td *TransactionDataV1) AddCommand(command Command) (index uint16) {
|
||||
index = uint16(len(td.Kind.ProgrammableTransaction.Commands))
|
||||
td.Kind.ProgrammableTransaction.Commands = append(td.Kind.ProgrammableTransaction.Commands, command)
|
||||
td.Kind.ProgrammableTransaction.Commands = append(td.Kind.ProgrammableTransaction.Commands, &command)
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
func (td *TransactionDataV1) AddInput(input CallArg) Argument {
|
||||
index := uint16(len(td.Kind.ProgrammableTransaction.Inputs))
|
||||
td.Kind.ProgrammableTransaction.Inputs = append(td.Kind.ProgrammableTransaction.Inputs, input)
|
||||
td.Kind.ProgrammableTransaction.Inputs = append(td.Kind.ProgrammableTransaction.Inputs, &input)
|
||||
|
||||
return Argument{
|
||||
Input: &index,
|
||||
@@ -64,37 +79,19 @@ func (td *TransactionDataV1) GetInputObjectIndex(address models.SuiAddress) *uin
|
||||
return nil
|
||||
}
|
||||
|
||||
type TransactionData struct {
|
||||
V1 TransactionDataV1
|
||||
}
|
||||
|
||||
func (td *TransactionData) Marshal() ([]byte, error) {
|
||||
bcsEncodedMsg := bytes.Buffer{}
|
||||
bcsEncoder := mystenbcs.NewEncoder(&bcsEncodedMsg)
|
||||
err := bcsEncoder.Encode(td)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bcsEncodedMsg.Bytes(), nil
|
||||
}
|
||||
|
||||
// GasData https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L1600
|
||||
type GasData struct {
|
||||
Payment []SuiObjectRef
|
||||
Owner models.SuiAddressBytes
|
||||
Price uint64
|
||||
Budget uint64
|
||||
Owner *models.SuiAddressBytes
|
||||
Price *uint64
|
||||
Budget *uint64
|
||||
}
|
||||
|
||||
func (gd *GasData) IsFullySet() bool {
|
||||
if len(gd.Payment) == 0 {
|
||||
return false
|
||||
}
|
||||
if gd.Owner.IsZero() {
|
||||
|
||||
}
|
||||
if gd.Price == 0 || gd.Budget == 0 {
|
||||
if gd.Owner == nil || gd.Price == nil || gd.Budget == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -113,8 +110,8 @@ func (*TransactionExpiration) IsBcsEnum() {}
|
||||
|
||||
// ProgrammableTransaction https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L702
|
||||
type ProgrammableTransaction struct {
|
||||
Inputs []CallArg
|
||||
Commands []Command
|
||||
Inputs []*CallArg
|
||||
Commands []*Command
|
||||
}
|
||||
|
||||
// TransactionKind https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L303
|
||||
@@ -148,12 +145,16 @@ func (tk *TransactionKind) Marshal() ([]byte, error) {
|
||||
// - UnresolvedPure
|
||||
// - UnresolvedObject
|
||||
type CallArg struct {
|
||||
Pure *[]byte
|
||||
Pure *Pure
|
||||
Object *ObjectArg
|
||||
UnresolvedPure any
|
||||
UnresolvedObject *UnresolvedObject
|
||||
}
|
||||
|
||||
type Pure struct {
|
||||
Bytes []byte
|
||||
}
|
||||
|
||||
func (*CallArg) IsBcsEnum() {}
|
||||
|
||||
type UnresolvedObject struct {
|
||||
@@ -201,22 +202,22 @@ type ProgrammableMoveCall struct {
|
||||
Module string
|
||||
Function string
|
||||
TypeArguments []string
|
||||
Arguments []Argument
|
||||
Arguments []*Argument
|
||||
}
|
||||
|
||||
type TransferObjects struct {
|
||||
Objects []Argument
|
||||
Address Argument
|
||||
Objects []*Argument
|
||||
Address *Argument
|
||||
}
|
||||
|
||||
type SplitCoins struct {
|
||||
Coin Argument
|
||||
Amount []Argument
|
||||
Coin *Argument
|
||||
Amount []*Argument
|
||||
}
|
||||
|
||||
type MergeCoins struct {
|
||||
Destination Argument
|
||||
Sources []Argument
|
||||
Destination *Argument
|
||||
Sources []*Argument
|
||||
}
|
||||
|
||||
type Publish struct {
|
||||
@@ -226,14 +227,14 @@ type Publish struct {
|
||||
|
||||
type MakeMoveVec struct {
|
||||
Type *string
|
||||
Elements []Argument
|
||||
Elements []*Argument
|
||||
}
|
||||
|
||||
type Upgrade struct {
|
||||
Modules [][]models.SuiAddressBytes
|
||||
Dependencies []models.SuiAddressBytes
|
||||
Package models.SuiAddressBytes
|
||||
Ticket Argument
|
||||
Ticket *Argument
|
||||
}
|
||||
|
||||
// Argument https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L745
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package transaction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/block-vision/sui-go-sdk/models"
|
||||
@@ -10,31 +11,45 @@ import (
|
||||
|
||||
func TestNewTransaction(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
fun func() *Transaction
|
||||
expectBcsBase64 string
|
||||
name string
|
||||
fun func() *Transaction
|
||||
onlyTransactionKind bool
|
||||
expectBcsBase64 string
|
||||
}{
|
||||
{
|
||||
name: "basic tx",
|
||||
name: "basic tx only kind",
|
||||
fun: func() *Transaction {
|
||||
return setupTransaction()
|
||||
},
|
||||
expectBcsBase64: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAWFiY2FiY2FiY2FiY2FiY2FiY2FiY2FiY2FiY2FiY2FiAgAAAAAAAAAgAAECAwQFBgcICQABAgMEBQYHCAkAAQIDBAUGBwgJAQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgUAAAAAAAAAZAAAAAAAAAAA",
|
||||
onlyTransactionKind: true,
|
||||
expectBcsBase64: "AAAA",
|
||||
},
|
||||
{
|
||||
name: "basic tx",
|
||||
fun: func() *Transaction {
|
||||
tx := setupTransaction()
|
||||
return tx
|
||||
},
|
||||
onlyTransactionKind: false,
|
||||
expectBcsBase64: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAWFiY2FiY2FiY2FiY2FiY2FiY2FiY2FiY2FiY2FiY2FiAgAAAAAAAAAgAAECAwQFBgcICQABAgMEBQYHCAkAAQIDBAUGBwgJAQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgUAAAAAAAAAZAAAAAAAAAAA",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
fmt.Println("Starting test case:", c.name)
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
tx := c.fun()
|
||||
|
||||
bcs, err := tx.build(true)
|
||||
bcs, err := tx.build(c.onlyTransactionKind)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal transaction: %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(bcs, c.expectBcsBase64); diff != "" {
|
||||
if diff := cmp.Diff(c.expectBcsBase64, bcs); diff != "" {
|
||||
t.Errorf("Transaction mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
fmt.Println(bcs)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -64,7 +79,7 @@ func generateObjectRef() SuiObjectRef {
|
||||
func setupTransaction() *Transaction {
|
||||
tx := NewTransaction()
|
||||
tx.SetSender("0x2").
|
||||
SetGasPrice(5).SetGasBudget(100).
|
||||
SetGasPrice(5).SetGasBudget(100).SetGasOwner("0x6").
|
||||
SetGasPayment([]SuiObjectRef{generateObjectRef()})
|
||||
return tx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user