Skip to content

Commit

Permalink
test: Add TestPublish
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed May 8, 2024
1 parent cfdb0e0 commit 7714aba
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 69 deletions.
4 changes: 4 additions & 0 deletions contracts/scripts/build_publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root_path=$(git rev-parse --show-toplevel)
cd $root_path/contracts/testcoin
sui move build
sui client publish --gas-budget 1000000000 --skip-dependency-verification --json > publish_receipt.json
4 changes: 4 additions & 0 deletions contracts/scripts/mint_coin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root_path=$(git rev-parse --show-toplevel)
cd $root_path/contracts/testcoin
sui move build
sui client publish --gas-budget 1000000000 --skip-dependency-verification --json > mint_receipt.json
37 changes: 37 additions & 0 deletions contracts/testcoin/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "testcoin"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
testcoin = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

29 changes: 29 additions & 0 deletions contracts/testcoin/sources/testcoin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module testcoin::testcoin {
use std::option;
use sui::coin::{Self, Coin, TreasuryCap};
use sui::transfer;
use sui::tx_context::{Self, TxContext};

/// The type identifier of coin. The coin will have a type
/// tag of kind: `Coin<package_object::testcoin::TESTCOIN>`
/// Make sure that the name of the type matches the module's name.
public struct TESTCOIN has drop {}

/// Module initializer is called once on module publish. A treasury
/// cap is sent to the publisher, who then controls minting and burning
fun init(witness: TESTCOIN, ctx: &mut TxContext) {
let (treasury, metadata) = coin::create_currency(witness, 6, b"TESTCOIN", b"", b"", option::none(), ctx);
transfer::public_freeze_object(metadata);
transfer::public_transfer(treasury, tx_context::sender(ctx))
}

public fun mint(
treasury_cap: &mut TreasuryCap<TESTCOIN>,
amount: u64,
recipient: address,
ctx: &mut TxContext,
) {
let coin = coin::mint(treasury_cap, amount, ctx);
transfer::public_transfer(coin, recipient)
}
}
19 changes: 19 additions & 0 deletions contracts/testcoin/tests/testcoin_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
#[test_only]
module testcoin::testcoin_tests {
// uncomment this line to import the module
// use testcoin::testcoin;
const ENotImplemented: u64 = 0;
#[test]
fun test_testcoin() {
// pass
}
#[test, expected_failure(abort_code = testcoin::testcoin_tests::ENotImplemented)]
fun test_testcoin_fail() {
abort ENotImplemented
}
}
*/
5 changes: 3 additions & 2 deletions sui/api_transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/howjmay/sui-go/models"
"github.com/howjmay/sui-go/sui_types"
"github.com/howjmay/sui-go/sui_types/serialization"
)

// TODO: execution_mode : <SuiTransactionBlockBuilderMode>
Expand Down Expand Up @@ -100,10 +101,10 @@ func (s *ImplSuiAPI) PaySui(
func (s *ImplSuiAPI) Publish(
ctx context.Context,
sender *sui_types.SuiAddress,
compiledModules []*suiBase64Data,
compiledModules []*serialization.Base64Data,
dependencies []sui_types.ObjectID,
gas *sui_types.ObjectID,
gasBudget uint,
gasBudget models.SafeSuiBigInt[uint64],
) (*models.TransactionBytes, error) {
var resp models.TransactionBytes
return &resp, s.http.CallContext(ctx, &resp, publish, sender, compiledModules, dependencies, gas, gasBudget)
Expand Down
79 changes: 22 additions & 57 deletions sui/api_transaction_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sui_test

import (
"context"
"fmt"
"math/big"
"testing"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/howjmay/sui-go/sui/conn"
"github.com/howjmay/sui-go/sui_signer"
"github.com/howjmay/sui-go/sui_types"
"github.com/howjmay/sui-go/utils"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -163,63 +165,26 @@ func TestPaySui(t *testing.T) {
}

func TestPublish(t *testing.T) {
// api := sui.NewSuiClient(conn.DevnetEndpointUrl)
// dmens, err := models.NewBase64Data(DmensDmensB64)
// require.NoError(t, err)
// profile, err := models.NewBase64Data(DmensProfileB64)
// require.NoError(t, err)
// coins, err := api.GetSuiCoinsOwnedByAddress(context.TODO(), *Address)
// require.NoError(t, err)
// coin, err := coins.PickCoinNoLess(30000)
// require.NoError(t, err)
//
// type args struct {
// ctx context.Context
// address models.Address
// compiledModules []*models.Base64Data
// gas models.ObjectID
// gasBudget uint
// }
//
// tests := []struct {
// name string
// client *client.Client
// args args
// want *models.TransactionBytes
// wantErr bool
// }{
//
// {
// name: "test for dmens publish",
// client: chain,
// args: args{
// ctx: context.TODO(),
// address: *Address,
// compiledModules: []*models.Base64Data{dmens, profile},
// gas: coin.CoinObjectID,
// gasBudget: 30000,
// },
// },
// }
//
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// got, err := tt.client.Publish(tt.args.ctx, tt.args.address, tt.args.compiledModules, tt.args.gas, tt.args.gasBudget)
// if (err != nil) != tt.wantErr {
// t.Errorf("Publish() error: %v, wantErr %v", err, tt.wantErr)
// return
// }
// t.Logf("%#v", got)
//
// txResult, err := tt.client.DryRunTransaction(context.TODO(), got)
// if (err != nil) != tt.wantErr {
// t.Errorf("Publish() error: %v, wantErr %v", err, tt.wantErr)
// return
// }
//
// t.Logf("%#v", txResult)
// })
// }
client := sui.NewSuiClient(conn.TestnetEndpointUrl)

modules, err := utils.MoveBuild(utils.GetGitRoot() + "/contracts/testcoin")
require.NoError(t, err)

dependencies := make([]sui_types.ObjectID, len(modules.Dependencies))
for i, v := range modules.Dependencies {
dependencies[i] = *sui_types.MustObjectIDFromHex(v)
}

coins, err := client.GetCoins(context.Background(), sui_signer.TEST_ADDRESS, nil, nil, 10)
require.NoError(t, err)
gasBudget := uint64(1000000)
pickedCoins, err := models.PickupCoins(coins, *big.NewInt(100000), gasBudget, 10, 10)
require.NoError(t, err)

txnBytes, err := client.Publish(context.Background(), sui_signer.TEST_ADDRESS, modules.Modules, dependencies, &pickedCoins.CoinIds()[0], models.NewSafeSuiBigInt(gasBudget))
require.NoError(t, err)

fmt.Println("txnBytes: ", txnBytes)
}

func TestSplitCoin(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions sui/api_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

"github.com/howjmay/sui-go/models"
"github.com/howjmay/sui-go/sui_types"
"github.com/howjmay/sui-go/sui_types/serialization"
)

func (s *ImplSuiAPI) DevInspectTransactionBlock(
ctx context.Context,
senderAddress *sui_types.SuiAddress,
txByte suiBase64Data,
txByte serialization.Base64Data,
gasPrice *models.SafeSuiBigInt[uint64],
epoch *uint64,
) (*models.DevInspectResults, error) {
Expand All @@ -20,14 +21,14 @@ func (s *ImplSuiAPI) DevInspectTransactionBlock(

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

func (s *ImplSuiAPI) ExecuteTransactionBlock(
ctx context.Context, txBytes suiBase64Data, signatures []any,
ctx context.Context, txBytes serialization.Base64Data, signatures []any,
options *models.SuiTransactionBlockResponseOptions, requestType models.ExecuteTransactionRequestType,
) (*models.SuiTransactionBlockResponse, error) {
resp := models.SuiTransactionBlockResponse{}
Expand Down
2 changes: 1 addition & 1 deletion sui/extend_ptb_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// NOTE: This a copy the query limit from our Rust JSON RPC backend, this needs to be kept in sync!
const QUERY_MAX_RESULT_LIMIT = 50

type suiBase64Data = serialization.Base64Data
// type suiBase64Data = serialization.Base64Data

// GetSuiCoinsOwnedByAddress This function will retrieve a maximum of 200 coins.
func (s *ImplSuiAPI) GetSuiCoinsOwnedByAddress(ctx context.Context, address *sui_types.SuiAddress) (models.Coins, error) {
Expand Down
16 changes: 11 additions & 5 deletions utils/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@ package utils

import (
"encoding/json"
"fmt"
"log"
"os/exec"
"strings"

"github.com/howjmay/sui-go/sui_types/serialization"
)

type CompiledMoveModules struct {
Modules []string `json:"modules"`
Dependencies []string `json:"dependencies"`
Digest []int `json:"digest"`
Modules []*serialization.Base64Data `json:"modules"`
Dependencies []string `json:"dependencies"`
Digest []int `json:"digest"`
}

func MoveBuild(path string) (*CompiledMoveModules, error) {
var err error
// Setup the command to be executed
cmd := exec.Command("sui", "move", "build", "--dump-bytecode-as-base64")
cmd.Dir = path
fmt.Println("path ", path)

// Run the command and capture the output
output, err := cmd.Output()
if err != nil {
return nil, err
// return nil, err
panic(err)
}

var modules CompiledMoveModules
err = json.Unmarshal(output, &modules)
if err != nil {
return nil, err
// return nil, err
panic(err)
}

return &modules, nil
Expand Down
2 changes: 1 addition & 1 deletion utils/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestMoveBuild(t *testing.T) {
t.Skip("FIXME install sui for ci to test")
// FIXME add a testing contract for the localnet
modules, err := utils.MoveBuild(utils.GetGitRoot() + "/isc/contracts/isc")
modules, err := utils.MoveBuild(utils.GetGitRoot() + "/contracts/testcoin/")
require.NoError(t, err)
fmt.Println("modules", modules)
}

0 comments on commit 7714aba

Please sign in to comment.