-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: RegisterName(), CreateContract()
- Loading branch information
1 parent
25fec10
commit f6a1ed2
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |