From 11f29be05f4f631937a4f872cbbd1daf9625168d Mon Sep 17 00:00:00 2001 From: maksaid Date: Thu, 18 Jan 2024 14:10:36 +0300 Subject: [PATCH] fix: updateRate logic split into two calls --- Comment/comment.go | 15 ++++++++------- Comment/comment.yml | 11 +++-------- Post/post_contract.go | 18 +++++++++++++----- Post/post_contract.yml | 4 ++-- User/user.go | 16 ++++++++++++++-- User/user.yml | 2 +- 6 files changed, 41 insertions(+), 25 deletions(-) diff --git a/Comment/comment.go b/Comment/comment.go index 99ee329..65348b9 100755 --- a/Comment/comment.go +++ b/Comment/comment.go @@ -108,20 +108,21 @@ func GetComment(commentId string) Comment { return std.Deserialize(comment.([]byte)).(Comment) } -func Rate(commentId string, walletHashFrom interop.Hash160) { - ctx := storage.GetContext() +func Rate(commentId string, walletHashFrom interop.Hash160) bool { + ctx := storage.GetReadOnlyContext() comment := GetComment(commentId) - success := gas.Transfer(walletHashFrom, storage.Get(ctx, comment.userLogin+"_hash").(interop.Hash160), gas_decimals, nil) + recipient := storage.Get(ctx, comment.userLogin+"_hash").(interop.Hash160) + success := gas.Transfer(walletHashFrom, recipient, gas_decimals, nil) if !success { - panic("gas transfer failed") + return false } else { - comment.likes += 1 - updateComment(comment) + return true } } -func updateComment(comment Comment) { +func IncreaseRate(commentId string) { ctx := storage.GetContext() + comment := GetComment(commentId) comments := GetByPostId(comment.postId) for i := 0; i < len(comments); i++ { if comments[i].id == comment.id { diff --git a/Comment/comment.yml b/Comment/comment.yml index 0553d67..3a549f7 100755 --- a/Comment/comment.yml +++ b/Comment/comment.yml @@ -1,11 +1,6 @@ -name: comment -sourceurl: http://example.com/ -safemethods: [] +name: "Comment" +safemethods: ["rate"] supportedstandards: [] events: - - name: Hello world! - parameters: - - name: args - type: Array permissions: - - methods: '*' + - methods: "*" diff --git a/Post/post_contract.go b/Post/post_contract.go index 186f70e..174eef6 100755 --- a/Post/post_contract.go +++ b/Post/post_contract.go @@ -94,18 +94,26 @@ func GetAllPostsByUser(login string) []Post { return postsByUser } -func Rate(postId string, walletHashFrom interop.Hash160) { +func Rate(postId string, walletHashFrom interop.Hash160) bool { ctx := storage.GetContext() post := GetPost(postId) - success := gas.Transfer(walletHashFrom, storage.Get(ctx, post.login+"_hash").(interop.Hash160), gas_decimals, nil) + recipient := storage.Get(ctx, post.login+"_hash").(interop.Hash160) + success := gas.Transfer(walletHashFrom, recipient, 1, nil) if !success { - panic("gas transfer failed") + return false } else { - post.likes += 1 - storage.Put(ctx, post.id, std.Serialize(post)) + return true } } +func IncreaseRate(postId string) { + ctx := storage.GetContext() + post := GetPost(postId) + post.likes += 1 + storage.Put(ctx, post.id, std.Serialize(post)) + +} + func getPostIndex(userId string) int { ctx := storage.GetContext() index := storage.Get(ctx, userId+lastIndex) diff --git a/Post/post_contract.yml b/Post/post_contract.yml index 00af16e..f6705ea 100644 --- a/Post/post_contract.yml +++ b/Post/post_contract.yml @@ -1,5 +1,5 @@ -name: Post -safemethods: [] +name: "Post" +safemethods: ["rate"] supportedstandards: [] events: permissions: diff --git a/User/user.go b/User/user.go index 0a45368..1b5e17f 100755 --- a/User/user.go +++ b/User/user.go @@ -43,9 +43,21 @@ func GetUser(login string) User { return getUserTst(storage.GetReadOnlyContext(), login) } -func RateForGas(commentId string, contractHash interop.Hash160, login string) { +/* +method used to invoke gas transfer in comment/post contracts +entityId - id of comment or post +contractHash - hash of the comment/post contract +login - login of user, who wants to rate comment/post +*/ + +func RateForGas(entityId string, contractHash interop.Hash160, login string) { ctx := storage.GetContext() - contract.Call(contractHash, "rate", contract.ReadOnly, commentId, storage.Get(ctx, login+"_hash")) + success := contract.Call(contractHash, "rate", contract.All, entityId, storage.Get(ctx, login+"_hash").(interop.Hash160)).(bool) + if success { + contract.Call(contractHash, "increaseRate", contract.All, entityId) + } else { + panic("something gone wrong with transfering gas") + } } func getUserTst(ctx storage.Context, login string) User { diff --git a/User/user.yml b/User/user.yml index 1fe695c..3b5ee9c 100755 --- a/User/user.yml +++ b/User/user.yml @@ -1,4 +1,4 @@ -name: user +name: "User" safemethods: [] supportedstandards: [] events: [] \ No newline at end of file