2022-05-09 09:58:04 +02:00
|
|
|
package net
|
2022-05-08 13:13:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-18 11:50:45 +02:00
|
|
|
"math/big"
|
|
|
|
|
2022-05-08 13:13:45 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-05-18 11:50:45 +02:00
|
|
|
"github.com/grassrootseconomics/cic-go/provider"
|
2022-05-08 13:13:45 +02:00
|
|
|
"github.com/lmittmann/w3/module/eth"
|
|
|
|
)
|
|
|
|
|
2022-05-09 09:58:04 +02:00
|
|
|
func (c *CicNet) LastNonce(ctx context.Context, address common.Address) (uint64, error) {
|
2022-05-08 13:13:45 +02:00
|
|
|
var nonce uint64
|
|
|
|
|
2022-05-18 11:50:45 +02:00
|
|
|
err := c.provider.EthClient.CallCtx(
|
2022-05-08 13:13:45 +02:00
|
|
|
ctx,
|
|
|
|
eth.Nonce(address, nil).Returns(&nonce),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonce, nil
|
|
|
|
}
|
|
|
|
|
2022-05-18 11:50:45 +02:00
|
|
|
func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData provider.WriteTx) (common.Hash, error) {
|
2022-05-08 13:13:45 +02:00
|
|
|
var txHash common.Hash
|
|
|
|
|
2022-05-18 11:50:45 +02:00
|
|
|
tx, err := types.SignNewTx(&txData.PrivateKey, c.provider.Signer, &types.LegacyTx{
|
|
|
|
To: &txData.To,
|
|
|
|
Nonce: txData.Nonce,
|
2022-05-08 13:13:45 +02:00
|
|
|
Data: input,
|
2022-05-18 11:50:45 +02:00
|
|
|
Gas: txData.GasLimit,
|
2022-05-08 13:13:45 +02:00
|
|
|
GasPrice: big.NewInt(1),
|
|
|
|
})
|
|
|
|
|
2022-05-18 11:50:45 +02:00
|
|
|
err = c.provider.EthClient.CallCtx(
|
2022-05-08 13:13:45 +02:00
|
|
|
ctx,
|
|
|
|
eth.SendTransaction(tx).Returns(&txHash),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHash, nil
|
|
|
|
}
|