generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): call wasm contract from fileSystem
- Loading branch information
Showing
8 changed files
with
136 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,113 @@ | ||
package fs | ||
|
||
import ( | ||
goctx "context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
) | ||
|
||
type FileSystem struct { | ||
ctx goctx.Context | ||
wasmKeeper types.WasmKeeper | ||
} | ||
|
||
// New return a new FileSystem object that will handle all virtual file on the interpreter. | ||
// File can be provided from different sources like CosmWasm cw-storage smart contract. | ||
func New(keeper types.WasmKeeper) FileSystem { | ||
func New(ctx goctx.Context, keeper types.WasmKeeper) FileSystem { | ||
return FileSystem{ | ||
ctx: ctx, | ||
wasmKeeper: keeper, | ||
} | ||
} | ||
|
||
func (f FileSystem) Open(name string) (fs.File, error) { | ||
return nil, fmt.Errorf("not implemented") | ||
data, err := f.ReadFile(name) | ||
return Object(data), err | ||
} | ||
|
||
// ReadFile reads the named file and returns its contents. | ||
// A successful call returns a nil error, not io.EOF. | ||
// (Because ReadFi le reads the whole file, the expected EOF | ||
// from the final Read is not treated as an error to be reported.) | ||
// | ||
// The caller is permitted to modify the returned byte slice. | ||
// This method should return a copy of the underlying data. | ||
func (f FileSystem) ReadFile(name string) ([]byte, error) { | ||
sdkCtx := sdk.UnwrapSDKContext(f.ctx) | ||
|
||
req := []byte(fmt.Sprintf("{\"object_data\":{\"id\": \"%s\"}}", name)) | ||
contractAddr, err := sdk.AccAddressFromBech32("okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data, err := f.wasmKeeper.QuerySmart(sdkCtx, contractAddr, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var program string | ||
err = json.Unmarshal(data, &program) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
decoded, err := base64.StdEncoding.DecodeString(program) | ||
return decoded, err | ||
} | ||
|
||
type Object []byte | ||
|
||
type ObjectInfo struct { | ||
name string | ||
size int64 | ||
} | ||
|
||
func From(object Object) ObjectInfo { | ||
return ObjectInfo{ | ||
name: "contract", | ||
size: int64(len(object)), | ||
} | ||
} | ||
|
||
func (o ObjectInfo) Name() string { | ||
return o.name | ||
} | ||
|
||
func (o ObjectInfo) Size() int64 { | ||
return o.size | ||
} | ||
|
||
func (o ObjectInfo) Mode() fs.FileMode { | ||
return fs.ModeIrregular | ||
} | ||
|
||
func (o ObjectInfo) ModTime() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func (o ObjectInfo) IsDir() bool { | ||
return false | ||
} | ||
|
||
func (o ObjectInfo) Sys() any { | ||
return nil | ||
} | ||
|
||
func (o Object) Stat() (fs.FileInfo, error) { | ||
return From(o), nil | ||
} | ||
|
||
func (o Object) Read(bytes []byte) (int, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (o Object) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package predicate | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
"github.com/okp4/okp4d/x/logic/util" | ||
) | ||
|
||
func QueryWasm(vm *engine.VM, contractAddr engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { | ||
return engine.Delay(func(ctx context.Context) *engine.Promise { | ||
sdkContext, err := util.UnwrapSDKContext(ctx) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("query_wasm/1: %w", err)) | ||
} | ||
wasmKeeper := sdkContext.Value(types.WasmKeeperContextKey).(types.WasmKeeper) | ||
addr, err := getBech32(env, contractAddr) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("query_wasm/1: %w", err)) | ||
} | ||
|
||
req := []byte("{\"ask\":{\"query\": \"query_wasm('okp410gnd30r45k9658jm7hzxvp8ehz4ptf33tqjnaepwkunev6kax5ks3mnvmf').\"}}") | ||
if !json.Valid(req) { | ||
return engine.Error(fmt.Errorf("query_wasm/1: wasm query must be a valid json")) | ||
} | ||
res, err := wasmKeeper.QuerySmart(sdkContext, addr, req) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("query_wasm/1: %w", err)) | ||
} | ||
fmt.Printf("result %w", string(res)) | ||
return engine.Unify(vm, contractAddr, engine.Integer(sdkContext.BlockHeight()), cont, env) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters