Add initial version of l6 materials

The code of the contracts is identical to what it was in l4.

Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
This commit is contained in:
Vladimir Domnich 2023-11-19 11:16:31 +04:00
parent 2ebf2c5c51
commit c776a7e4e9
8 changed files with 199 additions and 0 deletions

3
l6/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*.nef
config.json

42
l6/data.json Normal file
View file

@ -0,0 +1,42 @@
{
"store": {
"item": [
{
"id": 0,
"category": "Headgear",
"title": "Bucket Hat",
"price": 400
},
{
"id": 1,
"category": "Headgear",
"title": "Fugu Bell Hat",
"price": 1700
},
{
"id": 2,
"category": "Clothing",
"title": "Lumberjack Shirt",
"price": 800
},
{
"id": 3,
"category": "Clothing",
"title": "Pink Hoodie",
"price": 3400
},
{
"id": 4,
"category": "Shoes",
"title": "Cyan Trainers",
"price": 700
},
{
"id": 5,
"category": "Shoes",
"title": "Red Hi-Tops",
"price": 1800
}
]
}
}

5
l6/go.mod Normal file
View file

@ -0,0 +1,5 @@
module oracle
go 1.20
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231027092558-8ed6d97085d3

2
l6/go.sum Normal file
View 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=

5
l6/lottery/config.yml Normal file
View file

@ -0,0 +1,5 @@
name: "Lottery"
supportedstandards: []
events:
permissions:
- methods: []

View file

@ -0,0 +1,13 @@
package lottery
import (
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
)
func IsWinner(playerName string) bool {
if playerName == "demo" {
return true
}
random := runtime.GetRandom()
return random < 100
}

5
l6/player/config.yml Normal file
View file

@ -0,0 +1,5 @@
name: "Player"
supportedstandards: []
events:
permissions:
- methods: ["request", "isWinner"]

View file

@ -0,0 +1,124 @@
package player
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
items []int
}
const storeURLKey = "storeURL"
func _deploy(data interface{}, isUpdate bool) {
if isUpdate {
return
}
ctx := storage.GetContext()
storeURL := "https://git.frostfs.info/Web3N3/web3-course/raw/branch/master/l4/data.json"
storage.Put(ctx, storeURLKey, storeURL)
}
func NewPlayer(playerName string) {
ctx := storage.GetContext()
existingPlayer := storage.Get(ctx, playerName)
if existingPlayer != nil {
panic("player already exists")
}
items := make([]int, 0)
// NOTE: the commented code below requires `lottery/lottery_contract.go` to be deployed on the chain
// If you would like to uncomment it, please:
// 1. Deploy `lottery/lottery_contract.go` contract to your chain
// 2. Replace hash inisde `address.ToHash160` with the hash of deployed contract
// 3. Add imports to this file:
// "github.com/nspcc-dev/neo-go/pkg/interop/contract"
// "github.com/nspcc-dev/neo-go/pkg/interop/lib/address"
// 4. Now you're ready to uncomment the code and run player contract in integration with lottery.
//
// lotteryHash := address.ToHash160("NVBGxUL69FJEV9nhrZQQhPbhkzzvsJqF8q")
// isWinner := contract.Call(lotteryHash, "isWinner", contract.ReadOnly, playerName).(bool)
// if isWinner {
// items = append(items, 0)
// }
player := Player{
balance: 3000,
items: items,
}
storage.Put(ctx, playerName, std.Serialize(player))
}
func getPlayer(playerName string) Player {
ctx := storage.GetReadOnlyContext()
data := storage.Get(ctx, playerName)
if data == nil {
panic("player not found")
}
return std.Deserialize(data.([]byte)).(Player)
}
func Balance(playerName string) int {
p := getPlayer(playerName)
return p.balance
}
func Items(playerName string) []int {
p := getPlayer(playerName)
return p.items
}
func BuyItem(playerName string, itemID int) {
player := getPlayer(playerName)
for i := range player.items {
if itemID == player.items[i] {
panic("item has already been purchased")
}
}
ctx := storage.GetContext()
storeURL := storage.Get(ctx, storeURLKey).(string)
filter := []byte("$.store.item[" + std.Itoa10(itemID) + "]")
oracle.Request(storeURL, filter, "cbBuyItem", playerName, 2*oracle.MinimumResponseGas)
}
func CbBuyItem(url string, userData any, code int, result []byte) {
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(oracle.Hash) {
panic("not called from the oracle contract")
}
if code != oracle.Success {
panic("request failed for " + url + " with code " + std.Itoa(code, 10))
}
runtime.Log("result for " + url + " is: " + string(result))
// Remove quotes that surround resulting JSON
resultLen := len(result)
data := std.JSONDeserialize(result[1 : resultLen-1]).(map[string]any)
price := data["price"].(int)
gearID := data["id"].(int)
playerName := userData.(string)
player := getPlayer(playerName)
if player.balance < price {
panic("insufficient balance")
}
player.balance -= price
player.items = append(player.items, gearID)
ctx := storage.GetContext()
storage.Put(ctx, playerName, std.Serialize(player))
}