Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Semenyuk <[email protected]>
  • Loading branch information
alex-semenyuk committed Apr 1, 2024
1 parent 53aee26 commit 40c82b2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
15 changes: 5 additions & 10 deletions internal/tezos/deploy_contract_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func (c *tezosConnector) DeployContractPrepare(ctx context.Context, req *ffcapi.
return nil, ffcapi.ErrorReasonInvalidInputs, i18n.NewError(ctx, "Missing contract", req.Contract)
}

sc := asScript(req.Contract.String())
var sc micheline.Script
_ = json.Unmarshal([]byte(req.Contract.String()), &sc)
orig := &codec.Origination{
Script: sc,
}
Expand All @@ -30,11 +31,11 @@ func (c *tezosConnector) DeployContractPrepare(ctx context.Context, req *ffcapi.
return nil, ffcapi.ErrorReasonInvalidInputs, i18n.NewError(ctx, msgs.MsgInvalidFromAddress, req.From, err)
}

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

err = c.completeOp(ctx, op, req.From, req.Nonce)
if err != nil {
Expand All @@ -45,16 +46,10 @@ func (c *tezosConnector) DeployContractPrepare(ctx context.Context, req *ffcapi.
return nil, reason, err
}

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

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
}
53 changes: 48 additions & 5 deletions internal/tezos/get_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
)

const _address = "address"

type receiptExtraInfo struct {
ContractAddress *tezos.Address `json:"contractAddress"`
ConsumedGas *fftypes.FFBigInt `json:"consumedGas"`
Expand Down Expand Up @@ -91,7 +93,7 @@ func (c *tezosConnector) TransactionReceipt(ctx context.Context, req *ffcapi.Tra
var script *micheline.Script
if tx.Destination.IsContract() {
location, _ := json.Marshal(map[string]string{
"address": tx.Destination.String(),
_address: tx.Destination.String(),
})
receiptResponse.ContractLocation = fftypes.JSONAnyPtrBytes(location)
extraInfo.ContractAddress = &tx.Destination
Expand Down Expand Up @@ -121,11 +123,15 @@ 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))
result := o.(*rpc.Origination).Result()
originatedContracts := result.OriginatedContracts
if len(originatedContracts) > 0 {
location, _ := json.Marshal(map[string]string{
_address: originatedContracts[0].ContractAddress(),
})
receiptResponse.ContractLocation = fftypes.JSONAnyPtrBytes(location)
}
fullReceipt = c.extraInfoForDeployTransactionReceipt(ctx, result, operationReceipts)
}
}

Expand All @@ -134,3 +140,40 @@ func (c *tezosConnector) TransactionReceipt(ctx context.Context, req *ffcapi.Tra

return receiptResponse, "", nil
}

func (c *tezosConnector) extraInfoForDeployTransactionReceipt(ctx context.Context, res rpc.OperationResult, operationReceipts []receiptExtraInfo) []byte {
status := res.Status.String()
extraInfo := receiptExtraInfo{
ConsumedGas: fftypes.NewFFBigInt(res.ConsumedMilliGas / 1000),
StorageSize: fftypes.NewFFBigInt(res.StorageSize),
PaidStorageSizeDiff: fftypes.NewFFBigInt(res.PaidStorageSizeDiff),
Status: &status,
}

if len(res.Errors) > 0 {
errorMessage := ""
for _, err := range res.Errors {
errorMessage += err.Error()
}
extraInfo.ErrorMessage = &errorMessage
}

if prim := res.Storage; prim != nil {
val := micheline.NewValue(res.Storage.BuildType(), *prim)
m, err := val.Map()
if err != nil {
log.L(ctx).Error("error parsing contract storage: ", err)
}
storageBytes, _ := json.Marshal(m)
extraInfo.Storage = fftypes.JSONAnyPtrBytes(storageBytes)
}

if len(res.OriginatedContracts) > 0 {
extraInfo.ContractAddress = &res.OriginatedContracts[0]
}

operationReceipts = append(operationReceipts, extraInfo)
fullReceipt, _ := json.Marshal(operationReceipts)

return fullReceipt
}

0 comments on commit 40c82b2

Please sign in to comment.