-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef78189
commit 68ee650
Showing
34 changed files
with
2,331 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.