Skip to content

Commit

Permalink
KLC-124 Asset actions (#5)
Browse files Browse the repository at this point in the history
* Asset Trigger

* create KDA

* broadcast tx

* wallet from PEM file

* hasher/marshalizer tools

* calculate TX Hash

* Update account

* update demos
  • Loading branch information
fbsobreira authored Oct 24, 2022
1 parent 948465e commit 01c5691
Show file tree
Hide file tree
Showing 30 changed files with 1,136 additions and 37 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
*.pem*
.DS_Store
*.env*
bin/
vendor
.idea
.vscode

.vscode
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# KKKKKKKKK KKKKKKKLLLLLLLLLLL EEEEEEEEEEEEEEEEEEEEEEVVVVVVVV VVVVVVVVEEEEEEEEEEEEEEEEEEEEEERRRRRRRRRRRRRRRRR
# K:::::::K K:::::KL:::::::::L E::::::::::::::::::::EV::::::V V::::::VE::::::::::::::::::::ER::::::::::::::::R
# K:::::::K K:::::KL:::::::::L E::::::::::::::::::::EV::::::V V::::::VE::::::::::::::::::::ER::::::RRRRRR:::::R
# K:::::::K K::::::KLL:::::::LL EE::::::EEEEEEEEE::::EV::::::V V::::::VEE::::::EEEEEEEEE::::ERR:::::R R:::::R
# KK::::::K K:::::KKK L:::::L E:::::E EEEEEE V:::::V V:::::V E:::::E EEEEEE R::::R R:::::R
# K:::::K K:::::K L:::::L E:::::E V:::::V V:::::V E:::::E R::::R R:::::R
# K::::::K:::::K L:::::L E::::::EEEEEEEEEE V:::::V V:::::V E::::::EEEEEEEEEE R::::RRRRRR:::::R
# K:::::::::::K L:::::L E:::::::::::::::E V:::::V V:::::V E:::::::::::::::E R:::::::::::::RR
# K:::::::::::K L:::::L E:::::::::::::::E V:::::V V:::::V E:::::::::::::::E R::::RRRRRR:::::R
# K::::::K:::::K L:::::L E::::::EEEEEEEEEE V:::::V V:::::V E::::::EEEEEEEEEE R::::R R:::::R
# K:::::K K:::::K L:::::L E:::::E V:::::V:::::V E:::::E R::::R R:::::R
# KK::::::K K:::::KKK L:::::L LLLLLL E:::::E EEEEEE V:::::::::V E:::::E EEEEEE R::::R R:::::R
# K:::::::K K::::::KLL:::::::LLLLLLLLL:::::LEE::::::EEEEEEEE:::::E V:::::::V EE::::::EEEEEEEE:::::ERR:::::R R:::::R
# K:::::::K K:::::KL::::::::::::::::::::::LE::::::::::::::::::::E V:::::V E::::::::::::::::::::ER::::::R R:::::R
# K:::::::K K:::::KL::::::::::::::::::::::LE::::::::::::::::::::E V:::V E::::::::::::::::::::ER::::::R R:::::R
# KKKKKKKKK KKKKKKKLLLLLLLLLLLLLLLLLLLLLLLLEEEEEEEEEEEEEEEEEEEEEE VVV EEEEEEEEEEEEEEEEEEEEEERRRRRRRR RRRRRRR


# LOAD builder info
ifndef VERSION
SHELL := /bin/bash
VERSION := $(shell git describe --always --long --dirty --tag)
endif
ldflags := -X 'main.appVersion=${VERSION}'

GOCMD=go
GORUN=$(GOCMD) run -ldflags="$(ldflags)"
GOBUILD=$(GOCMD) build -ldflags="$(ldflags)"


############################
### DEMO APP ###
############################
.PHONY: demo-getAccount demo-transfer demo-createAsset
demo.%: DEMO=$*
demo.%:
$(GORUN) ./cmd/demo/$(DEMO)
77 changes: 77 additions & 0 deletions cmd/demo/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package demo

import (
"os"
"path/filepath"
"time"

"github.com/klever-io/klever-go-sdk/core/account"
"github.com/klever-io/klever-go-sdk/core/wallet"
"github.com/klever-io/klever-go-sdk/provider"
"github.com/klever-io/klever-go-sdk/provider/network"
"github.com/klever-io/klever-go-sdk/provider/utils"
)

func InitWallets() (
accounts []account.Account,
wallets []wallet.Wallet,
kc provider.KleverChain,
err error,
) {

wallets = make([]wallet.Wallet, 0)

net := network.NewNetworkConfig(network.TestNet)
httpClient := utils.NewHttpClient(10 * time.Second)
kc, err = provider.NewKleverChain(net, httpClient)
if err != nil {
return
}

// load account from pemfile
files, err := GetWallets(".", "*.pem")
if err != nil {
return
}
for _, f := range files {
var w wallet.Wallet
w, err = wallet.NewWalletFromPEM(f)
if err != nil {
return
}
wallets = append(wallets, w)

var acc account.Account
acc, err = w.GetAccount()
if err != nil {
return
}
err = acc.Sync(kc)

accounts = append(accounts, acc)
}

return
}

func GetWallets(root, pattern string) ([]string, error) {
var matches []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
matches = append(matches, path)
}
return nil
})
if err != nil {
return nil, err
}
return matches, nil
}
48 changes: 48 additions & 0 deletions cmd/demo/createAsset/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"

"github.com/klever-io/klever-go-sdk/cmd/demo"
"github.com/klever-io/klever-go-sdk/models"
)

func main() {

accounts, wallets, kc, err := demo.InitWallets()
if err != nil {
panic(err)
}

base := accounts[0].NewBaseTX()
tx, err := kc.CreateKDA(
base,
models.KDAData_Fungible,
&models.KDAOptions{
Name: "KleverTest",
Ticker: "TST",
Precision: 4,
MaxSupply: 1000,
InitialSupply: 10,
AddRolesMint: []string{accounts[0].Address().Bech32(), accounts[1].Address().Bech32()},
Properties: models.PropertiesInfo{
CanMint: true, CanBurn: true,
},
URIs: map[string]string{"explorer": "testnet.kleverscan.org"},
})
if err != nil {
panic(err)
}

err = tx.Sign(wallets[0])
if err != nil {
panic(err)
}

hash, err := tx.Broadcast(kc)
if err != nil {
panic(err)
}

fmt.Println("TxHash: ", hash)
}
4 changes: 2 additions & 2 deletions cmd/demo/main.go → cmd/demo/getaccount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (

func main() {

net := network.NewNetworkConfig(network.MainNet)
net := network.NewNetworkConfig(network.TestNet)
httpClient := utils.NewHttpClient(10 * time.Second)
kc, err := provider.NewKleverChain(net, httpClient)
if err != nil {
panic(err)
}

acc, err := kc.GetAccount("klv1usdnywjhrlv4tcyu6stxpl6yvhplg35nepljlt4y5r7yppe8er4qujlazy")
acc, err := kc.GetAccount("klv1mt8yw657z6nk9002pccmwql8w90k0ac6340cjqkvm9e7lu0z2wjqudt69s")
if err != nil {
panic(err)
}
Expand Down
33 changes: 33 additions & 0 deletions cmd/demo/transfer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"

"github.com/klever-io/klever-go-sdk/cmd/demo"
)

func main() {

accounts, wallets, kc, err := demo.InitWallets()
if err != nil {
panic(err)
}

base := accounts[0].NewBaseTX()
tx, err := kc.Send(base, accounts[1].Address().Bech32(), 1, "")
if err != nil {
panic(err)
}

err = tx.Sign(wallets[0])
if err != nil {
panic(err)
}

hash, err := tx.Broadcast(kc)
if err != nil {
panic(err)
}

fmt.Println("TxHash: ", hash)
}
45 changes: 37 additions & 8 deletions core/account/account.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
package account

import (
"time"

"github.com/klever-io/klever-go-sdk/core/address"
"github.com/klever-io/klever-go-sdk/models"
"github.com/klever-io/klever-go-sdk/provider"
)

type account struct {
address address.Address
balance int64
nonce uint64
info *models.Account

lastUpdate time.Time
}

func NewAccount(addr address.Address) (Account, error) {

return &account{address: addr, balance: 0, nonce: 0}, nil
return &account{address: addr, info: &models.Account{}}, nil
}

func (a *account) Address() address.Address {
return a.address
}

func (a *account) Balance() int64 {
return a.balance
if a.info != nil {
return a.info.Balance
}

return 0
}

func (a *account) Nonce() uint64 {
return a.nonce
if a.info != nil {
return a.info.Nonce
}

return 0
}

func (a *account) IncrementNonce() {
a.nonce += 1
a.info.Nonce += 1
}

func (a *account) Sync(p provider.KleverChain) error {
Expand All @@ -38,8 +50,25 @@ func (a *account) Sync(p provider.KleverChain) error {
return err
}

a.balance = acc.Balance
a.nonce = acc.Nonce
a.info = acc
a.lastUpdate = time.Now()

return nil
}

func (a *account) LastUpdate() time.Time {
return a.lastUpdate
}

func (a *account) GetInfo() *models.Account {
return a.info
}

func (a *account) NewBaseTX() *models.BaseTX {
return &models.BaseTX{
FromAddress: a.address.Bech32(),
Nonce: a.Nonce(),
PermID: 0, // Default owner
Message: make([]string, 0),
}
}
6 changes: 6 additions & 0 deletions core/account/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package account

import (
"time"

"github.com/klever-io/klever-go-sdk/core/address"
"github.com/klever-io/klever-go-sdk/models"
"github.com/klever-io/klever-go-sdk/provider"
)

Expand All @@ -11,4 +14,7 @@ type Account interface {
Nonce() uint64
IncrementNonce()
Sync(provider.KleverChain) error
LastUpdate() time.Time
GetInfo() *models.Account
NewBaseTX() *models.BaseTX
}
1 change: 1 addition & 0 deletions core/wallet/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Wallet interface {
PrivateKey() []byte
PublicKey() []byte
GetAccount() (account.Account, error)
Sign(msg []byte) ([]byte, error)
}
Loading

0 comments on commit 01c5691

Please sign in to comment.