Add contract files
This commit is contained in:
parent
a6e606fa40
commit
2bbef7e82a
5 changed files with 108 additions and 0 deletions
1
config.json
Normal file
1
config.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"name":"Oracle example","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"balance","offset":196,"parameters":[{"name":"playerID","type":"String"}],"returntype":"Integer","safe":false},{"name":"buyItem","offset":218,"parameters":[{"name":"playerID","type":"String"},{"name":"itemID","type":"Integer"}],"returntype":"Void","safe":false},{"name":"cbBuyItem","offset":358,"parameters":[{"name":"url","type":"String"},{"name":"userdata","type":"Any"},{"name":"code","type":"Integer"},{"name":"res","type":"ByteArray"}],"returntype":"Void","safe":false},{"name":"items","offset":207,"parameters":[{"name":"playerID","type":"String"}],"returntype":"Array","safe":false}],"events":[]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":["request"]}],"supportedstandards":[],"trusts":[],"extra":null}
|
5
config.yml
Normal file
5
config.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
name: "Oracle example"
|
||||
supportedstandards: []
|
||||
events:
|
||||
permissions:
|
||||
- methods: ["request"]
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module oracle
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231027092558-8ed6d97085d3
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -0,0 +1,2 @@
|
|||
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231027092558-8ed6d97085d3 h1:ybQcK5pTNAR+wQU3k4cGeOZN6OCiVcQkbgR3Zl6NFPU=
|
||||
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231027092558-8ed6d97085d3/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
|
95
main.go
Normal file
95
main.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
package oracleexample
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/native/oracle"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
balance int
|
||||
gear []int
|
||||
}
|
||||
|
||||
func player(playerID string) Player {
|
||||
ctx := storage.GetContext()
|
||||
data := storage.Get(ctx, playerID)
|
||||
if data == nil {
|
||||
panic("player is not exists")
|
||||
}
|
||||
|
||||
return std.Deserialize(data.([]byte)).(Player)
|
||||
}
|
||||
|
||||
func _deploy(data interface{}, isUpdate bool) {
|
||||
if isUpdate {
|
||||
return
|
||||
}
|
||||
|
||||
callie := Player{
|
||||
balance: 3000,
|
||||
gear: []int{},
|
||||
}
|
||||
|
||||
ctx := storage.GetContext()
|
||||
storage.Put(ctx, "Callie", std.Serialize(callie))
|
||||
|
||||
requestURI := "https://git.frostfs.info/alexvanin/oracle-test/raw/branch/master/data.json"
|
||||
storage.Put(ctx, "db", requestURI)
|
||||
}
|
||||
|
||||
func Balance(playerID string) int {
|
||||
p := player(playerID)
|
||||
return p.balance
|
||||
}
|
||||
|
||||
func Items(playerID string) []int {
|
||||
p := player(playerID)
|
||||
return p.gear
|
||||
}
|
||||
|
||||
func BuyItem(playerID string, itemID int) {
|
||||
p := player(playerID)
|
||||
for i := range p.gear {
|
||||
if itemID == p.gear[i] {
|
||||
panic("item already purchased")
|
||||
}
|
||||
}
|
||||
|
||||
ctx := storage.GetContext()
|
||||
uri := storage.Get(ctx, "db").(string)
|
||||
|
||||
filter := []byte("$.store.item[" + std.Itoa10(itemID) + "]")
|
||||
|
||||
oracle.Request(uri, filter, "cbBuyItem", playerID, 2*oracle.MinimumResponseGas)
|
||||
}
|
||||
|
||||
func CbBuyItem(url string, userdata any, code int, res []byte) {
|
||||
callingHash := runtime.GetCallingScriptHash()
|
||||
if !callingHash.Equals(oracle.Hash) {
|
||||
panic("not called from oracle contract")
|
||||
}
|
||||
if code != oracle.Success {
|
||||
panic("request failed for " + url + " with code " + std.Itoa(code, 10))
|
||||
}
|
||||
runtime.Log("result for " + url + ": " + string(res))
|
||||
|
||||
ln := len(res)
|
||||
data := std.JSONDeserialize(res[1 : ln-1]).(map[string]any)
|
||||
|
||||
playerID := userdata.(string)
|
||||
p := player(playerID)
|
||||
|
||||
price := data["price"].(int)
|
||||
|
||||
if p.balance < price {
|
||||
panic("not enough")
|
||||
}
|
||||
|
||||
p.balance -= price
|
||||
p.gear = append(p.gear, data["id"].(int))
|
||||
|
||||
ctx := storage.GetContext()
|
||||
storage.Put(ctx, playerID, std.Serialize(p))
|
||||
}
|
Loading…
Reference in a new issue