mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
ethclient: move test
This commit is contained in:
parent
49b29c0fa6
commit
c0be9ce9aa
2 changed files with 35 additions and 40 deletions
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum"
|
"github.com/ethereum/go-ethereum"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/consensus/ethash"
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
|
@ -772,3 +773,37 @@ func sendTransaction(ec *Client) error {
|
||||||
}
|
}
|
||||||
return ec.SendTransaction(context.Background(), tx)
|
return ec.SendTransaction(context.Background(), tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Here we show how to get the error message of reverted contract call.
|
||||||
|
func ExampleRevertErrorData() {
|
||||||
|
// First create an ethclient.Client instance.
|
||||||
|
ctx := context.Background()
|
||||||
|
ec, _ := DialContext(ctx, exampleNode.HTTPEndpoint())
|
||||||
|
|
||||||
|
// Call the contract.
|
||||||
|
// Note we expect the call to return an error.
|
||||||
|
contract := common.HexToAddress("290f1b36649a61e369c6276f6d29463335b4400c")
|
||||||
|
call := ethereum.CallMsg{To: &contract, Gas: 30000}
|
||||||
|
result, err := ec.CallContract(ctx, call, nil)
|
||||||
|
if len(result) > 0 {
|
||||||
|
panic("got result")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
panic("call did not return error")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the low-level revert data from the error.
|
||||||
|
revertData, ok := RevertErrorData(err)
|
||||||
|
if !ok {
|
||||||
|
panic("unpacking revert failed")
|
||||||
|
}
|
||||||
|
fmt.Printf("revert: %x\n", revertData)
|
||||||
|
|
||||||
|
// Parse the revert data to obtain the error message.
|
||||||
|
message, err := abi.UnpackRevert(revertData)
|
||||||
|
fmt.Println("message:", message)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// revert: 08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a75736572206572726f72
|
||||||
|
// message: user error
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,6 @@
|
||||||
package ethclient
|
package ethclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum"
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -39,37 +33,3 @@ func init() {
|
||||||
}
|
}
|
||||||
exampleNode = n
|
exampleNode = n
|
||||||
}
|
}
|
||||||
|
|
||||||
// Here we show how to get the error message of reverted contract call.
|
|
||||||
func ExampleRevertErrorData() {
|
|
||||||
// First create an ethclient.Client instance.
|
|
||||||
ctx := context.Background()
|
|
||||||
ec, _ := DialContext(ctx, exampleNode.HTTPEndpoint())
|
|
||||||
|
|
||||||
// Call the contract.
|
|
||||||
// Note we expect the call to return an error.
|
|
||||||
contract := common.HexToAddress("290f1b36649a61e369c6276f6d29463335b4400c")
|
|
||||||
call := ethereum.CallMsg{To: &contract, Gas: 30000}
|
|
||||||
result, err := ec.CallContract(ctx, call, nil)
|
|
||||||
if len(result) > 0 {
|
|
||||||
panic("got result")
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
panic("call did not return error")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the low-level revert data from the error.
|
|
||||||
revertData, ok := RevertErrorData(err)
|
|
||||||
if !ok {
|
|
||||||
panic("unpacking revert failed")
|
|
||||||
}
|
|
||||||
fmt.Printf("revert: %x\n", revertData)
|
|
||||||
|
|
||||||
// Parse the revert data to obtain the error message.
|
|
||||||
message, err := abi.UnpackRevert(revertData)
|
|
||||||
fmt.Println("message:", message)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// revert: 08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a75736572206572726f72
|
|
||||||
// message: user error
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue