Skip to content

Commit ac828bb

Browse files
committed
test: adding test for claim logic
1 parent 2a6b6f4 commit ac828bb

File tree

11 files changed

+124
-28
lines changed

11 files changed

+124
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Pactus Bot Engine (RoboPac)
66

7-
Pactus Bor Engine is a Robot that provides support and information about the Pactus Blockchain.
7+
The Pactus Bot Engine is a Robot that provides support and information about the Pactus Blockchain.
88

99

1010
## Contributing

client/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ func (c *Client) GetNodeInfo() (*pactus.GetNodeInfoResponse, error) {
149149
return info, err
150150
}
151151

152+
func (c *Client) GetTransactionData(txID string) (*pactus.GetTransactionResponse, error) {
153+
return c.transactionClient.GetTransaction(context.Background(), &pactus.GetTransactionRequest{
154+
Id: []byte(txID),
155+
Verbosity: pactus.TransactionVerbosity_TRANSACTION_DATA,
156+
})
157+
}
158+
152159
func (c *Client) Close() error {
153160
return c.conn.Close()
154161
}

client/client_mgr.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ func (cm *Mgr) GetValidatorInfoByNumber(num int32) (*pactus.GetValidatorResponse
179179
return nil, errors.New("unable to get validator info")
180180
}
181181

182+
func (cm *Mgr) GetTransactionData(txID string) (*pactus.GetTransactionResponse, error) {
183+
for _, c := range cm.clients {
184+
txData, err := c.GetTransactionData(txID)
185+
if err != nil {
186+
continue
187+
}
188+
return txData, nil
189+
}
190+
191+
return nil, errors.New("unable to get transaction data")
192+
}
193+
182194
func (cm *Mgr) Close() {
183195
for addr, c := range cm.clients {
184196
if err := c.Close(); err != nil {

client/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ type IClient interface {
1212
IsValidator(string) (bool, error)
1313
GetValidatorInfo(string) (*pactus.GetValidatorResponse, error)
1414
GetValidatorInfoByNumber(int32) (*pactus.GetValidatorResponse, error)
15+
GetTransactionData(string) (*pactus.GetTransactionResponse, error)
1516
Close() error
1617
}

client/mock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/engine.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/kehiy/RoboPac/utils"
1313
"github.com/kehiy/RoboPac/wallet"
1414
"github.com/libp2p/go-libp2p/core/peer"
15+
"github.com/pactus-project/pactus/util"
1516
)
1617

1718
type BotEngine struct {
@@ -179,7 +180,12 @@ func (be *BotEngine) Claim(tokens []string) (*store.ClaimTransaction, error) {
179180
return nil, errors.New("can't send bond transaction")
180181
}
181182

182-
err = be.Store.AddClaimTransaction(txID, claimer.TotalReward, time.Now(), "", discordID)
183+
txData, err := be.Cm.GetTransactionData(txID)
184+
if err != nil {
185+
return nil, err
186+
}
187+
188+
err = be.Store.AddClaimTransaction(txID, util.ChangeToCoin(txData.Transaction.Value), int64(txData.BlockTime), discordID)
183189
if err != nil {
184190
return nil, err
185191
}

engine/engine_test.go

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package engine_test
22

33
import (
4+
"fmt"
45
"testing"
56
"time"
67

78
"github.com/kehiy/RoboPac/client"
89
"github.com/kehiy/RoboPac/engine"
910
"github.com/kehiy/RoboPac/log"
10-
"github.com/kehiy/RoboPac/store"
11+
rpstore "github.com/kehiy/RoboPac/store"
1112
"github.com/kehiy/RoboPac/wallet"
1213
"github.com/libp2p/go-libp2p/core/peer"
14+
"github.com/pactus-project/pactus/util"
1315
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
1416
"github.com/stretchr/testify/assert"
1517
"go.uber.org/mock/gomock"
1618
)
1719

18-
func setup(t *testing.T) (engine.Engine, client.MockIClient, error) {
20+
func setup(t *testing.T) (engine.Engine, client.MockIClient, rpstore.MockIStore, wallet.MockIWallet, error) {
1921
t.Helper()
2022
ctrl := gomock.NewController(t)
2123

@@ -30,14 +32,14 @@ func setup(t *testing.T) (engine.Engine, client.MockIClient, error) {
3032
wallet := wallet.NewMockIWallet(ctrl)
3133

3234
// mocking store.
33-
store := store.NewMockIStore(ctrl)
35+
store := rpstore.NewMockIStore(ctrl)
3436

3537
eng, err := engine.NewBotEngine(sl, cm, wallet, store)
36-
return eng, *mockClient, err
38+
return eng, *mockClient, *store, *wallet, err
3739
}
3840

3941
func TestNetworkStatus(t *testing.T) {
40-
eng, client, err := setup(t)
42+
eng, client, _, _, err := setup(t)
4143
assert.NoError(t, err)
4244

4345
client.EXPECT().GetNetworkInfo().Return(
@@ -62,7 +64,7 @@ func TestNetworkStatus(t *testing.T) {
6264
}
6365

6466
func TestNetworkHealth(t *testing.T) {
65-
eng, client, err := setup(t)
67+
eng, client, _, _, err := setup(t)
6668
assert.NoError(t, err)
6769

6870
t.Run("should be healthy", func(t *testing.T) {
@@ -93,7 +95,7 @@ func TestNetworkHealth(t *testing.T) {
9395
}
9496

9597
func TestNodeInfo(t *testing.T) {
96-
eng, client, err := setup(t)
98+
eng, client, _, _, err := setup(t)
9799
assert.NoError(t, err)
98100

99101
t.Run("should return error, invalid input", func(t *testing.T) {
@@ -146,3 +148,64 @@ func TestNodeInfo(t *testing.T) {
146148
assert.Equal(t, float64(0.9), info.AvailabilityScore)
147149
})
148150
}
151+
152+
func TestClaim(t *testing.T) {
153+
eng, client, store, wallet, err := setup(t)
154+
assert.NoError(t, err)
155+
156+
t.Run("everything normal and good", func(t *testing.T) {
157+
valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp"
158+
discordID := "123456789"
159+
txID := "0x123456789"
160+
amount := 74.68
161+
time := time.Now().Unix()
162+
163+
client.EXPECT().IsValidator(valAddress).Return(
164+
true, nil,
165+
)
166+
167+
store.EXPECT().ClaimerInfo(discordID).Return(
168+
&rpstore.Claimer{
169+
DiscordID: discordID,
170+
TotalReward: amount,
171+
ClaimTransaction: nil,
172+
},
173+
)
174+
175+
memo := fmt.Sprintf("RP to: %v", discordID)
176+
wallet.EXPECT().BondTransaction("", valAddress, memo, amount).Return(
177+
txID, nil,
178+
)
179+
180+
client.EXPECT().GetTransactionData(txID).Return(
181+
&pactus.GetTransactionResponse{
182+
BlockTime: uint32(time),
183+
Transaction: &pactus.TransactionInfo{
184+
Id: []byte(txID),
185+
Value: util.CoinToChange(amount),
186+
Memo: memo,
187+
},
188+
}, nil,
189+
)
190+
191+
store.EXPECT().AddClaimTransaction(txID, amount, time, discordID).Return(
192+
nil,
193+
)
194+
195+
store.EXPECT().ClaimerInfo(discordID).Return(
196+
&rpstore.Claimer{
197+
DiscordID: discordID,
198+
TotalReward: amount,
199+
ClaimTransaction: &rpstore.ClaimTransaction{
200+
TxID: txID,
201+
Amount: amount,
202+
Time: time,
203+
},
204+
},
205+
).AnyTimes()
206+
207+
claimTx, err := eng.Claim([]string{valAddress, discordID})
208+
assert.NoError(t, err)
209+
assert.NotNil(t, claimTx)
210+
})
211+
}

store/interface.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package store
22

3-
import "time"
4-
53
type ClaimTransaction struct {
64
TxID string `json:"transaction_id"`
7-
Data string `json:"data"`
85
Amount float64 `json:"amount"`
96
Time int64 `json:"time"`
107
}
@@ -21,5 +18,5 @@ func (c *Claimer) IsClaimed() bool {
2118

2219
type IStore interface {
2320
ClaimerInfo(discordID string) *Claimer
24-
AddClaimTransaction(TxID string, Amount float64, Time time.Time, Data string, discordID string) error
21+
AddClaimTransaction(TxID string, Amount float64, Time int64, discordID string) error
2522
}

store/mock.go

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

store/store.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"sync"
8-
"time"
98

109
"github.com/kehiy/RoboPac/config"
1110
"github.com/kehiy/RoboPac/log"
@@ -54,32 +53,31 @@ func (s *Store) ClaimerInfo(discordID string) *Claimer {
5453
return claimerInfo
5554
}
5655

57-
func (s *Store) AddClaimTransaction(txID string, amount float64, time time.Time, txData string, discordID string) error {
56+
func (s *Store) AddClaimTransaction(txID string, amount float64, time int64, discordID string) error {
5857
s.syncMap.Store(discordID, &Claimer{
5958
DiscordID: discordID,
6059
ClaimTransaction: &ClaimTransaction{
6160
TxID: txID,
6261
Amount: amount,
63-
Time: time.Unix(),
64-
Data: txData,
62+
Time: time,
6563
},
6664
})
6765

6866
s.logger.Info("new claim transaction added", "discordID", discordID, "amount",
69-
amount, "data", txData, "time", time.Unix(), "txID", txID)
67+
amount, "time", time, "txID", txID)
7068

7169
// save record.
7270
data, err := marshalJSON(s.syncMap)
7371
if err != nil {
7472
s.logger.Panic("can't marshal json new claim transaction", "discordID", discordID, "amount",
75-
amount, "data", txData, "time", time.Unix(), "txID", txID, "err", err)
73+
amount, "time", time, "txID", txID, "err", err)
7674

7775
return fmt.Errorf("error marshalling validator data file: %w", err)
7876
}
7977

8078
if err := os.WriteFile(s.cfg.StorePath, data, 0o600); err != nil {
8179
s.logger.Panic("can't write new claim transaction", "discordID", discordID, "amount",
82-
amount, "data", txData, "time", time.Unix(), "txID", txID, "err", err)
80+
amount, "time", time, "txID", txID, "err", err)
8381

8482
return fmt.Errorf("failed to write to %s: %w", s.cfg.StorePath, err)
8583
}

store/store_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,17 @@ func TestStore(t *testing.T) {
4343
t.Run("test add claim transaction", func(t *testing.T) {
4444
txID := "0x123456789"
4545
time := time.Now()
46-
data := "data"
4746
discordID := "123456789"
4847

4948
claimer := store.ClaimerInfo(discordID)
5049

5150
isClaimed := claimer.IsClaimed()
5251
assert.False(t, isClaimed)
5352

54-
err := store.AddClaimTransaction(txID, claimer.TotalReward, time, data, discordID)
53+
err := store.AddClaimTransaction(txID, claimer.TotalReward, time.Unix(), discordID)
5554
assert.NoError(t, err)
5655

5756
claimedInfo := store.ClaimerInfo(discordID)
58-
assert.Equal(t, data, claimedInfo.ClaimTransaction.Data)
5957
assert.Equal(t, discordID, claimedInfo.DiscordID)
6058
assert.Equal(t, float64(100), claimedInfo.ClaimTransaction.Amount)
6159
assert.Equal(t, txID, claimedInfo.ClaimTransaction.TxID)

0 commit comments

Comments
 (0)