1
1
package engine
2
2
3
3
import (
4
- "fmt"
5
4
"testing"
6
5
"time"
7
6
@@ -10,7 +9,6 @@ import (
10
9
rpstore "github.com/kehiy/RoboPac/store"
11
10
"github.com/kehiy/RoboPac/wallet"
12
11
"github.com/libp2p/go-libp2p/core/peer"
13
- "github.com/pactus-project/pactus/util"
14
12
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
15
13
"github.com/stretchr/testify/assert"
16
14
"go.uber.org/mock/gomock"
@@ -150,138 +148,128 @@ func TestClaim(t *testing.T) {
150
148
eng , client , store , wallet := setup (t )
151
149
152
150
t .Run ("everything normal and good" , func (t * testing.T ) {
153
- valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp "
154
- testNetValAddr := "tpc1pqn7uaeduklpg00rqt6uq0m9wy5txnyt0kmxmgf "
151
+ mainnetAddr := "mainnet-addr "
152
+ testnetAddr := "testnet-addr "
155
153
discordID := "123456789"
156
- txID := "0x123456789"
157
- amount := float64 (74 )
158
- time := time .Now ().Unix ()
154
+ amount := int64 (74 )
155
+ txID := "tx-id"
159
156
160
- client .EXPECT ().IsValidator (valAddress ).Return (
157
+ client .EXPECT ().IsValidator (mainnetAddr ).Return (
161
158
true , nil ,
162
- )
159
+ ). AnyTimes ()
163
160
164
- store .EXPECT ().ClaimerInfo (testNetValAddr ).Return (
161
+ store .EXPECT ().ClaimerInfo (testnetAddr ).Return (
165
162
& rpstore.Claimer {
166
- DiscordID : discordID ,
167
- TotalReward : amount ,
168
- ClaimTransaction : nil ,
163
+ DiscordID : discordID ,
164
+ TotalReward : amount ,
165
+ ClaimedTxID : "" ,
169
166
},
170
167
)
171
168
172
- memo := fmt .Sprintf ("RP to: %v" , discordID )
173
- wallet .EXPECT ().BondTransaction ("" , valAddress , memo , amount ).Return (
174
- txID , nil ,
175
- )
176
-
177
- client .EXPECT ().GetTransactionData (txID ).Return (
178
- & pactus.GetTransactionResponse {
179
- BlockTime : uint32 (time ),
180
- Transaction : & pactus.TransactionInfo {
181
- Id : []byte (txID ),
182
- Value : util .CoinToChange (amount ),
183
- Memo : memo ,
169
+ client .EXPECT ().GetNetworkInfo ().Return (
170
+ & pactus.GetNetworkInfoResponse {
171
+ ConnectedPeers : []* pactus.PeerInfo {
172
+ {
173
+ ConsensusAddress : []string {mainnetAddr },
174
+ ConsensusKeys : []string {"public-key-1" },
175
+ },
184
176
},
185
177
}, nil ,
178
+ ).AnyTimes ()
179
+
180
+ wallet .EXPECT ().BondTransaction ("public-key-1" , mainnetAddr , "" , amount ).Return (
181
+ txID , nil ,
186
182
)
187
183
188
- store .EXPECT ().AddClaimTransaction (amount , time , txID , discordID , testNetValAddr ).Return (
184
+ store .EXPECT ().AddClaimTransaction (testnetAddr , txID ).Return (
189
185
nil ,
190
186
)
191
187
192
- store .EXPECT ().ClaimerInfo (testNetValAddr ).Return (
193
- & rpstore.Claimer {
194
- DiscordID : discordID ,
195
- TotalReward : amount ,
196
- ClaimTransaction : & rpstore.ClaimTransaction {
197
- TxID : txID ,
198
- Amount : amount ,
199
- Time : time ,
200
- },
201
- },
202
- ).AnyTimes ()
203
-
204
- claimTx , err := eng .Claim ([]string {valAddress , testNetValAddr , discordID })
188
+ expectedTx , err := eng .Claim (discordID , testnetAddr , mainnetAddr )
205
189
assert .NoError (t , err )
206
- assert .NotNil (t , claimTx )
207
-
208
- assert .Equal (t , amount , claimTx .Amount )
209
- assert .Equal (t , txID , claimTx .TxID )
210
- assert .Equal (t , time , claimTx .Time )
190
+ assert .NotNil (t , expectedTx , txID )
211
191
212
192
//! can't claim twice.
213
- claimTx , err = eng .Claim ([]string {valAddress , testNetValAddr , discordID })
214
- assert .EqualError (t , err , "this claimer have already claimed rewards" )
215
- assert .Nil (t , claimTx )
216
- })
217
-
218
- t .Run ("missing arguments" , func (t * testing.T ) {
219
- claimTx , err := eng .Claim ()
220
- assert .EqualError (t , err , "missing argument: validator address" )
221
- assert .Nil (t , claimTx )
222
- })
223
-
224
- t .Run ("claimer not found" , func (t * testing.T ) {
225
- valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp"
226
- testNetValAddr := "tpc1peaeyzmwjqu6nz93c27hr8ad2l265tx4s9v6zhw"
227
- discordID := "987654321"
228
193
229
- store .EXPECT ().ClaimerInfo (testNetValAddr ).Return (
230
- nil ,
231
- )
232
-
233
- claimTx , err := eng .Claim ([]string {valAddress , testNetValAddr , discordID })
234
- assert .EqualError (t , err , "claimer not found" )
235
- assert .Nil (t , claimTx )
236
- })
237
-
238
- t .Run ("not validator address" , func (t * testing.T ) {
239
- valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp"
240
- testNetValAddr := "tpc1p2vx5t8sglhvncmp3en0qhgtxyc59w0gfgnaqe7"
241
- discordID := "1234567890"
242
- amount := 74.68
243
-
244
- store .EXPECT ().ClaimerInfo (testNetValAddr ).Return (
194
+ store .EXPECT ().ClaimerInfo (testnetAddr ).Return (
245
195
& rpstore.Claimer {
246
196
DiscordID : discordID ,
247
197
TotalReward : amount ,
198
+ ClaimedTxID : txID ,
248
199
},
249
200
)
250
-
251
- client .EXPECT ().IsValidator (valAddress ).Return (
252
- false , nil ,
253
- )
254
-
255
- claimTx , err := eng .Claim ([]string {valAddress , testNetValAddr , discordID })
256
- assert .EqualError (t , err , "invalid argument: validator address" )
257
- assert .Nil (t , claimTx )
201
+ expectedTx , err = eng .Claim (discordID , testnetAddr , mainnetAddr )
202
+ assert .Error (t , err )
203
+ assert .Empty (t , expectedTx )
258
204
})
259
205
260
- t .Run ("empty transaction ID" , func (t * testing.T ) {
261
- valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp"
262
- testNetValAddr := "tpc1pvmundkkp83u5cfz04sem5r7688dc0lef5u0mmv"
263
- discordID := "1234567890"
264
- amount := 74.68
265
-
266
- client .EXPECT ().IsValidator (valAddress ).Return (
267
- true , nil ,
268
- )
269
-
270
- store .EXPECT ().ClaimerInfo (testNetValAddr ).Return (
271
- & rpstore.Claimer {
272
- DiscordID : discordID ,
273
- TotalReward : amount ,
274
- ClaimTransaction : nil ,
275
- },
276
- )
277
-
278
- memo := fmt .Sprintf ("RP to: %v" , discordID )
279
- wallet .EXPECT ().BondTransaction ("" , valAddress , memo , amount ).Return (
280
- "" , nil ,
281
- )
282
-
283
- claimTx , err := eng .Claim ([]string {valAddress , testNetValAddr , discordID })
284
- assert .EqualError (t , err , "can't send bond transaction" )
285
- assert .Nil (t , claimTx )
286
- })
206
+ // t.Run("missing arguments", func(t *testing.T) {
207
+ // claimTx, err := eng.Claim()
208
+ // assert.EqualError(t, err, "missing argument: validator address")
209
+ // assert.Nil(t, claimTx)
210
+ // })
211
+
212
+ // t.Run("claimer not found", func(t *testing.T) {
213
+ // valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp"
214
+ // testNetValAddr := "tpc1peaeyzmwjqu6nz93c27hr8ad2l265tx4s9v6zhw"
215
+ // discordID := "987654321"
216
+
217
+ // store.EXPECT().ClaimerInfo(testNetValAddr).Return(
218
+ // nil,
219
+ // )
220
+
221
+ // claimTx, err := eng.Claim([]string{valAddress, testNetValAddr, discordID})
222
+ // assert.EqualError(t, err, "claimer not found")
223
+ // assert.Nil(t, claimTx)
224
+ // })
225
+
226
+ // t.Run("not validator address", func(t *testing.T) {
227
+ // valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp"
228
+ // testNetValAddr := "tpc1p2vx5t8sglhvncmp3en0qhgtxyc59w0gfgnaqe7"
229
+ // discordID := "1234567890"
230
+ // amount := 74.68
231
+
232
+ // store.EXPECT().ClaimerInfo(testNetValAddr).Return(
233
+ // &rpstore.Claimer{
234
+ // DiscordID: discordID,
235
+ // TotalReward: amount,
236
+ // },
237
+ // )
238
+
239
+ // client.EXPECT().IsValidator(valAddress).Return(
240
+ // false, nil,
241
+ // )
242
+
243
+ // claimTx, err := eng.Claim([]string{valAddress, testNetValAddr, discordID})
244
+ // assert.EqualError(t, err, "invalid argument: validator address")
245
+ // assert.Nil(t, claimTx)
246
+ // })
247
+
248
+ // t.Run("empty transaction ID", func(t *testing.T) {
249
+ // valAddress := "pc1p74scge5dyzjktv9q70xtr0pjmyqcqk7nuh8nzp"
250
+ // testNetValAddr := "tpc1pvmundkkp83u5cfz04sem5r7688dc0lef5u0mmv"
251
+ // discordID := "1234567890"
252
+ // amount := 74.68
253
+
254
+ // client.EXPECT().IsValidator(valAddress).Return(
255
+ // true, nil,
256
+ // )
257
+
258
+ // store.EXPECT().ClaimerInfo(testNetValAddr).Return(
259
+ // &rpstore.Claimer{
260
+ // DiscordID: discordID,
261
+ // TotalReward: amount,
262
+ // ClaimTransaction: nil,
263
+ // },
264
+ // )
265
+
266
+ // memo := fmt.Sprintf("RP to: %v", discordID)
267
+ // wallet.EXPECT().BondTransaction("", valAddress, memo, amount).Return(
268
+ // "", nil,
269
+ // )
270
+
271
+ // claimTx, err := eng.Claim([]string{valAddress, testNetValAddr, discordID})
272
+ // assert.EqualError(t, err, "can't send bond transaction")
273
+ // assert.Nil(t, claimTx)
274
+ // })
287
275
}
0 commit comments