Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeployContract implementation for Tezos connector #36

Merged
merged 5 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions internal/tezos/deploy_contract_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,59 @@ package tezos

import (
"context"
"errors"
"encoding/hex"
"encoding/json"

"blockwatch.cc/tzgo/codec"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/tezos"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-common/pkg/log"
"github.com/hyperledger/firefly-tezosconnect/internal/msgs"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
)

func (c *tezosConnector) DeployContractPrepare(_ context.Context, _ *ffcapi.ContractDeployPrepareRequest) (*ffcapi.TransactionPrepareResponse, ffcapi.ErrorReason, error) {
return nil, "", errors.New("contract deployment is not supported")
func (c *tezosConnector) DeployContractPrepare(ctx context.Context, req *ffcapi.ContractDeployPrepareRequest) (*ffcapi.TransactionPrepareResponse, ffcapi.ErrorReason, error) {
if req.Contract == nil {
return nil, ffcapi.ErrorReasonInvalidInputs, i18n.NewError(ctx, "Missing contract", req.Contract)
}

sc := asScript(req.Contract.String())
orig := &codec.Origination{
Script: sc,
}

addr, err := tezos.ParseAddress(req.From)
if err != nil {
return nil, ffcapi.ErrorReasonInvalidInputs, i18n.NewError(ctx, msgs.MsgInvalidFromAddress, req.From, err)
}

hash, _ := c.client.GetBlockHash(ctx, rpc.Head)
op := codec.NewOp().
WithContents(orig).
WithSource(addr).
WithBranch(hash)

err = c.completeOp(ctx, op, req.From, req.Nonce)
if err != nil {
return nil, ffcapi.ErrorReasonInvalidInputs, err
}
opts := &rpc.DefaultOptions
if reason, err := c.estimateAndAssignTxCost(ctx, op, opts); err != nil {
return nil, reason, err
}

log.L(ctx).Infof("Prepared deploy transaction dataLen=%d gas=%s", len(op.Bytes()), req.Gas.Int())

return &ffcapi.TransactionPrepareResponse{
Gas: req.Gas,
TransactionData: hex.EncodeToString(op.Bytes()),
}, "", nil
}

func asScript(s string) micheline.Script {
var sc micheline.Script
_ = json.Unmarshal([]byte(s), &sc)
return sc
}
125 changes: 121 additions & 4 deletions internal/tezos/deploy_contract_prepare_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,135 @@
package tezos

import (
"context"
"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/tezos"
"github.com/stretchr/testify/mock"
"testing"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
"github.com/stretchr/testify/assert"
)

func TestDeployContractPrepare(t *testing.T) {
_, c, _, done := newTestConnector(t)
ctx, c, mRPC, done := newTestConnector(t)
defer done()
mRPC.On("GetBlockHash", ctx, mock.Anything, mock.Anything).
Return(tezos.BlockHash{}, nil)
mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)
mRPC.On("Simulate", ctx, mock.Anything, mock.Anything).
Return(&rpc.Receipt{
Op: &rpc.Operation{
Contents: []rpc.TypedOperation{
rpc.Transaction{
Manager: rpc.Manager{
Generic: rpc.Generic{
Metadata: rpc.OperationMetadata{
Result: rpc.OperationResult{
Status: tezos.OpStatusApplied,
},
},
},
},
},
},
},
}, nil)

_, _, err := c.DeployContractPrepare(context.Background(), &ffcapi.ContractDeployPrepareRequest{})
resp, reason, err := c.DeployContractPrepare(ctx, &ffcapi.ContractDeployPrepareRequest{
Contract: fftypes.JSONAnyPtr("{\"code\":[{\"args\":[{\"prim\":\"string\"}],\"prim\":\"parameter\"},{\"args\":[{\"prim\":\"string\"}],\"prim\":\"storage\"},{\"args\":[[{\"prim\":\"CAR\"},{\"args\":[{\"prim\":\"operation\"}],\"prim\":\"NIL\"},{\"prim\":\"PAIR\"}]],\"prim\":\"code\"}],\"storage\":{\"string\":\"hello\"}}"),
})

assert.NotNil(t, resp)
assert.Equal(t, reason, ffcapi.ErrorReason(""))
assert.NoError(t, err)
}

func TestDeployContractPrepareGetContractExtError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()
mRPC.On("GetBlockHash", ctx, mock.Anything, mock.Anything).
Return(tezos.BlockHash{}, nil)
mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, assert.AnError)

resp, reason, err := c.DeployContractPrepare(ctx, &ffcapi.ContractDeployPrepareRequest{
Contract: fftypes.JSONAnyPtr("{\"code\":[{\"args\":[{\"prim\":\"string\"}],\"prim\":\"parameter\"},{\"args\":[{\"prim\":\"string\"}],\"prim\":\"storage\"},{\"args\":[[{\"prim\":\"CAR\"},{\"args\":[{\"prim\":\"operation\"}],\"prim\":\"NIL\"},{\"prim\":\"PAIR\"}]],\"prim\":\"code\"}],\"storage\":{\"string\":\"hello\"}}"),
})

assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs)
}

func TestDeployContractPrepareSimulateError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()
mRPC.On("GetBlockHash", ctx, mock.Anything, mock.Anything).
Return(tezos.BlockHash{}, nil)
mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)
mRPC.On("Simulate", ctx, mock.Anything, mock.Anything).
Return(&rpc.Receipt{
Op: &rpc.Operation{
Contents: []rpc.TypedOperation{
rpc.Transaction{
Manager: rpc.Manager{
Generic: rpc.Generic{
Metadata: rpc.OperationMetadata{
Result: rpc.OperationResult{
Status: tezos.OpStatusApplied,
},
},
},
},
},
},
},
}, assert.AnError)

resp, reason, err := c.DeployContractPrepare(ctx, &ffcapi.ContractDeployPrepareRequest{
Contract: fftypes.JSONAnyPtr("{\"code\":[{\"args\":[{\"prim\":\"string\"}],\"prim\":\"parameter\"},{\"args\":[{\"prim\":\"string\"}],\"prim\":\"storage\"},{\"args\":[[{\"prim\":\"CAR\"},{\"args\":[{\"prim\":\"operation\"}],\"prim\":\"NIL\"},{\"prim\":\"PAIR\"}]],\"prim\":\"code\"}],\"storage\":{\"string\":\"hello\"}}"),
})

assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, reason, ffcapi.ErrorReason(""))
}

func TestDeployContractPrepareMisingContractError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

resp, reason, err := c.DeployContractPrepare(ctx, &ffcapi.ContractDeployPrepareRequest{})

assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs)
}

func TestDeployContractPrepareParseAddressError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

resp, reason, err := c.DeployContractPrepare(ctx, &ffcapi.ContractDeployPrepareRequest{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "wrong",
},
Contract: fftypes.JSONAnyPtr("{\"code\":[{\"args\":[{\"prim\":\"string\"}],\"prim\":\"parameter\"},{\"args\":[{\"prim\":\"string\"}],\"prim\":\"storage\"},{\"args\":[[{\"prim\":\"CAR\"},{\"args\":[{\"prim\":\"operation\"}],\"prim\":\"NIL\"},{\"prim\":\"PAIR\"}]],\"prim\":\"code\"}],\"storage\":{\"string\":\"hello\"}}"),
})

assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, err.Error(), "contract deployment is not supported")
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs)
}
6 changes: 6 additions & 0 deletions internal/tezos/get_receipt.go
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this file doesn't have coverage TODO add this at next PR

Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func (c *tezosConnector) TransactionReceipt(ctx context.Context, req *ffcapi.Tra

operationReceipts = append(operationReceipts, extraInfo)
fullReceipt, _ = json.Marshal(operationReceipts)
} else if o.Kind() == tezos.OpTypeOrigination {
res := o.(*rpc.Origination).Result()
if len(res.OriginatedContracts) > 0 {
contractAddress := res.OriginatedContracts[0].ContractAddress()
receiptResponse.ContractLocation = fftypes.JSONAnyPtrBytes([]byte(contractAddress))
}
}
}

Expand Down
Loading