From 9aa04d2d199bf1ae049182fb5cb0d514b02841c9 Mon Sep 17 00:00:00 2001 From: maksaid Date: Wed, 17 Jan 2024 18:54:48 +0300 Subject: [PATCH] add: gas transfer --- Comment/comment.go | 41 ++++++++++++++++++++++++++++++++--------- Post/post_contract.go | 14 +++++++++----- User/user.go | 9 ++++++++- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/Comment/comment.go b/Comment/comment.go index 692d657..bc8b85b 100755 --- a/Comment/comment.go +++ b/Comment/comment.go @@ -1,7 +1,9 @@ package Comment import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/iterator" + "github.com/nspcc-dev/neo-go/pkg/interop/native/gas" "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" @@ -19,10 +21,25 @@ type Comment struct { const ( comment_prefix = "comment_" user_comment_count_prefix = "comment_count_" + gas_decimals = 1_0000_0000 + comment_hash = "comment_hash" ) func _deploy(data interface{}, isUpdate bool) { + if isUpdate { + return + } + + args := data.(struct { + commentHash interop.Hash160 + }) + + if len(args.commentHash) != interop.Hash160Len { + panic("invalid hash of player contrast") + } + ctx := storage.GetContext() + storage.Put(ctx, comment_hash, args.commentHash) storage.Put(ctx, "index_comment", "0") } @@ -102,26 +119,32 @@ func GetComment(commentId string) Comment { return std.Deserialize(comment.([]byte)).(Comment) } -func RateComment(isLike bool, postId string, commentId string) { +func Rate(commentId string, walletHashFrom interop.Hash160) { + ctx := storage.GetContext() comment := GetComment(commentId) - if isLike { - comment.likes++ + success := gas.Transfer(walletHashFrom, storage.Get(ctx, comment.userLogin+"_hash").(interop.Hash160), gas_decimals, nil) + if !success { + panic("gas transfer failed") } else { - comment.dislikes++ + comment.likes += 1 + updateComment(comment) } - - UpdateComment(comment, postId) } -func UpdateComment(comment Comment, postId string) { +func updateComment(comment Comment) { ctx := storage.GetContext() - comments := GetByPostId(postId) + comments := GetByPostId(comment.postId) for i := 0; i < len(comments); i++ { if comments[i].id == comment.id { comments[i] = comment } } - storage.Put(ctx, comment_prefix+postId, std.Serialize(comments)) + storage.Put(ctx, comment_prefix+comment.postId, std.Serialize(comments)) storage.Put(ctx, comment.id, std.Serialize(comment)) } + +func GetContractHash() interop.Hash160 { + ctx := storage.GetContext() + return storage.Get(ctx, comment_hash).(interop.Hash160) +} diff --git a/Post/post_contract.go b/Post/post_contract.go index 4755858..158e9f9 100755 --- a/Post/post_contract.go +++ b/Post/post_contract.go @@ -1,7 +1,9 @@ package Post import ( + "github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop/iterator" + "github.com/nspcc-dev/neo-go/pkg/interop/native/gas" "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" @@ -20,6 +22,7 @@ const ( lastIndex = "user_post_index" id = "current_post_id" comment_prefix = "comment_" + gas_decimals = 1_0000_0000 ) func NewPost(login string, text string, postName string) { @@ -93,15 +96,16 @@ func GetAllPostsByUser(login string) []Post { return postsByUser } -func RatePost(isLike bool, postId string) { +func Rate(postId string, walletHashFrom interop.Hash160) { ctx := storage.GetContext() post := GetPost(postId) - if isLike { - post.likes++ + success := gas.Transfer(walletHashFrom, storage.Get(ctx, post.login+"_hash").(interop.Hash160), gas_decimals, nil) + if !success { + panic("gas transfer failed") } else { - post.dislikes++ + post.likes += 1 + storage.Put(ctx, post.id, std.Serialize(post)) } - storage.Put(ctx, post.id, std.Serialize(post)) } func getPostIndex(userId string) int { diff --git a/User/user.go b/User/user.go index d375812..0a45368 100755 --- a/User/user.go +++ b/User/user.go @@ -2,6 +2,7 @@ package Users import ( "github.com/nspcc-dev/neo-go/pkg/interop" + "github.com/nspcc-dev/neo-go/pkg/interop/contract" "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" @@ -35,12 +36,18 @@ func NewUser(name string, surname string, login string, password string, owner i } saveUser(ctx, login, user) + } func GetUser(login string) User { return getUserTst(storage.GetReadOnlyContext(), login) } +func RateForGas(commentId string, contractHash interop.Hash160, login string) { + ctx := storage.GetContext() + contract.Call(contractHash, "rate", contract.ReadOnly, commentId, storage.Get(ctx, login+"_hash")) +} + func getUserTst(ctx storage.Context, login string) User { data := storage.Get(ctx, login) @@ -54,6 +61,6 @@ func getUserTst(ctx storage.Context, login string) User { func saveUser(ctx storage.Context, userLogin string, user User) { runtime.Log("User " + userLogin + " was created") - storage.Put(ctx, userLogin+lastIndex, -1) + storage.Put(ctx, userLogin+"_hash", user.ownerHash) storage.Put(ctx, userLogin, std.Serialize(user)) }