Skip to content

Commit

Permalink
add options to TxToMethod to just generate raw tx
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed Feb 28, 2025
1 parent 4143f01 commit 6df6a16
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 2 deletions.
32 changes: 31 additions & 1 deletion pkg/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,33 @@ func ParseSpec(
return name, string(abiBytes), nil
}

// Signer that just copy the raw Tx and returns error
// To be used on multisig flows where the user is going
// to sign and commit in separate flow
//
// Using this signer hack because it is the only setting that can be
// configured to get access to the raw tx before beign signed (subnet-evm
// transact function)
//
// Not multithread safe
var (
rawTxCopy *types.Transaction
errRawTxCopy = errors.New("raw tx has been copied, not signed")
)
func rawTxCopySigner(
_ common.Address,
tx *types.Transaction,
) (*types.Transaction, error) {
rawTxCopy = tx
return nil, errRawTxCopy
}

// get method name and types from [methodsSpec], then call it
// at the smart contract [contractAddress] with the given [params].
// also send [payment] tokens to it
func TxToMethod(
rpcURL string,
generateRawTxOnly bool,
privateKey string,
contractAddress common.Address,
payment *big.Int,
Expand Down Expand Up @@ -324,8 +346,14 @@ func TxToMethod(
return nil, nil, err
}
txOpts.Value = payment
if generateRawTxOnly {
txOpts.Signer = rawTxCopySigner
}
tx, err := contract.Transact(txOpts, methodName, params...)
if err != nil {
if err == errRawTxCopy {
return rawTxCopy, nil, nil
}
trace, traceCallErr := DebugTraceCall(
rpcURL,
privateKey,
Expand Down Expand Up @@ -373,6 +401,7 @@ func TxToMethod(
// also send [payment] tokens to it
func TxToMethodWithWarpMessage(
rpcURL string,
generateRawTxOnly bool,
privateKey string,
contractAddress common.Address,
warpMessage *avalancheWarp.Message,
Expand Down Expand Up @@ -402,8 +431,9 @@ func TxToMethodWithWarpMessage(
return nil, nil, err
}
defer client.Close()
tx, err := evm.GetSignedTxToMethodWithWarpMessage(
tx, err := evm.GetTxToMethodWithWarpMessage(
client,
generateRawTxOnly,
privateKey,
warpMessage,
contractAddress,
Expand Down
6 changes: 5 additions & 1 deletion pkg/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ func FundAddress(
return nil
}

func GetSignedTxToMethodWithWarpMessage(
func GetTxToMethodWithWarpMessage(
client ethclient.Client,
generateRawTxOnly bool,
privateKeyStr string,
warpMessage *avalancheWarp.Message,
contract common.Address,
Expand Down Expand Up @@ -320,6 +321,9 @@ func GetSignedTxToMethodWithWarpMessage(
Data: callData,
AccessList: accessList,
})
if generateRawTxOnly {
return tx, nil
}
txSigner := types.LatestSignerForChainID(chainID)
return types.SignTx(tx, txSigner, privateKey)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/ictt/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func RegisterRemote(
}
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
remoteAddress,
nil,
Expand Down
9 changes: 9 additions & 0 deletions pkg/ictt/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func ERC20TokenHomeSend(
}
if _, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
tokenAddress,
nil,
Expand All @@ -251,6 +252,7 @@ func ERC20TokenHomeSend(
}
_, _, err = contract.TxToMethod(
rpcURL,
false,
privateKey,
homeAddress,
nil,
Expand Down Expand Up @@ -298,6 +300,7 @@ func NativeTokenHomeSend(
}
_, _, err = contract.TxToMethod(
rpcURL,
false,
privateKey,
homeAddress,
amount,
Expand All @@ -320,6 +323,7 @@ func ERC20TokenRemoteSend(
) error {
if _, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
remoteAddress,
nil,
Expand Down Expand Up @@ -353,6 +357,7 @@ func ERC20TokenRemoteSend(
}
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
remoteAddress,
nil,
Expand Down Expand Up @@ -396,6 +401,7 @@ func NativeTokenRemoteSend(
}
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
remoteAddress,
amount,
Expand All @@ -417,6 +423,7 @@ func NativeTokenHomeAddCollateral(
) error {
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
homeAddress,
amount,
Expand All @@ -443,6 +450,7 @@ func ERC20TokenHomeAddCollateral(
}
if _, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
tokenAddress,
nil,
Expand All @@ -456,6 +464,7 @@ func ERC20TokenHomeAddCollateral(
}
_, _, err = contract.TxToMethod(
rpcURL,
false,
privateKey,
homeAddress,
nil,
Expand Down
1 change: 1 addition & 0 deletions pkg/interchain/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func SendCrossChainMessage(
}
return contract.TxToMethod(
rpcURL,
false,
privateKey,
messengerAddress,
nil,
Expand Down
4 changes: 4 additions & 0 deletions pkg/precompiles/allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func SetAdmin(
) error {
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
precompile,
nil,
Expand All @@ -38,6 +39,7 @@ func SetManager(
) error {
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
precompile,
nil,
Expand All @@ -57,6 +59,7 @@ func SetEnabled(
) error {
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
precompile,
nil,
Expand All @@ -76,6 +79,7 @@ func SetNone(
) error {
_, _, err := contract.TxToMethod(
rpcURL,
false,
privateKey,
precompile,
nil,
Expand Down
1 change: 1 addition & 0 deletions pkg/validatormanager/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func SetupValidatorManagerAtProxy(
) (*types.Transaction, *types.Receipt, error) {
return contract.TxToMethod(
rpcURL,
false,
proxyManagerPrivateKey,
common.HexToAddress(validatorManagerSDK.ProxyAdminContractAddress),
big.NewInt(0),
Expand Down
3 changes: 3 additions & 0 deletions pkg/validatormanager/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func InitializeValidatorRegistrationPoSNative(

return contract.TxToMethod(
rpcURL,
false,
managerOwnerPrivateKey,
managerAddress,
stakeAmount,
Expand Down Expand Up @@ -138,6 +139,7 @@ func InitializeValidatorRegistrationPoA(
}
return contract.TxToMethod(
rpcURL,
false,
managerOwnerPrivateKey,
managerAddress,
big.NewInt(0),
Expand Down Expand Up @@ -322,6 +324,7 @@ func CompleteValidatorRegistration(
) (*types.Transaction, *types.Receipt, error) {
return contract.TxToMethodWithWarpMessage(
rpcURL,
false,
privateKey,
managerAddress,
subnetValidatorRegistrationSignedMessage,
Expand Down
4 changes: 4 additions & 0 deletions pkg/validatormanager/removal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func InitializeValidatorRemoval(
if force {
return contract.TxToMethod(
rpcURL,
false,
privateKey,
managerAddress,
big.NewInt(0),
Expand All @@ -56,6 +57,7 @@ func InitializeValidatorRemoval(
// remove PoS validator with uptime proof
return contract.TxToMethodWithWarpMessage(
rpcURL,
false,
privateKey,
managerAddress,
uptimeProofSignedMessage,
Expand All @@ -71,6 +73,7 @@ func InitializeValidatorRemoval(
// PoA case
return contract.TxToMethod(
rpcURL,
false,
privateKey,
managerAddress,
big.NewInt(0),
Expand Down Expand Up @@ -286,6 +289,7 @@ func CompleteValidatorRemoval(
) (*types.Transaction, *types.Receipt, error) {
return contract.TxToMethodWithWarpMessage(
rpcURL,
false,
privateKey,
managerAddress,
subnetValidatorRegistrationSignedMessage,
Expand Down
1 change: 1 addition & 0 deletions sdk/validatormanager/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func InitializeValidatorsSet(
}
return contract.TxToMethodWithWarpMessage(
rpcURL,
false,
privateKey,
managerAddress,
subnetConversionSignedMessage,
Expand Down
1 change: 1 addition & 0 deletions sdk/validatormanager/validator_manager_poa.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func PoAValidatorManagerInitialize(
}
return contract.TxToMethod(
rpcURL,
false,
privateKey,
managerAddress,
nil,
Expand Down
1 change: 1 addition & 0 deletions sdk/validatormanager/validator_manager_pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func PoSValidatorManagerInitialize(

return contract.TxToMethod(
rpcURL,
false,
privateKey,
managerAddress,
nil,
Expand Down

0 comments on commit 6df6a16

Please sign in to comment.