Skip to content

Commit 29e92e7

Browse files
committed
[api] add gasFee info (#3063)
1 parent 39d2933 commit 29e92e7

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

api/coreservice.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,6 @@ func (core *coreService) actionsInBlock(blk *block.Block, start, count uint64) [
11511151
h := blk.HashBlock()
11521152
blkHash := hex.EncodeToString(h[:])
11531153
blkHeight := blk.Height()
1154-
ts := blk.Header.BlockHeaderCoreProto().Timestamp
11551154

11561155
lastAction := start + count
11571156
if count == math.MaxUint64 {
@@ -1169,14 +1168,21 @@ func (core *coreService) actionsInBlock(blk *block.Block, start, count uint64) [
11691168
log.L().Debug("Skipping action due to hash error", zap.Error(err))
11701169
continue
11711170
}
1171+
receipt, err := core.dao.GetReceiptByActionHash(actHash, blkHeight)
1172+
if err != nil {
1173+
log.L().Debug("Skipping action due to failing to get receipt", zap.Error(err))
1174+
continue
1175+
}
1176+
gas := new(big.Int).Mul(selp.GasPrice(), big.NewInt(int64(receipt.GasConsumed)))
11721177
sender := selp.SrcPubkey().Address()
11731178
res = append(res, &iotexapi.ActionInfo{
11741179
Action: selp.Proto(),
11751180
ActHash: hex.EncodeToString(actHash[:]),
11761181
BlkHash: blkHash,
1177-
Timestamp: ts,
1182+
Timestamp: blk.Header.BlockHeaderCoreProto().Timestamp,
11781183
BlkHeight: blkHeight,
11791184
Sender: sender.String(),
1185+
GasFee: gas.String(),
11801186
Index: uint32(i),
11811187
})
11821188
}
@@ -1187,7 +1193,6 @@ func (core *coreService) reverseActionsInBlock(blk *block.Block, reverseStart, c
11871193
h := blk.HashBlock()
11881194
blkHash := hex.EncodeToString(h[:])
11891195
blkHeight := blk.Height()
1190-
ts := blk.Header.BlockHeaderCoreProto().Timestamp
11911196

11921197
var res []*iotexapi.ActionInfo
11931198
for i := reverseStart; i < uint64(len(blk.Actions)) && i < reverseStart+count; i++ {
@@ -1198,14 +1203,21 @@ func (core *coreService) reverseActionsInBlock(blk *block.Block, reverseStart, c
11981203
log.L().Debug("Skipping action due to hash error", zap.Error(err))
11991204
continue
12001205
}
1206+
receipt, err := core.dao.GetReceiptByActionHash(actHash, blkHeight)
1207+
if err != nil {
1208+
log.L().Debug("Skipping action due to failing to get receipt", zap.Error(err))
1209+
continue
1210+
}
1211+
gas := new(big.Int).Mul(selp.GasPrice(), big.NewInt(int64(receipt.GasConsumed)))
12011212
sender := selp.SrcPubkey().Address()
12021213
res = append([]*iotexapi.ActionInfo{{
12031214
Action: selp.Proto(),
12041215
ActHash: hex.EncodeToString(actHash[:]),
12051216
BlkHash: blkHash,
1206-
Timestamp: ts,
1217+
Timestamp: blk.Header.BlockHeaderCoreProto().Timestamp,
12071218
BlkHeight: blkHeight,
12081219
Sender: sender.String(),
1220+
GasFee: gas.String(),
12091221
Index: uint32(ri),
12101222
}}, res...)
12111223
}

api/grpcserver_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,30 +290,35 @@ var (
290290
start uint64
291291
count uint64
292292
numActions int
293+
firstTxGas string
293294
}{
294295
{
295296
2,
296297
0,
297298
7,
298299
7,
300+
"0",
299301
},
300302
{
301303
4,
302304
2,
303305
5,
304306
3,
307+
"0",
305308
},
306309
{
307310
3,
308311
0,
309312
0,
310313
0,
314+
"",
311315
},
312316
{
313317
1,
314318
0,
315319
math.MaxUint64,
316320
2,
321+
"0",
317322
},
318323
}
319324

@@ -1137,6 +1142,9 @@ func TestGrpcServer_GetActionsByBlock(t *testing.T) {
11371142
}
11381143
require.NoError(err)
11391144
require.Equal(test.numActions, len(res.ActionInfo))
1145+
if test.numActions > 0 {
1146+
require.Equal(test.firstTxGas, res.ActionInfo[0].GasFee)
1147+
}
11401148
for _, v := range res.ActionInfo {
11411149
require.Equal(test.blkHeight, v.BlkHeight)
11421150
require.Equal(blkHash[test.blkHeight], v.BlkHash)

0 commit comments

Comments
 (0)