func Transfer(client *ethclient.Client, privateKeyHex string, recipient common.Address,
amount *big.Int, data []byte) (transaction *types.Transaction, err error) {
privateKey, err := crypto.HexToECDSA(privateKeyHex)
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, ErrEcdsaPublickey
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return nil, err
}
gasLimit, err := estimateGas(client, fromAddress, recipient, amount, data) // in units
if err != nil {
return nil, err
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
tx := types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &recipient,
Value: amount,
Gas: gasLimit,
GasPrice: gasPrice,
Data: data,
})
chainID, err := client.NetworkID(context.Background())
if err != nil {
return nil, err
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
return nil, err
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return nil, err
}
return signedTx, nil
}
func estimateGas(client *ethclient.Client, fromAddress, toAddress common.Address, value *big.Int, data []byte) (uint64, error) {
callMsg := ethereum.CallMsg{
From: fromAddress,
To: &toAddress,
Value: value,
Data: data,
}
gasLimit, err := client.EstimateGas(context.Background(), callMsg)
if err != nil {
return 0, err
}
return gasLimit, nil
}
type OpusService struct {
ownerPk string
ownerAddress common.Address
privateKey *ecdsa.PrivateKey
// abiInstance abi.ABI
contractAddress common.Address
contract *Contract
chainId *big.Int
rpcUrl string
client *ethclient.Client
}
func NewOpusService(rpcUrl string, contractAddress string, ownerPk string) (s *OpusService, err error) {
s = &OpusService{
rpcUrl: rpcUrl,
ownerPk: ownerPk,
contractAddress: common.HexToAddress(contractAddress),
}
s.privateKey, err = crypto.HexToECDSA(s.ownerPk)
if err != nil {
return
}
publicKey := s.privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, ethUtil.ErrEcdsaPublicKey
}
s.ownerAddress = crypto.PubkeyToAddress(*publicKeyECDSA)
s.client, err = ethclient.Dial(rpcUrl)
if err != nil {
return nil, err
}
s.chainId, err = s.client.ChainID(context.Background())
if err != nil {
return nil, err
}
s.contract, err = NewContract(s.contractAddress, s.client)
return
}
func (s *OpusService) EthClient() (client *ethclient.Client) {
return s.client
}
func (s *OpusService) Contract() (client *Contract) {
return s.contract
}
func (s *OpusService) TransactOpts() (opts *bind.TransactOpts, err error) {
return bind.NewKeyedTransactorWithChainID(s.privateKey, s.chainId)
}
func (s *OpusService) TransactOptsWithPrivateKey(privateKeyHex string) (opts *bind.TransactOpts, err error) {
privateKey, err := crypto.HexToECDSA(privateKeyHex)
if err != nil {
return nil, err
}
return bind.NewKeyedTransactorWithChainID(privateKey, s.chainId)
}
go 使用solidity合约
猜你喜欢
转载自blog.csdn.net/qq_34751210/article/details/127858576
今日推荐
周排行