Skip to content

Commit

Permalink
add module-sdk-go test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicke-lucky committed Jun 30, 2021
1 parent ef78189 commit 68ee650
Show file tree
Hide file tree
Showing 34 changed files with 2,331 additions and 20 deletions.
20 changes: 10 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
txtypes "github.com/irisnet/irishub-sdk-go/types/tx"
)

type IRISHUBClient struct {
type Client struct {
logger log.Logger
moduleManager map[string]types.Module
encodingConfig types.EncodingConfig
Expand All @@ -46,7 +46,7 @@ type IRISHUBClient struct {
Swap coinswap.Client
}

func NewIRISHUBClient(cfg types.ClientConfig) IRISHUBClient {
func NewIRISHUBClient(cfg types.ClientConfig) Client {
encodingConfig := makeEncodingConfig()

// create a instance of baseClient
Expand All @@ -66,7 +66,7 @@ func NewIRISHUBClient(cfg types.ClientConfig) IRISHUBClient {
htlcClient := htlc.NewClient(baseClient, encodingConfig.Marshaler)
swapClient := coinswap.NewClient(baseClient, encodingConfig.Marshaler, bankClient.TotalSupply)

client := &IRISHUBClient{
client := &Client{
logger: baseClient.Logger(),
BaseClient: baseClient,
moduleManager: make(map[string]types.Module),
Expand Down Expand Up @@ -101,27 +101,27 @@ func NewIRISHUBClient(cfg types.ClientConfig) IRISHUBClient {
return *client
}

func (client *IRISHUBClient) SetLogger(logger log.Logger) {
func (client *Client) SetLogger(logger log.Logger) {
client.BaseClient.SetLogger(logger)
}

func (client *IRISHUBClient) Codec() *codec.LegacyAmino {
func (client *Client) Codec() *codec.LegacyAmino {
return client.encodingConfig.Amino
}

func (client *IRISHUBClient) AppCodec() codec.Marshaler {
func (client *Client) AppCodec() codec.Marshaler {
return client.encodingConfig.Marshaler
}

func (client *IRISHUBClient) EncodingConfig() types.EncodingConfig {
func (client *Client) EncodingConfig() types.EncodingConfig {
return client.encodingConfig
}

func (client *IRISHUBClient) Manager() types.BaseClient {
func (client *Client) Manager() types.BaseClient {
return client.BaseClient
}

func (client *IRISHUBClient) RegisterModule(ms ...types.Module) {
func (client *Client) RegisterModule(ms ...types.Module) {
for _, m := range ms {
_, ok := client.moduleManager[m.Name()]
if ok {
Expand All @@ -134,7 +134,7 @@ func (client *IRISHUBClient) RegisterModule(ms ...types.Module) {
}
}

func (client *IRISHUBClient) Module(name string) types.Module {
func (client *Client) Module(name string) types.Module {
return client.moduleManager[name]
}

Expand Down
2 changes: 1 addition & 1 deletion module-sdk/coinswap/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module coinswap
module github.com/irisnet/module-sdk-go

go 1.16

Expand Down
2 changes: 1 addition & 1 deletion module-sdk/gov/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module gov
module github.com/irisnet/module-sdk-go

go 1.16

Expand Down
2 changes: 1 addition & 1 deletion module-sdk/htlc/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module htlc
module github.com/irisnet/module-sdk-go

go 1.16

Expand Down
64 changes: 64 additions & 0 deletions module-sdk/integration_test/coinswap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package integration_test

import (
"time"

"github.com/stretchr/testify/require"

"github.com/irisnet/irishub-sdk-go/modules/coinswap"
"github.com/irisnet/irishub-sdk-go/modules/token"
sdk "github.com/irisnet/irishub-sdk-go/types"
)

func (s IntegrationTestSuite) TestCoinSwap() {
baseTx := sdk.BaseTx{
From: s.Account().Name,
Gas: 200000,
Memo: "test",
Mode: sdk.Commit,
Password: s.Account().Password,
}

issueTokenReq := token.IssueTokenRequest{
Symbol: "bnb",
Name: s.RandStringOfLength(8),
Scale: 6,
MinUnit: "ubnb",
InitialSupply: 10000000,
MaxSupply: 21000000,
Mintable: true,
}

result, er := s.Token.IssueToken(issueTokenReq, baseTx)
require.NoError(s.T(), er)
require.NotEmpty(s.T(), result.Hash)

request := coinswap.AddLiquidityRequest{
MaxToken: sdk.Coin{
Denom: "ubnb",
Amount: sdk.NewInt(1000_000_000),
},
BaseAmt: sdk.NewInt(1000_000_000),
MinLiquidity: sdk.NewInt(1000_000_000),
Deadline: time.Now().Add(time.Hour).Unix(),
}

res, err := s.Swap.AddLiquidity(request, baseTx)
require.NoError(s.T(), err)
require.True(s.T(), res.Liquidity.GTE(request.MinLiquidity))
require.NotEmpty(s.T(), res.TxHash)
require.True(s.T(), request.MaxToken.Amount.GTE(res.TokenAmt))

boughtCoin := sdk.NewCoin("uiris", sdk.NewInt(100))
deadline := time.Now().Add(10 * time.Second).Unix()
resp, err := s.Swap.BuyTokenWithAutoEstimate("ubnb", boughtCoin, deadline, baseTx)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), resp.TxHash)
require.True(s.T(), resp.InputAmt.Equal(sdk.NewInt(101)))

soldCoin := sdk.NewCoin("uiris", sdk.NewInt(100))
resp, err = s.Swap.SellTokenWithAutoEstimate("ubnb", soldCoin, deadline, baseTx)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), resp.TxHash)
require.True(s.T(), resp.OutputAmt.Equal(sdk.NewInt(99)))
}
131 changes: 131 additions & 0 deletions module-sdk/integration_test/gov_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package integration_test

import (
"encoding/json"
"fmt"

"github.com/stretchr/testify/require"

"github.com/irisnet/irishub-sdk-go/modules/gov"
"github.com/irisnet/irishub-sdk-go/types"
)

func (s IntegrationTestSuite) TestGov() {
cases := []SubTest{
{
"TestGov",
testGov,
},

{
"TestParams",
testParams,
},
}

for _, t := range cases {
s.Run(t.testName, func() {
t.testCase(s)
})
}

}

func testGov(s IntegrationTestSuite) {
baseTx := types.BaseTx{
From: s.Account().Name,
Gas: 200000,
Memo: "TEST",
Mode: types.Commit,
Password: s.Account().Password,
}

// send submitProposal tx
submitProposalReq := gov.SubmitProposalRequest{
Title: s.RandStringOfLength(4),
Description: s.RandStringOfLength(6),
Type: "Text",
}
proposalId, res, err := s.Gov.SubmitProposal(submitProposalReq, baseTx)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), res.Hash)

// query proposal details based on ProposalID.
proposal, err := s.Gov.QueryProposal(proposalId)
require.NoError(s.T(), err)
require.Equal(s.T(), proposal.ProposalId, proposalId)
require.Equal(s.T(), "PROPOSAL_STATUS_DEPOSIT_PERIOD", proposal.Status)

// query all proposals based on given status.
proposalStatus := proposal.Status
proposals, err := s.Gov.QueryProposals(proposalStatus)
var exists bool
require.NoError(s.T(), err)
require.NotEmpty(s.T(), proposals)
for _, proposal := range proposals {
if proposal.ProposalId == proposalId {
exists = true
}
}
require.True(s.T(), exists)

// send Deposit tx
amount, e := types.ParseDecCoins("2000iris")
require.NoError(s.T(), e)
depositReq := gov.DepositRequest{
ProposalId: proposalId,
Amount: amount,
}
res, err = s.Gov.Deposit(depositReq, baseTx)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), res.Hash)

// query single deposit information based proposalID, depositAddr.
depositor := s.Account().Address.String()
deposit, err := s.Gov.QueryDeposit(proposalId, depositor)
require.NoError(s.T(), err)
require.Equal(s.T(), "2000000000uiris", deposit.Amount.String())

// query all deposits of a single proposal.
deposits, err := s.Gov.QueryDeposits(proposalId)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), deposits)
for _, deposit := range deposits {
require.Equal(s.T(), proposalId, deposit.ProposalId)
}

// send vote tx
voteReq := gov.VoteRequest{
ProposalId: proposalId,
Option: "VOTE_OPTION_YES",
}
res, err = s.Gov.Vote(voteReq, baseTx)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), res.Hash)

// query voted information based on proposalID, voterAddr.
voter := s.Account().Address.String()
vote, err := s.Gov.QueryVote(proposalId, voter)
require.NoError(s.T(), err)
require.Equal(s.T(), proposalId, vote.ProposalId)

// query votes of a given proposal.
votes, err := s.Gov.QueryVotes(proposalId)
require.NoError(s.T(), err)
require.Greater(s.T(), len(votes), 0)

// query the tally of a proposal vote.
_, err = s.Gov.QueryTallyResult(proposalId)
require.NoError(s.T(), err)
}

func testParams(s IntegrationTestSuite) {
paramsTypes := []string{"voting", "tallying", "deposit"}
for _, paramType := range paramsTypes {
res, err := s.Gov.QueryParams(paramType)
require.NoError(s.T(), err)
bz, e := json.Marshal(res)
require.NoError(s.T(), e)
fmt.Println(string(bz))
}
}
77 changes: 77 additions & 0 deletions module-sdk/integration_test/htlc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package integration_test

import (
"encoding/hex"
"encoding/json"
"fmt"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/tmhash"

"github.com/irisnet/irishub-sdk-go/modules/htlc"
sdk "github.com/irisnet/irishub-sdk-go/types"
)

func (s IntegrationTestSuite) TestHTLC() {
baseTx := sdk.BaseTx{
From: s.Account().Name,
Gas: 200000,
Memo: "test",
Mode: sdk.Commit,
Password: s.Account().Password,
}

amount, err := sdk.ParseDecCoins("10iris")
require.NoError(s.T(), err)
secret := s.GetSecret()
hashLock := s.GetHashLock(secret, 0)
fmt.Println("hashLock: " + hashLock)
fmt.Println("secret: " + secret)
receiverOnOtherChain := "0x" + s.RandStringOfLength(14)

to := s.GetRandAccount().Address
createHTLCRequest := htlc.CreateHTLCRequest{
To: to.String(),
ReceiverOnOtherChain: receiverOnOtherChain,
Amount: amount,
HashLock: hashLock,
}
res, err := s.HTLC.CreateHTLC(createHTLCRequest, baseTx)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), res.Hash)

hashLockBytes, _ := hex.DecodeString(hashLock)
minCoins, _ := s.ToMinCoin(amount...)
htlcId := hex.EncodeToString(tmhash.Sum(append(append(append(hashLockBytes, s.Account().Address...), to...), []byte(minCoins.Sort().String())...)))

queryHTLCResp, err := s.HTLC.QueryHTLC(htlcId)
require.NoError(s.T(), err)
require.Equal(s.T(), receiverOnOtherChain, queryHTLCResp.ReceiverOnOtherChain)

res, err = s.HTLC.ClaimHTLC(htlcId, secret, baseTx)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), res.Hash)
}

// GetHashLock calculates the hash lock from the given secret and timestamp
func (s IntegrationTestSuite) GetHashLock(secret string, timestamp uint64) string {
secretBz, _ := hex.DecodeString(secret)
if timestamp > 0 {
return string(tmhash.Sum(append(secretBz, sdk.Uint64ToBigEndian(timestamp)...)))
}
sum := tmhash.Sum(secretBz)
return hex.EncodeToString(sum)
}

func (s IntegrationTestSuite) GetSecret() string {
random := s.RandStringOfLength(10)
sum := tmhash.Sum([]byte(random))
return hex.EncodeToString(sum)
}

func (s IntegrationTestSuite) TestQueryParams() {
res, err := s.HTLC.QueryParams()
require.NoError(s.T(), err)
data, _ := json.Marshal(res)
fmt.Println(string(data))
}
Loading

0 comments on commit 68ee650

Please sign in to comment.