Skip to content

Commit

Permalink
feature: RegisterName(), CreateContract()
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Nov 11, 2019
1 parent 25fec10 commit f6a1ed2
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
43 changes: 43 additions & 0 deletions aeternity/aens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package aeternity

import (
"github.com/aeternity/aepp-sdk-go/v6/account"
"github.com/aeternity/aepp-sdk-go/v6/config"
"github.com/aeternity/aepp-sdk-go/v6/naet"
"github.com/aeternity/aepp-sdk-go/v6/transactions"
)

// RegisterName allows one to easily register a name on AENS. It does the
// preclaim, transaction sending, confirmation and claim for you.
func RegisterName(n naet.NodeInterface, acc *account.Account, name string) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
status, err := n.GetStatus()
if err != nil {
return
}
networkID := *status.NetworkID
ttlNonceGetter := GenerateGetTTLNonce(GenerateGetTTL(n), GenerateGetNextNonce(n))
ttl, nonce, err := ttlNonceGetter(acc.Address, config.Client.TTL)
if err != nil {
return
}

cm, nameSalt, err := generateCommitmentID(name)
preclaimTx := transactions.NewNamePreclaimTx(acc.Address, cm, config.Client.Fee, ttl, nonce)
transactions.CalculateFee(preclaimTx)
_, _, _, _, _, err = SignBroadcastWaitTransaction(preclaimTx, acc, n.(*naet.Node), networkID, config.Client.WaitBlocks)
if err != nil {
return
}

ttl, nonce, err = ttlNonceGetter(acc.Address, config.Client.TTL)
if err != nil {
return
}
claimTx := transactions.NewNameClaimTx(acc.Address, name, nameSalt, config.Client.Fee, ttl, nonce)
transactions.CalculateFee(claimTx)
signedTxStr, hash, signature, blockHeight, blockHash, err = SignBroadcastWaitTransaction(claimTx, acc, n.(*naet.Node), networkID, config.Client.WaitBlocks)
if err != nil {
return
}
return
}
67 changes: 67 additions & 0 deletions aeternity/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package aeternity

import (
"errors"
"fmt"

"github.com/aeternity/aepp-sdk-go/v6/account"
"github.com/aeternity/aepp-sdk-go/v6/config"
"github.com/aeternity/aepp-sdk-go/v6/naet"
"github.com/aeternity/aepp-sdk-go/v6/transactions"
)

func findVMABIVersion(nodeVersion, compilerBackend string) (VMVersion, ABIVersion uint16, err error) {
if nodeVersion[0] == '5' && compilerBackend == "fate" {
return 5, 3, nil
} else if nodeVersion[0] == '5' && compilerBackend == "aevm" {
return 6, 1, nil
} else if nodeVersion[0] == '4' {
return 4, 1, nil
} else {
return 0, 0, errors.New("Other node versions unsupported")
}
}

type compileencoder interface {
naet.CompileContracter
naet.EncodeCalldataer
}

// CreateContract lets one deploy a contract with minimum fuss.
func CreateContract(n naet.NodeInterface, c compileencoder, acc *account.Account, source, function string, args []string, backend string) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
status, err := n.GetStatus()
if err != nil {
return
}
networkID := *status.NetworkID

bytecode, err := c.CompileContract(source, backend)
if err != nil {
return
}
calldata, err := c.EncodeCalldata(source, function, args, backend)
if err != nil {
return
}

VMVersion, ABIVersion, err := findVMABIVersion(*status.NodeVersion, backend)
if err != nil {
return
}
ttlNonceGetter := GenerateGetTTLNonce(GenerateGetTTL(n), GenerateGetNextNonce(n))
ttl, nonce, err := ttlNonceGetter(acc.Address, config.Client.TTL)
if err != nil {
return
}

createTx := transactions.NewContractCreateTx(acc.Address, nonce, bytecode, VMVersion, ABIVersion, config.Client.Contracts.Deposit, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, config.Client.Fee, ttl, calldata)
transactions.CalculateFee(createTx)
createTxStr, _ := transactions.SerializeTx(createTx)
fmt.Printf("%+v\n", createTx)
fmt.Println(createTxStr)
signedTxStr, hash, signature, blockHeight, blockHash, err = SignBroadcastWaitTransaction(createTx, acc, n.(*naet.Node), networkID, config.Client.WaitBlocks)
if err != nil {
return
}
return
}

0 comments on commit f6a1ed2

Please sign in to comment.