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