/
oracle_script.go
executable file
·124 lines (109 loc) · 4.75 KB
/
oracle_script.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package oraclekeeper
import (
"bytes"
"github.com/pkg/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
oracletypes "github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
)
// HasOracleScript checks if the oracle script of this ID exists in the storage.
func (k Keeper) HasOracleScript(ctx sdk.Context, id oracletypes.OracleScriptID) bool {
return ctx.KVStore(k.storeKey).Has(oracletypes.OracleScriptStoreKey(id))
}
// GetOracleScript returns the oracle script struct for the given ID or error if not exists.
func (k Keeper) GetOracleScript(ctx sdk.Context, id oracletypes.OracleScriptID) (oracletypes.OracleScript, error) {
bz := ctx.KVStore(k.storeKey).Get(oracletypes.OracleScriptStoreKey(id))
if bz == nil {
return oracletypes.OracleScript{}, sdkerrors.Wrapf(oracletypes.ErrOracleScriptNotFound, "id: %d", id)
}
var oracleScript oracletypes.OracleScript
k.cdc.MustUnmarshal(bz, &oracleScript)
return oracleScript, nil
}
// GetPaginatedOracleScripts returns oracle scripts with pagination.
func (k Keeper) GetPaginatedOracleScripts(
ctx sdk.Context,
limit, offset uint64,
) ([]oracletypes.OracleScript, *query.PageResponse, error) {
oracleScripts := make([]oracletypes.OracleScript, 0)
oracleScriptsStore := prefix.NewStore(ctx.KVStore(k.storeKey), oracletypes.OracleScriptStoreKeyPrefix)
pagination := &query.PageRequest{
Limit: limit,
Offset: offset,
}
pageRes, err := query.FilteredPaginate(
oracleScriptsStore,
pagination,
func(key []byte, value []byte, accumulate bool) (bool, error) {
var oracleScript oracletypes.OracleScript
if err := k.cdc.Unmarshal(value, &oracleScript); err != nil {
return false, err
}
if accumulate {
oracleScripts = append(oracleScripts, oracleScript)
}
return true, nil
},
)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to paginate oracle scripts")
}
return oracleScripts, pageRes, nil
}
// MustGetOracleScript returns the oracle script struct for the given ID. Panic if not exists.
func (k Keeper) MustGetOracleScript(ctx sdk.Context, id oracletypes.OracleScriptID) oracletypes.OracleScript {
oracleScript, err := k.GetOracleScript(ctx, id)
if err != nil {
panic(err)
}
return oracleScript
}
// SetOracleScript saves the given oracle script to the storage without performing validation.
func (k Keeper) SetOracleScript(ctx sdk.Context, id oracletypes.OracleScriptID, oracleScript oracletypes.OracleScript) {
oracleScript.ID = id
store := ctx.KVStore(k.storeKey)
store.Set(oracletypes.OracleScriptStoreKey(id), k.cdc.MustMarshal(&oracleScript))
}
// AddOracleScript adds the given oracle script to the storage.
func (k Keeper) AddOracleScript(ctx sdk.Context, oracleScript oracletypes.OracleScript) oracletypes.OracleScriptID {
id := k.GetNextOracleScriptID(ctx)
k.SetOracleScript(ctx, id, oracleScript)
return id
}
// MustEditOracleScript edits the given oracle script by id and flushes it to the storage. Panic if not exists.
func (k Keeper) MustEditOracleScript(ctx sdk.Context, id oracletypes.OracleScriptID, new oracletypes.OracleScript) {
oracleScript := k.MustGetOracleScript(ctx, id)
oracleScript.Owner = new.Owner
oracleScript.Name = modify(oracleScript.Name, new.Name)
oracleScript.Description = modify(oracleScript.Description, new.Description)
oracleScript.Filename = modify(oracleScript.Filename, new.Filename)
oracleScript.Schema = modify(oracleScript.Schema, new.Schema)
oracleScript.SourceCodeURL = modify(oracleScript.SourceCodeURL, new.SourceCodeURL)
k.SetOracleScript(ctx, id, oracleScript)
}
// GetAllOracleScripts returns the list of all oracle scripts in the store, or nil if there is none.
func (k Keeper) GetAllOracleScripts(ctx sdk.Context) (oracleScripts []oracletypes.OracleScript) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, oracletypes.OracleScriptStoreKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var oracleScript oracletypes.OracleScript
k.cdc.MustUnmarshal(iterator.Value(), &oracleScript)
oracleScripts = append(oracleScripts, oracleScript)
}
return oracleScripts
}
// AddOracleScriptFile compiles Wasm code (see go-owasm), adds the compiled file to filecache,
// and returns its sha256 reference name. Returns do-not-modify symbol if input is do-not-modify.
func (k Keeper) AddOracleScriptFile(file []byte) (string, error) {
if bytes.Equal(file, oracletypes.DoNotModifyBytes) {
return oracletypes.DoNotModify, nil
}
compiledFile, err := k.owasmVM.Compile(file, oracletypes.MaxCompiledWasmCodeSize)
if err != nil {
return "", sdkerrors.Wrapf(oracletypes.ErrOwasmCompilation, "with error: %s", err.Error())
}
return k.fileCache.AddFile(compiledFile), nil
}