Add gas estimation

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera
2020-07-20 19:48:30 +02:00
parent e1c9c4297b
commit 310fa67f9d
10 changed files with 183 additions and 18 deletions
+14 -6
View File
@@ -18,6 +18,7 @@ type MpoolAPI struct {
fx.In
WalletAPI
GasAPI
Chain *store.ChainStore
@@ -86,15 +87,26 @@ func (a *MpoolAPI) MpoolPush(ctx context.Context, smsg *types.SignedMessage) (ci
return a.Mpool.Push(smsg)
}
// GasMargin sets by how much should gas limit be increased over test execution
var GasMargin = 1.2
func (a *MpoolAPI) MpoolPushMessage(ctx context.Context, msg *types.Message) (*types.SignedMessage, error) {
if msg.Nonce != 0 {
return nil, xerrors.Errorf("MpoolPushMessage expects message nonce to be 0, was %d", msg.Nonce)
}
if msg.GasLimit == 0 {
msg.GasLimit = 100_000_000 // TODO: gas limit estimation
gasLimit, err := a.GasEstimateGasLimit(ctx, msg, types.TipSetKey{})
if err != nil {
return nil, xerrors.Errorf("estimating gas limit: %w", err)
}
msg.GasLimit = int64(float64(gasLimit) * GasMargin)
}
if types.BigCmp(msg.GasPrice, types.NewInt(0)) == 0 {
msg.GasPrice = types.NewInt(1) // TODO: gas price estimation
gasPrice, err := a.GasEstimateGasPrice(ctx, 2, msg.From, msg.GasLimit, types.TipSetKey{})
if err != nil {
return nil, xerrors.Errorf("estimating gas price: %w", err)
}
msg.GasPrice = gasPrice
}
return a.Mpool.PushWithNonce(ctx, msg.From, func(from address.Address, nonce uint64) (*types.SignedMessage, error) {
@@ -124,7 +136,3 @@ func (a *MpoolAPI) MpoolGetNonce(ctx context.Context, addr address.Address) (uin
func (a *MpoolAPI) MpoolSub(ctx context.Context) (<-chan api.MpoolUpdate, error) {
return a.Mpool.Updates(ctx)
}
func (a *MpoolAPI) MpoolEstimateGasPrice(ctx context.Context, nblocksincl uint64, sender address.Address, gaslimit int64, tsk types.TipSetKey) (types.BigInt, error) {
return a.Mpool.EstimateGasPrice(ctx, nblocksincl, sender, gaslimit, tsk)
}