Merge pull request 'add: gas transfer' (#15) from gas_transfer into master
Reviewed-on: https://codeberg.org/NaMe2te/Blog/pulls/15
This commit is contained in:
commit
5abd38c4e6
3 changed files with 49 additions and 15 deletions
|
@ -1,7 +1,9 @@
|
||||||
package Comment
|
package Comment
|
||||||
|
|
||||||
import (
|
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/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/native/std"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
|
@ -19,10 +21,25 @@ type Comment struct {
|
||||||
const (
|
const (
|
||||||
comment_prefix = "comment_"
|
comment_prefix = "comment_"
|
||||||
user_comment_count_prefix = "comment_count_"
|
user_comment_count_prefix = "comment_count_"
|
||||||
|
gas_decimals = 1_0000_0000
|
||||||
|
comment_hash = "comment_hash"
|
||||||
)
|
)
|
||||||
|
|
||||||
func _deploy(data interface{}, isUpdate bool) {
|
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()
|
ctx := storage.GetContext()
|
||||||
|
storage.Put(ctx, comment_hash, args.commentHash)
|
||||||
storage.Put(ctx, "index_comment", "0")
|
storage.Put(ctx, "index_comment", "0")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,26 +119,32 @@ func GetComment(commentId string) Comment {
|
||||||
return std.Deserialize(comment.([]byte)).(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)
|
comment := GetComment(commentId)
|
||||||
if isLike {
|
success := gas.Transfer(walletHashFrom, storage.Get(ctx, comment.userLogin+"_hash").(interop.Hash160), gas_decimals, nil)
|
||||||
comment.likes++
|
if !success {
|
||||||
|
panic("gas transfer failed")
|
||||||
} else {
|
} else {
|
||||||
comment.dislikes++
|
comment.likes += 1
|
||||||
|
updateComment(comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateComment(comment, postId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateComment(comment Comment, postId string) {
|
func updateComment(comment Comment) {
|
||||||
ctx := storage.GetContext()
|
ctx := storage.GetContext()
|
||||||
comments := GetByPostId(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 {
|
||||||
comments[i] = comment
|
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))
|
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
|
package Post
|
||||||
|
|
||||||
import (
|
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/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/native/std"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||||
|
@ -20,6 +22,7 @@ const (
|
||||||
lastIndex = "user_post_index"
|
lastIndex = "user_post_index"
|
||||||
id = "current_post_id"
|
id = "current_post_id"
|
||||||
comment_prefix = "comment_"
|
comment_prefix = "comment_"
|
||||||
|
gas_decimals = 1_0000_0000
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPost(login string, text string, postName string) {
|
func NewPost(login string, text string, postName string) {
|
||||||
|
@ -93,15 +96,16 @@ func GetAllPostsByUser(login string) []Post {
|
||||||
return postsByUser
|
return postsByUser
|
||||||
}
|
}
|
||||||
|
|
||||||
func RatePost(isLike bool, postId string) {
|
func Rate(postId string, walletHashFrom interop.Hash160) {
|
||||||
ctx := storage.GetContext()
|
ctx := storage.GetContext()
|
||||||
post := GetPost(postId)
|
post := GetPost(postId)
|
||||||
if isLike {
|
success := gas.Transfer(walletHashFrom, storage.Get(ctx, post.login+"_hash").(interop.Hash160), gas_decimals, nil)
|
||||||
post.likes++
|
if !success {
|
||||||
|
panic("gas transfer failed")
|
||||||
} else {
|
} 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 {
|
func getPostIndex(userId string) int {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package Users
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop"
|
"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/native/std"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"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)
|
saveUser(ctx, login, user)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUser(login string) User {
|
func GetUser(login string) User {
|
||||||
return getUserTst(storage.GetReadOnlyContext(), login)
|
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 {
|
func getUserTst(ctx storage.Context, login string) User {
|
||||||
data := storage.Get(ctx, login)
|
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) {
|
func saveUser(ctx storage.Context, userLogin string, user User) {
|
||||||
runtime.Log("User " + userLogin + " was created")
|
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))
|
storage.Put(ctx, userLogin, std.Serialize(user))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue