add oracle for getting uuid

This commit is contained in:
RustamOper05 2024-01-14 21:28:44 +03:00
parent 94e9ea947c
commit 08b1b0a7ef
9 changed files with 70 additions and 39 deletions

View file

@ -1,8 +1,9 @@
package Comment
import (
guuid "github.com/google/uuid"
"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"
)
@ -17,8 +18,9 @@ type Comment struct {
func CreateNewComment(authorId string, postId string, text string) {
ctx := storage.GetContext()
newComment := Comment{
id: guuid.New().String(),
id: "none",
authorId: authorId,
postId: postId,
text: text,
@ -26,11 +28,32 @@ func CreateNewComment(authorId string, postId string, text string) {
dislikes: 0,
}
storeURL := "https://www.uuidgenerator.net/api/version7"
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
newComment.id = storage.Get(ctx, "lastCommentId").(string)
comments := GetByPostId(postId)
comments = append(comments, newComment)
storage.Put(ctx, postId+"_comment", std.Serialize(comments))
}
func cbGetUUID(url string, commentData 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))
runtime.Log("Last Comment id is: " + string(result))
ctx := storage.GetContext()
storage.Put(ctx, "lastCommentId", string(result))
}
func GetByPostId(postId string) []Comment {
ctx := storage.GetContext()
return std.Deserialize(storage.Get(ctx, postId+"_comment").([]byte)).([]Comment)
@ -80,4 +103,4 @@ func UpdateComment(comment Comment, postId string) {
}
storage.Put(ctx, postId+"_comment", std.Serialize(comments))
}
}

View file

@ -1 +0,0 @@
{"name":"comment","abi":{"methods":[{"name":"createNewComment","offset":0,"parameters":[{"name":"authorId","type":"String"},{"name":"postId","type":"String"},{"name":"text","type":"String"}],"returntype":"Void","safe":false},{"name":"getByAuthorId","offset":109,"parameters":[{"name":"postId","type":"String"},{"name":"authorId","type":"String"}],"returntype":"Array","safe":false},{"name":"getByPostId","offset":61,"parameters":[{"name":"postId","type":"String"}],"returntype":"Array","safe":false},{"name":"getComment","offset":157,"parameters":[{"name":"commentId","type":"String"},{"name":"postId","type":"String"}],"returntype":"Array","safe":false},{"name":"rateComment","offset":259,"parameters":[{"name":"isLike","type":"Boolean"},{"name":"postId","type":"String"},{"name":"commentId","type":"String"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}

Binary file not shown.

View file

@ -1,11 +0,0 @@
name: Post
sourceurl: http://example.com/
safemethods: []
supportedstandards: []
events:
- name: Hello world!
parameters:
- name: args
type: Array
permissions:
- methods: '*'

View file

@ -1,10 +1,9 @@
package Post
import (
"strconv"
guuid "github.com/google/uuid"
"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"
)
@ -23,13 +22,6 @@ const (
lastIndex = "_lastIndex"
)
/*
func _deploy(data interface{}, isUpdate bool, userId string) {
if isUpdate {
return
}
}
*/
func NewPost(authorId string, text string, postName string) {
ctx := storage.GetContext()
@ -40,22 +32,44 @@ func NewPost(authorId string, text string, postName string) {
likes: 0,
category: "none",
postName: postName,
id: guuid.New().String(),
id: "none",
}
storeURL := "https://www.uuidgenerator.net/api/version7"
oracle.Request(storeURL, nil, "cbGetUUID", nil, oracle.MinimumResponseGas)
post.id = storage.Get(ctx, "lastPostId").(string)
storage.Put(ctx, post.id, std.Serialize(post))
updatePostIndex(authorId)
storage.Put(ctx, authorId+strconv.Itoa(getPostIndex(authorId)), post.id)
lastPostIndex := storage.Get(ctx, authorId+lastIndex)
storage.Put(ctx, authorId+lastPostIndex.(string), post.id)
}
func CbGetUUID(url string, postData 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))
ctx := storage.GetContext()
storage.Put(ctx, "lastPostId", string(result))
}
func GetPost(postId string) Post {
ctx := storage.GetReadOnlyContext()
data := storage.Get(ctx, postId)
if data == nil {
panic("player not found")
panic("post not found")
}
return std.Deserialize(data.([]byte)).(Post)
}
func RatePost(isLike bool, postId string) {
@ -79,5 +93,4 @@ func updatePostIndex(userId string) {
ctx := storage.GetContext()
index := getPostIndex(userId)
storage.Put(ctx, userId+lastIndex, index+1)
}

6
Post/post_contract.yml Normal file
View file

@ -0,0 +1,6 @@
name: Post
safemethods: []
supportedstandards: []
events:
permissions:
- methods: ["request"]

View file

@ -3,6 +3,7 @@ package Users
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"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"
)
@ -14,6 +15,10 @@ type User struct {
ownerHash interop.Hash160
}
const (
lastIndex = "_lastIndex"
)
func NewUser(name string, surname string, login string, password string, owner interop.Hash160) {
ctx := storage.GetContext()
@ -44,7 +49,8 @@ func GetUser(ctx storage.Context, login string) User {
}
func saveUser(ctx storage.Context, userLogin string, user User) {
storage.Put(ctx, user.ownerHash, std.Serialize(user))
storage.Put(ctx, string(user.ownerHash)+"LastPost", -1)
storage.Put(ctx, string(user.ownerHash)+"LastCom", -1)
runtime.Log("User " + userLogin + " was created")
storage.Put(ctx, userLogin+lastIndex, -1)
storage.Put(ctx, userLogin, std.Serialize(user))
}

5
go.mod
View file

@ -2,7 +2,4 @@ module blog
go 1.21.5
require (
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502
github.com/rs/xid v1.5.0
)
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502

2
go.sum
View file

@ -1,4 +1,2 @@
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502 h1:HwR9fWkdJXCbsTnrb2Rm92xkDGdEM0VHvZV+7XJUXM0=
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231219060339-de98b39a9502/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=