Skip to content

Commit

Permalink
test: Enable test of DevInspectTransactionBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Jun 11, 2024
1 parent b39250b commit 244be4a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 43 deletions.
15 changes: 9 additions & 6 deletions sui/api_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@ import (
"github.com/howjmay/sui-go/sui_types"
)

// the txKindBytes is TransactionKind in base64
// which is different from `DryRunTransaction` and `ExecuteTransactionBlock`
// `DryRunTransaction` and `ExecuteTransactionBlock` takes `TransactionData` in base64
func (s *ImplSuiAPI) DevInspectTransactionBlock(
ctx context.Context,
senderAddress *sui_types.SuiAddress,
txByte sui_types.Base64Data,
txKindBytes sui_types.Base64Data,
gasPrice *models.SafeSuiBigInt[uint64], // optional
epoch *uint64, // optional
) (*models.DevInspectResults, error) {
var resp models.DevInspectResults
return &resp, s.http.CallContext(ctx, &resp, devInspectTransactionBlock, senderAddress, txByte, gasPrice, epoch)
return &resp, s.http.CallContext(ctx, &resp, devInspectTransactionBlock, senderAddress, txKindBytes, gasPrice, epoch)
}

func (s *ImplSuiAPI) DryRunTransaction(
ctx context.Context,
txBytes sui_types.Base64Data,
txDataBytes sui_types.Base64Data,
) (*models.DryRunTransactionBlockResponse, error) {
var resp models.DryRunTransactionBlockResponse
return &resp, s.http.CallContext(ctx, &resp, dryRunTransactionBlock, txBytes)
return &resp, s.http.CallContext(ctx, &resp, dryRunTransactionBlock, txDataBytes)
}

func (s *ImplSuiAPI) ExecuteTransactionBlock(
ctx context.Context,
txBytes sui_types.Base64Data,
txDataBytes sui_types.Base64Data,
signatures []*sui_signer.Signature,
options *models.SuiTransactionBlockResponseOptions,
requestType models.ExecuteTransactionRequestType,
) (*models.SuiTransactionBlockResponse, error) {
resp := models.SuiTransactionBlockResponse{}
return &resp, s.http.CallContext(ctx, &resp, executeTransactionBlock, txBytes, signatures, options, requestType)
return &resp, s.http.CallContext(ctx, &resp, executeTransactionBlock, txDataBytes, signatures, options, requestType)
}
60 changes: 35 additions & 25 deletions sui/api_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,54 @@ import (
"github.com/howjmay/sui-go/sui/conn"
"github.com/howjmay/sui-go/sui_signer"
"github.com/howjmay/sui-go/sui_types"

"github.com/fardream/go-bcs/bcs"
"github.com/stretchr/testify/require"
)

func TestDevInspectTransactionBlock(t *testing.T) {
t.Skip("FIXME")
// api := sui.NewSuiClient(conn.DevnetEndpointUrl)
// signer := Address
// price, err := api.GetReferenceGasPrice(context.TODO())
// require.NoError(t, err)
// coins, err := api.GetCoins(context.Background(), signer, nil, nil, 10)
// require.NoError(t, err)

// amount := SUI(0.01).Int64()
// gasBudget := SUI(0.01).Uint64()
// pickedCoins, err := models.PickupCoins(coins, *big.NewInt(amount * 2), 0, false)
// require.NoError(t, err)
// tx, err := api.PayAllSui(context.Background(),
// signer, signer,
// pickedCoins.CoinIds(),
// models.NewSafeSuiBigInt(gasBudget))
// require.NoError(t, err)

// resp, err := api.DevInspectTransactionBlock(context.Background(), signer, tx.TxBytes, price, nil)
// require.NoError(t, err)
// t.Log(resp)
client, sender := sui.NewTestSuiClientWithSignerAndFund(conn.DevnetEndpointUrl, sui_signer.TEST_MNEMONIC)
coinType := models.SuiCoinType
limit := uint(3)
coinPages, err := client.GetCoins(context.Background(), sender.Address, &coinType, nil, limit)
require.NoError(t, err)
coins := models.Coins(coinPages.Data)

ptb := sui_types.NewProgrammableTransactionBuilder()
ptb.PayAllSui(sender.Address)
pt := ptb.Finish()
tx := sui_types.NewProgrammable(
sender.Address,
pt,
coins.CoinRefs(),
sui.DefaultGasBudget,
sui.DefaultGasPrice,
)
txBytes, err := bcs.Marshal(tx.V1.Kind)
require.NoError(t, err)

resp, err := client.DevInspectTransactionBlock(
context.Background(),
sender.Address,
txBytes,
nil,
nil,
)
require.NoError(t, err)
require.True(t, resp.Effects.Data.IsSuccess())
}

func TestDryRunTransaction(t *testing.T) {
api := sui.NewSuiClient(conn.TestnetEndpointUrl)
signer := sui_signer.TEST_ADDRESS
coins, err := api.GetCoins(context.Background(), signer, nil, nil, 10)
require.NoError(t, err)

amount := uint64(sui_types.UnitSui / 100)
pickedCoins, err := models.PickupCoins(coins, new(big.Int).SetUint64(amount), sui.DefaultGasBudget, 0, 0)
pickedCoins, err := models.PickupCoins(coins, big.NewInt(100), sui.DefaultGasBudget, 0, 0)
require.NoError(t, err)
tx, err := api.PayAllSui(
context.Background(), signer, signer,
context.Background(),
signer,
signer,
pickedCoins.CoinIds(),
models.NewSafeSuiBigInt(sui.DefaultGasBudget),
)
Expand Down
21 changes: 16 additions & 5 deletions sui_types/programmable_transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ func (p *ProgrammableTransactionBuilder) TransferArgs(recipient *SuiAddress, arg
}

func (p *ProgrammableTransactionBuilder) TransferObject(recipient *SuiAddress, objectRef *ObjectRef) error {
recArg := p.MustPure(recipient)
recArg, err := p.Pure(recipient)
if err != nil {
return fmt.Errorf("can't add recipient as arg: %w", err)
}
objArg, err := p.Obj(ObjectArg{ImmOrOwnedObject: objectRef})
if err != nil {
return err
Expand All @@ -297,8 +300,11 @@ func (p *ProgrammableTransactionBuilder) TransferObject(recipient *SuiAddress, o
return nil
}

func (p *ProgrammableTransactionBuilder) TransferSui(recipient *SuiAddress, amount *uint64) {
recArg := p.MustPure(recipient)
func (p *ProgrammableTransactionBuilder) TransferSui(recipient *SuiAddress, amount *uint64) error {
recArg, err := p.Pure(recipient)
if err != nil {
return fmt.Errorf("can't add recipient as arg: %w", err)
}
var coinArg Argument
if amount != nil {
amtArg := p.MustPure(amount)
Expand All @@ -317,17 +323,22 @@ func (p *ProgrammableTransactionBuilder) TransferSui(recipient *SuiAddress, amou
Address: recArg,
}},
)
return nil
}

// the gas coin is consumed as the coin to be paid
func (p *ProgrammableTransactionBuilder) PayAllSui(recipient *SuiAddress) {
recArg := p.MustPure(recipient)
func (p *ProgrammableTransactionBuilder) PayAllSui(recipient *SuiAddress) error {
recArg, err := p.Pure(recipient)
if err != nil {
return fmt.Errorf("can't add recipient as arg: %w", err)
}
p.Command(Command{
TransferObjects: &ProgrammableTransferObjects{
Objects: []Argument{{GasCoin: &serialization.EmptyEnum{}}},
Address: recArg,
}},
)
return nil
}

// the gas coin is consumed as the coin to be paid
Expand Down
13 changes: 6 additions & 7 deletions sui_types/programmable_transaction_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

func TestPTBMoveCall(t *testing.T) {
t.Skip("use other move contract for test")
client, sender := sui.NewTestSuiClientWithSignerAndFund(conn.DevnetEndpointUrl, sui_signer.TEST_MNEMONIC)
coinType := models.SuiCoinType
limit := uint(3)
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestPTBMoveCall(t *testing.T) {
require.NoError(t, err)
require.Empty(t, simulate1.Effects.Data.V1.Status.Error)
require.True(t, simulate1.Effects.Data.IsSuccess())
require.Equal(t, coins[0].CoinObjectID.String(), simulate1.Effects.Data.V1.GasObject.Reference.ObjectID)
require.Equal(t, coins[0].CoinObjectID, simulate1.Effects.Data.V1.GasObject.Reference.ObjectID)

// case 2: direct stake the specified coin
ptb2 := sui_types.NewProgrammableTransactionBuilder()
Expand Down Expand Up @@ -99,7 +98,7 @@ func TestPTBMoveCall(t *testing.T) {
require.NoError(t, err)
require.Empty(t, simulate2.Effects.Data.V1.Status.Error)
require.True(t, simulate2.Effects.Data.IsSuccess())
require.Equal(t, coins[0].CoinObjectID.String(), simulate2.Effects.Data.V1.GasObject.Reference.ObjectID)
require.Equal(t, coins[0].CoinObjectID, simulate2.Effects.Data.V1.GasObject.Reference.ObjectID)
}

func TestPTBTransferObject(t *testing.T) {
Expand Down Expand Up @@ -153,7 +152,7 @@ func TestPTBTransferSui(t *testing.T) {

// build with BCS
ptb := sui_types.NewProgrammableTransactionBuilder()
ptb.TransferSui(recipient.Address, &amount)
err = ptb.TransferSui(recipient.Address, &amount)
require.NoError(t, err)
pt := ptb.Finish()
tx := sui_types.NewProgrammable(
Expand Down Expand Up @@ -191,7 +190,7 @@ func TestPTBPayAllSui(t *testing.T) {

// build with BCS
ptb := sui_types.NewProgrammableTransactionBuilder()
ptb.PayAllSui(recipient.Address)
err = ptb.PayAllSui(recipient.Address)
require.NoError(t, err)
pt := ptb.Finish()
tx := sui_types.NewProgrammable(
Expand Down Expand Up @@ -250,7 +249,7 @@ func TestPTBPaySui(t *testing.T) {
require.NoError(t, err)
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
require.True(t, simulate.Effects.Data.IsSuccess())
require.Equal(t, coin.CoinObjectID, simulate.Effects.Data.V1.GasObject.Reference.ObjectID)
require.Equal(t, coin.CoinObjectID.String(), simulate.Effects.Data.V1.GasObject.Reference.ObjectID)

// 1 for Mutated, 2 created (the 2 transfer in pay_sui pt),
require.Len(t, simulate.ObjectChanges, 3)
Expand Down Expand Up @@ -316,7 +315,7 @@ func TestPTBPay(t *testing.T) {
require.NoError(t, err)
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
require.True(t, simulate.Effects.Data.IsSuccess())
require.Equal(t, gasCoin.CoinObjectID, simulate.Effects.Data.V1.GasObject.Reference.ObjectID)
require.Equal(t, gasCoin.CoinObjectID.String(), simulate.Effects.Data.V1.GasObject.Reference.ObjectID)

// 2 for Mutated (1 gas coin and 1 merged coin in pay pt), 2 created (the 2 transfer in pay pt),
require.Len(t, simulate.ObjectChanges, 5)
Expand Down

0 comments on commit 244be4a

Please sign in to comment.