/
smartaccount.go
51 lines (43 loc) · 1.4 KB
/
smartaccount.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
39
40
41
42
43
44
45
46
47
48
49
50
51
package types
import (
"strconv"
errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// generate predictable contract address
func Instantiate2Address(
ctx sdk.Context,
wasmKeeper wasmkeeper.Keeper,
codeId uint64,
initMsg []byte,
salt []byte,
pubKey cryptotypes.PubKey,
) (sdk.AccAddress, error) {
// we use pubkey.Address() as owner of this contract
// remember this account doesn't exist on chain yet if have not received any funds before
ownerAcc := sdk.AccAddress(pubKey.Address())
codeInfo := wasmKeeper.GetCodeInfo(ctx, codeId)
if codeInfo == nil {
return nil, errorsmod.Wrap(ErrNoSuchCodeID, strconv.FormatUint(codeId, 10))
}
addrGenerator := wasmkeeper.PredicableAddressGenerator(ownerAcc, salt, initMsg, true)
contractAddress := addrGenerator(ctx, codeId, codeInfo.CodeHash)
if wasmKeeper.HasContractInfo(ctx, contractAddress) {
return nil, errorsmod.Wrap(ErrInstantiateDuplicate, contractAddress.String())
}
return contractAddress, nil
}
// 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, ""
}
}