diff --git a/go.mod b/go.mod index b38de8e..91174e8 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,8 @@ require ( github.com/gorilla/websocket v1.5.0 github.com/machinebox/graphql v0.2.2 github.com/mr-tron/base58 v1.2.0 - github.com/stretchr/testify v1.8.2 + github.com/samber/lo v1.49.1 + github.com/stretchr/testify v1.10.0 github.com/tidwall/gjson v1.14.4 github.com/tyler-smith/go-bip39 v1.1.0 golang.org/x/crypto v0.23.0 @@ -29,6 +30,6 @@ require ( github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect + golang.org/x/text v0.21.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index cb0348a..abfea75 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.2 h1:7z68G0FCGvDk646jz1AelTYNYWrTNm0bEcFAo147wt4= github.com/leodido/go-urn v1.2.2/go.mod h1:kUaIbLZWttglzwNuG0pgsh5vuV6u2YcGBYz1hIPjtOQ= github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkBZfo= @@ -23,18 +25,23 @@ github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ= github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ= +github.com/samber/lo v1.49.1 h1:4BIFyVfuQSEpluc7Fua+j1NolZHiEHEpaSEKdsH0tew= +github.com/samber/lo v1.49.1/go.mod h1:dO6KHFzUKXgP8LDhU0oI8d2hekjXnGOu0DB8Jecxd6o= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= @@ -53,12 +60,13 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/transaction/command.go b/transaction/command.go index 3f28a55..5080771 100644 --- a/transaction/command.go +++ b/transaction/command.go @@ -1,43 +1,45 @@ package transaction +import "github.com/samber/lo" + func moveCall(input ProgrammableMoveCall) Command { return Command{ - MoveCall: input, + MoveCall: lo.ToPtr(input), } } func transferObjects(input TransferObjects) Command { return Command{ - TransferObjects: input, + TransferObjects: lo.ToPtr(input), } } func splitCoins(input SplitCoins) Command { return Command{ - SplitCoins: input, + SplitCoins: lo.ToPtr(input), } } func mergeCoins(input MergeCoins) Command { return Command{ - MergeCoins: input, + MergeCoins: lo.ToPtr(input), } } func publish(input Publish) Command { return Command{ - Publish: input, + Publish: lo.ToPtr(input), } } func makeMoveVec(input MakeMoveVec) Command { return Command{ - MakeMoveVec: input, + MakeMoveVec: lo.ToPtr(input), } } func upgrade(input Upgrade) Command { return Command{ - Upgrade: input, + Upgrade: lo.ToPtr(input), } } diff --git a/transaction/transaction.go b/transaction/transaction.go index 0a5433f..70020de 100644 --- a/transaction/transaction.go +++ b/transaction/transaction.go @@ -8,6 +8,7 @@ import ( "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/samber/lo" ) type Transaction struct { @@ -92,7 +93,7 @@ func (tx *Transaction) SetGasBudgetIfNotSet(budget uint64) *Transaction { func (tx *Transaction) Gas() Argument { return Argument{ - GasCoin: true, + GasCoin: lo.ToPtr(true), } } @@ -234,7 +235,7 @@ func (tx *Transaction) Object(input any) *Argument { } arg := tx.Data.V1.AddInput(CallArg{ - UnresolvedObject: UnresolvedObject{ + UnresolvedObject: &UnresolvedObject{ ObjectId: *addressBytes, }, }) @@ -302,10 +303,10 @@ func (tx *Transaction) Pure(input any) *Argument { bcsEncoder := mystenbcs.NewEncoder(&bcsEncodedMsg) err := bcsEncoder.Encode(val) if err != nil { - tx.Data.V1.AddInput(CallArg{UnresolvedPure: bcsEncodedMsg.Bytes()}) + tx.Data.V1.AddInput(CallArg{UnresolvedPure: lo.ToPtr(bcsEncodedMsg.Bytes())}) } - arg := tx.Data.V1.AddInput(CallArg{Pure: bcsEncodedMsg.Bytes()}) + arg := tx.Data.V1.AddInput(CallArg{Pure: lo.ToPtr(bcsEncodedMsg.Bytes())}) return &arg } @@ -376,12 +377,11 @@ func (tx *Transaction) build(onlyTransactionKind bool) (string, error) { func createTransactionResult(index uint16, length *uint16) Argument { if length == nil { - m := uint16(math.MaxUint16) - length = &m + length = lo.ToPtr(uint16(math.MaxUint16)) } // TODO: Support NestedResult return Argument{ - Result: index, + Result: lo.ToPtr(index), } } diff --git a/transaction/transaction_data.go b/transaction/transaction_data.go index 6eabc76..7e9cd7a 100644 --- a/transaction/transaction_data.go +++ b/transaction/transaction_data.go @@ -1,6 +1,8 @@ package transaction import ( + "bytes" + "github.com/block-vision/sui-go-sdk/models" "github.com/block-vision/sui-go-sdk/mystenbcs" ) @@ -21,11 +23,11 @@ func (td *TransactionDataV1) AddCommand(command Command) (index uint16) { } func (td *TransactionDataV1) AddInput(input CallArg) Argument { - index := len(td.Kind.ProgrammableTransaction.Inputs) + index := uint16(len(td.Kind.ProgrammableTransaction.Inputs)) td.Kind.ProgrammableTransaction.Inputs = append(td.Kind.ProgrammableTransaction.Inputs, input) return Argument{ - Input: uint16(index), + Input: &index, } } @@ -67,8 +69,14 @@ type TransactionData struct { } func (td *TransactionData) Marshal() ([]byte, error) { - // TODO - return []byte{}, nil + 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 @@ -97,15 +105,16 @@ func (gd *GasData) IsFullySet() bool { // - None // - Epoch type TransactionExpiration struct { - mystenbcs.Option[*uint64] + None *bool + Epoch *uint64 } func (*TransactionExpiration) IsBcsEnum() {} // ProgrammableTransaction https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L702 type ProgrammableTransaction struct { - Inputs []CallArg `bcs:""` - Commands []Command `bcs:""` + Inputs []CallArg + Commands []Command } // TransactionKind https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L303 @@ -114,17 +123,23 @@ type ProgrammableTransaction struct { // - Genesis // - ConsensusCommitPrologue type TransactionKind struct { - ProgrammableTransaction ProgrammableTransaction - ChangeEpoch bool - Genesis bool - ConsensusCommitPrologue bool + ProgrammableTransaction *ProgrammableTransaction + ChangeEpoch *bool + Genesis *bool + ConsensusCommitPrologue *bool } func (*TransactionKind) IsBcsEnum() {} -func (*TransactionKind) Marshal() ([]byte, error) { - // TODO - return []byte{}, nil +func (tk *TransactionKind) Marshal() ([]byte, error) { + bcsEncodedMsg := bytes.Buffer{} + bcsEncoder := mystenbcs.NewEncoder(&bcsEncodedMsg) + err := bcsEncoder.Encode(tk) + if err != nil { + return nil, err + } + + return bcsEncodedMsg.Bytes(), nil } // CallArg https://github.com/MystenLabs/sui/blob/fb27c6c7166f5e4279d5fd1b2ebc5580ca0e81b2/crates/sui-types/src/transaction.rs#L80 @@ -133,12 +148,14 @@ func (*TransactionKind) Marshal() ([]byte, error) { // - UnresolvedPure // - UnresolvedObject type CallArg struct { - Pure []byte - Object ObjectArg + Pure *[]byte + Object *ObjectArg UnresolvedPure any - UnresolvedObject UnresolvedObject + UnresolvedObject *UnresolvedObject } +func (*CallArg) IsBcsEnum() {} + type UnresolvedObject struct { ObjectId models.SuiAddressBytes // Version @@ -146,16 +163,14 @@ type UnresolvedObject struct { // InitialSharedVersion } -func (*CallArg) IsBcsEnum() {} - // ObjectArg // - ImmOrOwnedObject // - SharedObject // - Receiving type ObjectArg struct { - ImmOrOwnedObject SuiObjectRef - SharedObject SharedObjectRef - Receiving SuiObjectRef + ImmOrOwnedObject *SuiObjectRef + SharedObject *SharedObjectRef + Receiving *SuiObjectRef } func (*ObjectArg) IsBcsEnum() {} @@ -169,13 +184,13 @@ func (*ObjectArg) IsBcsEnum() {} // - MakeMoveVec // - Upgrade type Command struct { - MoveCall ProgrammableMoveCall - TransferObjects TransferObjects - SplitCoins SplitCoins - MergeCoins MergeCoins - Publish Publish - MakeMoveVec MakeMoveVec - Upgrade Upgrade + MoveCall *ProgrammableMoveCall + TransferObjects *TransferObjects + SplitCoins *SplitCoins + MergeCoins *MergeCoins + Publish *Publish + MakeMoveVec *MakeMoveVec + Upgrade *Upgrade } func (*Command) IsBcsEnum() {} @@ -210,7 +225,7 @@ type Publish struct { } type MakeMoveVec struct { - Type *string `bcs:"optional"` + Type *string Elements []Argument } @@ -227,10 +242,10 @@ type Upgrade struct { // - Result // - NestedResult type Argument struct { - GasCoin bool - Input uint16 - Result uint16 - NestedResult NestedResult + GasCoin *bool + Input *uint16 + Result *uint16 + NestedResult *NestedResult } func (*Argument) IsBcsEnum() {}