Skip to content

Commit

Permalink
rawTransaction: refactor and fix SendRawTransactionDelegateSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
inciner8r committed Mar 31, 2024
1 parent 191154d commit 02d09f6
Showing 1 changed file with 22 additions and 46 deletions.
68 changes: 22 additions & 46 deletions config/smartcontract/rawtransaction/rawtransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (

"github.com/MyriadFlow/storefront-gateway/config/envconfig"
"github.com/MyriadFlow/storefront-gateway/config/smartcontract"
"github.com/MyriadFlow/storefront-gateway/generated/smartcontract/signatureSeries"
"github.com/MyriadFlow/storefront-gateway/util/pkg/ethwallet"
"github.com/MyriadFlow/storefront-gateway/util/pkg/logwrapper"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/sirupsen/logrus"
)

func SendRawTransaction(abiS string, method string, args ...interface{}) (*types.Transaction, error) {
Expand Down Expand Up @@ -98,13 +101,8 @@ func SendRawTransaction(abiS string, method string, args ...interface{}) (*types
return signedTx, nil
}

func SendRawTransactionDelegateSignature(abiS string, method string, address string, args ...interface{}) (*types.Transaction, error) {
func SendRawTransactionDelegateSignature(abiS string, method string, address string, creator common.Address, metdata string, royalty *big.Int) (*types.Transaction, error) {

abiP, err := abi.JSON(strings.NewReader(abiS))
if err != nil {
logwrapper.Errorf("failed to parse JSON abi, error %v", err)
return nil, err
}
client, err := smartcontract.GetClient()
if err != nil {
return nil, err
Expand All @@ -126,59 +124,37 @@ func SendRawTransactionDelegateSignature(abiS string, method string, address str
logwrapper.Warnf("failed to get nonce")
return nil, err
}
envContractAddress := address

toAddress := common.HexToAddress(envContractAddress)

chainID, err := client.NetworkID(context.Background())
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
logwrapper.Errorf("failed to call client.NetworkID, error: %v", err.Error())
return nil, err
if err != nil {
logwrapper.Warnf("failed to get gas price")
return nil, err
}
}

bytesData, err := abiP.Pack(method, args...)
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(80001))
if err != nil {
logwrapper.Errorf("failed to pack trasaction of method %v, error: %v", method, err)
logwrapper.Warnf("failed to get transactops")
return nil, err
}

logwrapper.Infof("nonce is %v", nonce)

maxPriorityFeePerGas, err := client.SuggestGasTipCap(context.Background())
if err != nil {
logwrapper.Errorf("failed to suggestGasTipCap, error %v", err)
return nil, err
}
config := &params.ChainConfig{
ChainID: big.NewInt(80001),
}
bn, _ := client.BlockNumber(context.Background())
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(300000) // in units
auth.GasPrice = gasPrice

bignumBn := big.NewInt(0).SetUint64(bn)
blk, _ := client.BlockByNumber(context.Background(), bignumBn)
baseFee := misc.CalcBaseFee(config, blk.Header())
big2 := big.NewInt(2)
mulRes := big.NewInt(0).Mul(baseFee, big2)
maxFeePerGas := big.NewInt(0).Add(mulRes, maxPriorityFeePerGas)
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasFeeCap: maxFeePerGas,
GasTipCap: maxPriorityFeePerGas,
Gas: 310000,
To: &toAddress,
Data: bytesData,
})
signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey)
addressVar := common.HexToAddress(address)
instance, err := signatureSeries.NewSignatureSeries(addressVar, client)
if err != nil {
logwrapper.Errorf("failed to sign trasaction %v, error: %v", tx, err.Error())
logwrapper.Warnf("failed to create instance")
return nil, err
}

err = client.SendTransaction(context.TODO(), signedTx)
tx, err := instance.DelegateAssetCreation(auth, creator, metdata, royalty)
if err != nil {
logwrapper.Error("failed to send trasaction, error: ", err)
logwrapper.Warnf("failed to execute transaction")
return nil, err
}
return signedTx, nil
logrus.Info("transaction send", tx.Hash().String())
return tx, nil
}

0 comments on commit 02d09f6

Please sign in to comment.