Compare commits

...

1 commit

Author SHA1 Message Date
11f29be05f fix: updateRate logic split into two calls 2024-01-18 14:10:36 +03:00
6 changed files with 41 additions and 25 deletions

View file

@ -108,20 +108,21 @@ func GetComment(commentId string) Comment {
return std.Deserialize(comment.([]byte)).(Comment) return std.Deserialize(comment.([]byte)).(Comment)
} }
func Rate(commentId string, walletHashFrom interop.Hash160) { func Rate(commentId string, walletHashFrom interop.Hash160) bool {
ctx := storage.GetContext() ctx := storage.GetReadOnlyContext()
comment := GetComment(commentId) 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 { if !success {
panic("gas transfer failed") return false
} else { } else {
comment.likes += 1 return true
updateComment(comment)
} }
} }
func updateComment(comment Comment) { func IncreaseRate(commentId string) {
ctx := storage.GetContext() ctx := storage.GetContext()
comment := GetComment(commentId)
comments := GetByPostId(comment.postId) comments := GetByPostId(comment.postId)
for i := 0; i < len(comments); i++ { for i := 0; i < len(comments); i++ {
if comments[i].id == comment.id { if comments[i].id == comment.id {

View file

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

View file

@ -94,18 +94,26 @@ func GetAllPostsByUser(login string) []Post {
return postsByUser return postsByUser
} }
func Rate(postId string, walletHashFrom interop.Hash160) { func Rate(postId string, walletHashFrom interop.Hash160) bool {
ctx := storage.GetContext() ctx := storage.GetContext()
post := GetPost(postId) 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 { if !success {
panic("gas transfer failed") return false
} else { } else {
post.likes += 1 return true
storage.Put(ctx, post.id, std.Serialize(post))
} }
} }
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 { func getPostIndex(userId string) int {
ctx := storage.GetContext() ctx := storage.GetContext()
index := storage.Get(ctx, userId+lastIndex) index := storage.Get(ctx, userId+lastIndex)

View file

@ -1,5 +1,5 @@
name: Post name: "Post"
safemethods: [] safemethods: ["rate"]
supportedstandards: [] supportedstandards: []
events: events:
permissions: permissions:

View file

@ -43,9 +43,21 @@ func GetUser(login string) User {
return getUserTst(storage.GetReadOnlyContext(), login) 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() 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 { func getUserTst(ctx storage.Context, login string) User {

View file

@ -1,4 +1,4 @@
name: user name: "User"
safemethods: [] safemethods: []
supportedstandards: [] supportedstandards: []
events: [] events: []