-
Notifications
You must be signed in to change notification settings - Fork 168
/
contracts.go
38 lines (33 loc) · 1.03 KB
/
contracts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package helpers
import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Execute contract, recover from panic
func ExecuteContract(k wasmtypes.ContractOpsKeeper, childCtx sdk.Context, contractAddr sdk.AccAddress, msgBz []byte, err *error) {
// Recover from panic, return error
defer func() {
if recoveryError := recover(); recoveryError != nil {
// Determine error associated with panic
if isOutofGas, msg := IsOutOfGasError(recoveryError); isOutofGas {
*err = ErrOutOfGas.Wrapf("%s", msg)
} else {
*err = ErrContractExecutionPanic.Wrapf("%s", recoveryError)
}
}
}()
// Execute contract with sudo
_, *err = k.Sudo(childCtx, contractAddr, msgBz)
}
// Check if error is out of gas error
func IsOutOfGasError(err any) (bool, string) {
switch e := err.(type) {
case storetypes.ErrorOutOfGas:
return true, e.Descriptor
case storetypes.ErrorGasOverflow:
return true, e.Descriptor
default:
return false, ""
}
}