You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.2 KiB
72 lines
2.2 KiB
5 years ago
|
package apiv1
|
||
6 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
|
"github.com/ethereum/go-ethereum/crypto"
|
||
|
"github.com/harmony-one/harmony/core/types"
|
||
6 years ago
|
common2 "github.com/harmony-one/harmony/internal/common"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/utils"
|
||
5 years ago
|
staking "github.com/harmony-one/harmony/staking/types"
|
||
6 years ago
|
)
|
||
|
|
||
5 years ago
|
// defaultPageSize is to have default pagination.
|
||
5 years ago
|
const (
|
||
5 years ago
|
defaultPageSize = uint32(100)
|
||
5 years ago
|
)
|
||
|
|
||
|
// ReturnWithPagination returns result with pagination (offset, page in TxHistoryArgs).
|
||
|
func ReturnWithPagination(hashes []common.Hash, args TxHistoryArgs) []common.Hash {
|
||
5 years ago
|
pageSize := defaultPageSize
|
||
|
pageIndex := args.PageIndex
|
||
|
if args.PageSize > 0 {
|
||
|
pageSize = args.PageSize
|
||
5 years ago
|
}
|
||
5 years ago
|
if uint64(pageSize)*uint64(pageIndex) >= uint64(len(hashes)) {
|
||
5 years ago
|
return make([]common.Hash, 0)
|
||
|
}
|
||
5 years ago
|
if uint64(pageSize)*uint64(pageIndex)+uint64(pageSize) > uint64(len(hashes)) {
|
||
5 years ago
|
return hashes[pageSize*pageIndex:]
|
||
5 years ago
|
}
|
||
5 years ago
|
return hashes[pageSize*pageIndex : pageSize*pageIndex+pageSize]
|
||
5 years ago
|
}
|
||
|
|
||
6 years ago
|
// SubmitTransaction is a helper function that submits tx to txPool and logs a message.
|
||
5 years ago
|
func SubmitTransaction(
|
||
|
ctx context.Context, b Backend, tx *types.Transaction,
|
||
|
) (common.Hash, error) {
|
||
6 years ago
|
if err := b.SendTx(ctx, tx); err != nil {
|
||
|
return common.Hash{}, err
|
||
|
}
|
||
|
if tx.To() == nil {
|
||
5 years ago
|
signer := types.MakeSigner(b.ChainConfig(), b.CurrentBlock().Epoch())
|
||
6 years ago
|
from, err := types.Sender(signer, tx)
|
||
|
if err != nil {
|
||
|
return common.Hash{}, err
|
||
|
}
|
||
|
addr := crypto.CreateAddress(from, tx.Nonce())
|
||
5 years ago
|
utils.Logger().Info().
|
||
|
Str("fullhash", tx.Hash().Hex()).
|
||
|
Str("contract", common2.MustAddressToBech32(addr)).
|
||
|
Msg("Submitted contract creation")
|
||
6 years ago
|
} else {
|
||
5 years ago
|
utils.Logger().Info().
|
||
|
Str("fullhash", tx.Hash().Hex()).
|
||
|
Str("recipient", tx.To().Hex()).
|
||
|
Msg("Submitted transaction")
|
||
6 years ago
|
}
|
||
|
return tx.Hash(), nil
|
||
|
}
|
||
5 years ago
|
|
||
|
// SubmitStakingTransaction is a helper function that submits tx to txPool and logs a message.
|
||
|
func SubmitStakingTransaction(
|
||
|
ctx context.Context, b Backend, tx *staking.StakingTransaction,
|
||
|
) (common.Hash, error) {
|
||
|
if err := b.SendStakingTx(ctx, tx); err != nil {
|
||
|
return common.Hash{}, err
|
||
|
}
|
||
|
utils.Logger().Info().Str("fullhash", tx.Hash().Hex()).Msg("Submitted Staking transaction")
|
||
|
return tx.Hash(), nil
|
||
|
}
|