Skip to content

Commit

Permalink
refactor: AENS higher level TestRegisterName uses Broadcaster; Create…
Browse files Browse the repository at this point in the history
…Contract returns ctID; CallContract introduced
  • Loading branch information
randomshinichi committed Jan 21, 2020
1 parent 9cf81a8 commit 6b8280f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
25 changes: 24 additions & 1 deletion aeternity/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type compileencoder interface {
}

// CreateContract lets one deploy a contract with minimum fuss.
func CreateContract(n nodeStatusHeightAccounterBroadcaster, c compileencoder, acc *account.Account, source, function string, args []string, backend string) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
func CreateContract(n nodeStatusHeightAccounterBroadcaster, c compileencoder, acc *account.Account, source, function string, args []string, backend string) (ctID, signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
networkID, err := getNetworkID(n)
if err != nil {
return
Expand Down Expand Up @@ -65,5 +65,28 @@ func CreateContract(n nodeStatusHeightAccounterBroadcaster, c compileencoder, ac
if err != nil {
return
}
ctID, err = createTx.ContractID()
return
}

// CallContract calls a smart contract's function, automatically calling the
// compiler to transform the arguments into bytecode.
func CallContract(n nodeStatusHeightAccounterBroadcaster, c compileencoder, acc *account.Account, ctID, source, function string, args []string, backend string) (signedTxStr, hash, signature string, blockHeight uint64, blockHash string, err error) {
networkID, err := getNetworkID(n)
if err != nil {
return
}
_, _, ttlnoncer := transactions.GenerateTTLNoncer(n)

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

callTx, err := transactions.NewContractCallTx(acc.Address, ctID, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, config.Client.Contracts.ABIVersion, callData, ttlnoncer)
if err != nil {
return
}

return SignBroadcastWaitTransaction(callTx, acc, n, networkID, config.Client.WaitBlocks)
}
6 changes: 5 additions & 1 deletion integration_test/aens_higherlevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ func TestRegisterName(t *testing.T) {
n := setupNetwork(t, privatenetURL, false)
alice, _ := setupAccounts(t)

broadcaster, err := aeternity.NewBroadcaster(alice, n)
if err != nil {
t.Fatal(err)
}
name := "somelongnamefdsafdffsa.chain"
nameFee := transactions.CalculateMinNameFee(name)
_, _, _, _, _, err := aeternity.RegisterName(n, alice, name, nameFee)
_, _, _, _, _, err = aeternity.RegisterName(n, broadcaster, name, nameFee)
if err != nil {
t.Error(err)
}
Expand Down
25 changes: 24 additions & 1 deletion integration_test/contract_higherlevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,30 @@ contract SimpleStorage =
function get() : int = state.data
stateful function set(value : int) = put(state{data = value})`

_, _, _, _, _, err := aeternity.CreateContract(n, c, alice, simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
_, _, _, _, _, _, err := aeternity.CreateContract(n, c, alice, simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
if err != nil {
t.Error(err)
}
}

func TestCallContract(t *testing.T) {
n := setupNetwork(t, privatenetURL, false)
c := naet.NewCompiler("http://localhost:3080", false)
alice, _ := setupAccounts(t)

simplestorage := `
contract SimpleStorage =
record state = { data : int }
entrypoint init(value : int) : state = { data = value }
function get() : int = state.data
stateful function set(value : int) = put(state{data = value})`

ctID, _, _, _, _, _, err := aeternity.CreateContract(n, c, alice, simplestorage, "init", []string{"42"}, config.CompilerBackendFATE)
if err != nil {
t.Fatal(err)
}

_, _, _, _, _, err = aeternity.CallContract(n, c, alice, ctID, simplestorage, "get", []string{}, config.CompilerBackendFATE)
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 6b8280f

Please sign in to comment.