Skip to content

Commit 83a73e7

Browse files
committed
Add block reward calculation API
1 parent a4d4868 commit 83a73e7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

internal/quaiapi/quai_api.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,3 +1585,57 @@ func (s *PublicBlockChainQuaiAPI) GetTransactionReceipt(ctx context.Context, has
15851585
}
15861586
return fields, nil
15871587
}
1588+
1589+
// GetBlockRewardInQuai returns the block reward in Quai for a given block
1590+
func (s *PublicBlockChainQuaiAPI) GetBlockRewardInQuai(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) {
1591+
// Get the block
1592+
block, err := s.b.BlockByNumberOrHash(ctx, blockNrOrHash)
1593+
if block == nil || err != nil {
1594+
return nil, err
1595+
}
1596+
1597+
// Get the work object header which contains difficulty
1598+
woHeader := block.WorkObjectHeader()
1599+
if woHeader == nil {
1600+
return nil, errors.New("work object header not found")
1601+
}
1602+
1603+
// Get difficulty and exchange rate
1604+
difficulty := woHeader.Difficulty()
1605+
exchangeRate := block.ExchangeRate()
1606+
1607+
if difficulty == nil || exchangeRate == nil {
1608+
return nil, errors.New("difficulty or exchange rate not available")
1609+
}
1610+
1611+
// Calculate Quai reward using the misc package
1612+
quaiReward := misc.CalculateQuaiReward(difficulty, exchangeRate)
1613+
1614+
return (*hexutil.Big)(quaiReward), nil
1615+
}
1616+
1617+
// GetBlockRewardInQi returns the block reward in Qi for a given block
1618+
func (s *PublicBlockChainQuaiAPI) GetBlockRewardInQi(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) {
1619+
// Get the block
1620+
block, err := s.b.BlockByNumberOrHash(ctx, blockNrOrHash)
1621+
if block == nil || err != nil {
1622+
return nil, err
1623+
}
1624+
1625+
// Get the work object header which contains difficulty
1626+
woHeader := block.WorkObjectHeader()
1627+
if woHeader == nil {
1628+
return nil, errors.New("work object header not found")
1629+
}
1630+
1631+
// Get difficulty
1632+
difficulty := woHeader.Difficulty()
1633+
if difficulty == nil {
1634+
return nil, errors.New("difficulty not available")
1635+
}
1636+
1637+
// Calculate Qi reward using the misc package
1638+
qiReward := misc.CalculateQiReward(woHeader, difficulty)
1639+
1640+
return (*hexutil.Big)(qiReward), nil
1641+
}

0 commit comments

Comments
 (0)