Skip to content

Commit

Permalink
initial work on transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Nov 7, 2019
1 parent 10f19a3 commit ab4e8e8
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
58 changes: 58 additions & 0 deletions account_create_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package hedera

import (
"fmt"

"github.com/hashgraph/hedera-sdk-go/hedera_proto"
)

type AccountCreateTransaction struct {
TransactionBuilder
client *Client
body hedera_proto.CryptoCreateTransactionBody
maxTransactionFee uint64
}

func NewAccountCreateTransaction(client *Client) AccountCreateTransaction {
return AccountCreateTransaction{
client: client,
body: hedera_proto.CryptoCreateTransactionBody{},
}
}

func (transaction AccountCreateTransaction) SetMaxTransactionFee(tinybar uint64) AccountCreateTransaction {
transaction.maxTransactionFee = tinybar

return transaction
}

func (transaction AccountCreateTransaction) SetKey(publicKey Ed25519PublicKey) AccountCreateTransaction {
// fixme: use our own built in function for this
protoKey := hedera_proto.Key_Ed25519{Ed25519: publicKey.keyData}

transaction.body.Key = &hedera_proto.Key{Key: &protoKey}
return transaction
}

func (transaction AccountCreateTransaction) SetInitialBalance(tinybar uint64) AccountCreateTransaction {
transaction.body.InitialBalance = tinybar

return transaction
}

func (transaction AccountCreateTransaction) validate() error {
if transaction.body.Key == nil {
return fmt.Errorf("AccountCreateTransaction requires .setKey")
}

return nil
}

func (transaction AccountCreateTransaction) Build() (Transaction, error) {
if err := transaction.validate(); err != nil {
return nil, err
}

// not implemented yet
return nil, nil
}
14 changes: 10 additions & 4 deletions examples/create_account/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"

"github.com/hashgraph/hedera-sdk-go"
Expand All @@ -9,7 +10,7 @@ import (
func main() {
client, err := hedera.NewClient(
// Node ID
hedera.AccountID { Account: 3 },
hedera.AccountID{Account: 3},
// Node Address
"0.testnet.hedera.com:50211",
)
Expand All @@ -26,18 +27,23 @@ func main() {

client.SetOperator(
// Operator Account ID
hedera.AccountID { Account: 2 },
hedera.AccountID{Account: 2},
// Operator Private Key
operatorPrivateKey,
)

newKey := hedera.NewEd25519PrivateKey()
newPublicKey := newKey.PublicKey()

tx := hedera.NewAccountCreateTransaction(client).
tx, err := hedera.NewAccountCreateTransaction(client).
SetKey(newPublicKey).
SetInitialBalance(1000).
SetMaxTransactionFee(10000000)
SetMaxTransactionFee(10000000).
Build()

if err != nil {
panic(err)
}

receipt, err := tx.ExecuteForReceipt()

Expand Down
25 changes: 25 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hedera

import "strings"

type ErrorTransactionValidation struct {
Messages []string
Err error
}

func (e *ErrorTransactionValidation) Error() string {
return "The following requirements were not met: \n" + strings.Join(e.Messages, "\n")
}

// fixme: this should probably be a struct with those functions implementat on it
type Transaction interface {
Execute() error
ExecuteForReceipt() (TransactionReceipt, error)
}

type TransactionBuilder interface {
SetMaxTransactionFee(uint64) *TransactionBuilder
SetMemo(string)
validate() ErrorTransactionValidation
Build() (Transaction, error)
}
41 changes: 41 additions & 0 deletions transaction_receipt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hedera

import (
"errors"

"github.com/hashgraph/hedera-sdk-go/hedera_proto"
)

var ErrNoReceipt = errors.New("response was not `TransactionGetReceipt`")

type TransactionReceipt struct {
inner *hedera_proto.TransactionReceipt
}

func TransactionReceiptFromResponse(response hedera_proto.Response) (*TransactionReceipt, error) {
transactionGetReceipt := response.GetTransactionGetReceipt()

if transactionGetReceipt == nil {
return nil, ErrNoReceipt
}

receipt := TransactionReceipt{
transactionGetReceipt.Receipt,
}

return &receipt, nil
}

func (transactionReceipt *TransactionReceipt) AccountID() *AccountID {
internalID := transactionReceipt.inner.AccountID

if internalID == nil {
return nil
}

return &AccountID{
uint64(internalID.ShardNum),
uint64(internalID.RealmNum),
uint64(internalID.AccountNum),
}
}

0 comments on commit ab4e8e8

Please sign in to comment.