Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -450,37 +450,37 @@ type fakeContractWriter struct {
cr *fakeContractReader
}

func (f *fakeContractWriter) SubmitTransaction(_ context.Context, contractName, method string, args any, transactionID string, toAddress string, meta *types.TxMeta, value *big.Int) error {
contractID := toAddress + "-" + contractName
switch method {
func (f *fakeContractWriter) SubmitTransaction(_ context.Context, req types.SubmitTransactionRequest) error {
contractID := req.ToAddress + "-" + req.ContractName
switch req.Method {
case MethodSettingStruct:
v, ok := args.(TestStruct)
v, ok := req.Args.(TestStruct)
if !ok {
return fmt.Errorf("unexpected type %T", args)
return fmt.Errorf("unexpected type %T", req.Args)
}
f.cr.SetTestStructLatestValue(contractID, &v)
case MethodSettingUint64:
v, ok := args.(PrimitiveArgs)
v, ok := req.Args.(PrimitiveArgs)
if !ok {
return fmt.Errorf("unexpected type %T", args)
return fmt.Errorf("unexpected type %T", req.Args)
}
f.cr.SetUintLatestValue(contractID, v.Value, ExpectedGetLatestValueArgs{})
case MethodTriggeringEvent:
if err := f.cr.triggers.RecordEvent(contractID, args, primitives.Unconfirmed, EventName); err != nil {
if err := f.cr.triggers.RecordEvent(contractID, req.Args, primitives.Unconfirmed, EventName); err != nil {
return fmt.Errorf("failed to record event: %w", err)
}
case MethodTriggeringEventWithDynamicTopic:
if err := f.cr.triggers.RecordEvent(contractID, args, primitives.Unconfirmed, DynamicTopicEventName); err != nil {
if err := f.cr.triggers.RecordEvent(contractID, req.Args, primitives.Unconfirmed, DynamicTopicEventName); err != nil {
return fmt.Errorf("failed to record event: %w", err)
}
case "batchContractWrite":
v, ok := args.(BatchCallEntry)
v, ok := req.Args.(BatchCallEntry)
if !ok {
return fmt.Errorf("unexpected type %T", args)
return fmt.Errorf("unexpected type %T", req.Args)
}
f.cr.SetBatchLatestValues(v)
default:
return fmt.Errorf("unsupported method: %s", method)
return fmt.Errorf("unsupported method: %s", req.Method)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ func WithClientEncoding(version codecpb.EncodingVersion) ClientOpt {
}
}

func (c *Client) SubmitTransaction(ctx context.Context, contractName, method string, params any, transactionID, toAddress string, meta *types.TxMeta, value *big.Int) error {
versionedParams, err := codecpb.EncodeVersionedBytes(params, c.encodeWith)
func (c *Client) SubmitTransaction(ctx context.Context, req types.SubmitTransactionRequest) error {
versionedParams, err := codecpb.EncodeVersionedBytes(req.Args, c.encodeWith)
if err != nil {
return err
}

req := pb.SubmitTransactionRequest{
ContractName: contractName,
Method: method,
pbReq := pb.SubmitTransactionRequest{
ContractName: req.ContractName,
Method: req.Method,
Params: versionedParams,
TransactionId: transactionID,
ToAddress: toAddress,
Meta: TxMetaToProto(meta),
Value: pb.NewBigIntFromInt(value),
TransactionId: req.TransactionID,
ToAddress: req.ToAddress,
Meta: TxMetaToProto(req.Meta),
Value: pb.NewBigIntFromInt(req.Value),
}

_, err = c.grpc.SubmitTransaction(ctx, &req)
_, err = c.grpc.SubmitTransaction(ctx, &pbReq)
if err != nil {
return net.WrapRPCErr(err)
}
Expand Down Expand Up @@ -153,7 +153,15 @@ func (s *Server) SubmitTransaction(ctx context.Context, req *pb.SubmitTransactio
return nil, err
}

err := s.impl.SubmitTransaction(ctx, req.ContractName, req.Method, params, req.TransactionId, req.ToAddress, TxMetaFromProto(req.Meta), req.Value.Int())
err := s.impl.SubmitTransaction(ctx, types.SubmitTransactionRequest{
ContractName: req.ContractName,
Method: req.Method,
Args: params,
TransactionID: req.TransactionId,
ToAddress: req.ToAddress,
Meta: TxMetaFromProto(req.Meta),
Value: req.Value.Int(),
})
if err != nil {
return nil, err
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/types/contract_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@ const (
// IdempotencyKey is generated by SubmitTransaction caller to prevent double sending or to track transaction status
type IdempotencyKey = string

// SubmitTransactionRequest contains parameters for submitting a transaction.
type SubmitTransactionRequest struct {
ContractName string
Method string
Args any
TransactionID IdempotencyKey
ToAddress string
Meta *TxMeta
Value *big.Int
}

type ContractWriter interface {
services.Service

// SubmitTransaction packs and broadcasts a transaction to the underlying chain contract.
//
// - `args` should be any object which maps a set of method param into the contract and method specific method params.
// - `transactionID` will be used by the underlying TXM as an idempotency key, and unique reference to track transaction attempts.
SubmitTransaction(ctx context.Context, contractName, method string, args any, transactionID IdempotencyKey, toAddress string, meta *TxMeta, value *big.Int) error
// - `req.Args` should be any object which maps a set of method param into the contract and method specific method params.
// - `req.TransactionID` will be used by the underlying TXM as an idempotency key, and unique reference to track transaction attempts.
SubmitTransaction(ctx context.Context, req SubmitTransactionRequest) error

// GetTransactionStatus returns the current status of a transaction in the underlying chain's TXM.
GetTransactionStatus(ctx context.Context, transactionID IdempotencyKey) (TransactionStatus, error)
Expand Down
16 changes: 14 additions & 2 deletions pkg/types/interfacetests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ func RunTestsInParallel[T TestingT[T]](t T, tester BasicTester[T], tests []Testc
func batchContractWrite[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], cw types.ContractWriter, boundContracts []types.BoundContract, batchCallEntry BatchCallEntry, mockRun bool) {
// This is necessary because the mock helper function requires the entire batchCallEntry rather than an individual testStruct
if mockRun {
err := cw.SubmitTransaction(t.Context(), AnyContractName, "batchContractWrite", batchCallEntry, "", "", nil, big.NewInt(0))
err := cw.SubmitTransaction(t.Context(), types.SubmitTransactionRequest{
ContractName: AnyContractName,
Method: "batchContractWrite",
Args: batchCallEntry,
Value: big.NewInt(0),
})
require.NoError(t, err)
return
}
Expand All @@ -115,7 +120,14 @@ func batchContractWrite[T TestingT[T]](t T, tester ChainComponentsInterfaceTeste
func SubmitTransactionToCW[T TestingT[T]](t T, tester ChainComponentsInterfaceTester[T], cw types.ContractWriter, method string, args any, contract types.BoundContract, status types.TransactionStatus) string {
tester.DirtyContracts()
txID := uuid.New().String()
err := cw.SubmitTransaction(t.Context(), contract.Name, method, args, txID, contract.Address, nil, big.NewInt(0))
err := cw.SubmitTransaction(t.Context(), types.SubmitTransactionRequest{
ContractName: contract.Name,
Method: method,
Args: args,
TransactionID: txID,
ToAddress: contract.Address,
Value: big.NewInt(0),
})
require.NoError(t, err)

err = WaitForTransactionStatus(t, tester, cw, txID, status, false)
Expand Down
Loading