add: gas transfer
This commit is contained in:
parent
5a6cd4bbb8
commit
9aa04d2d19
3 changed files with 49 additions and 15 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue