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)
}
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 {

View file

@ -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: "*"

View file

@ -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)

View file

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

View file

@ -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 {

View file

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