Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu committed Feb 8, 2024
1 parent 4983e48 commit 337a5c5
Showing 1 changed file with 32 additions and 38 deletions.
70 changes: 32 additions & 38 deletions service/service_construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ func (s ConstructionService) createUnwrapPayload(
}

func (s ConstructionService) createGenericContractCallPayload(req *types.ConstructionPayloadsRequest) (*ethtypes.Transaction, *transaction, *string, *types.Error) {
operationDescriptions, err := s.CreateGenericContractCallOperationDescription(req.Operations)
operationDescriptions, err := createGenericContractCallOperationDescription(req.Operations)
if err != nil {
return nil, nil, nil, WrapError(ErrInvalidInput, err.Error())
}
Expand Down Expand Up @@ -945,7 +945,7 @@ func (s ConstructionService) ConstructionPreprocess(
operationDescriptions []*parser.OperationDescription
preprocessOptions *options
err error
typesError *types.Error
terr *types.Error
)

switch {
Expand All @@ -954,29 +954,29 @@ func (s ConstructionService) ConstructionPreprocess(
if err != nil {
return nil, WrapError(ErrInvalidInput, err.Error())
}
preprocessOptions, typesError = s.createUnwrapPreprocessOptions(operationDescriptions, req)
if typesError != nil {
return nil, typesError
preprocessOptions, terr = createUnwrapPreprocessOptions(operationDescriptions, req)
if terr != nil {
return nil, terr
}
case isGenericContractCall(req.Metadata):
// To ensure we don't conflict with ERC-20 transfer handling (which are also contract calls), we populate
// the "method_signature" key in metadata (what is used in this check).
operationDescriptions, err = s.CreateGenericContractCallOperationDescription(req.Operations)
operationDescriptions, err = createGenericContractCallOperationDescription(req.Operations)
if err != nil {
return nil, WrapError(ErrInvalidInput, err.Error())
}
preprocessOptions, typesError = s.createGenericContractCallPreprocessOptions(operationDescriptions, req)
if typesError != nil {
return nil, typesError
preprocessOptions, terr = createGenericContractCallPreprocessOptions(operationDescriptions, req)
if terr != nil {
return nil, terr
}
default:
operationDescriptions, err = s.CreateTransferOperationDescription(req.Operations)
if err != nil {
return nil, WrapError(ErrInvalidInput, err.Error())
}
preprocessOptions, typesError = s.createTransferPreprocessOptions(operationDescriptions, req)
if typesError != nil {
return nil, typesError
preprocessOptions, terr = createTransferPreprocessOptions(operationDescriptions, req)
if terr != nil {
return nil, terr
}
}

Expand Down Expand Up @@ -1093,15 +1093,15 @@ func (s ConstructionService) CreateTransferOperationDescription(
}

if types.Hash(firstCurrency) == types.Hash(mapper.AvaxCurrency) {
return s.createOperationDescriptionTransfer(mapper.AvaxCurrency, mapper.OpCall), nil
return createOperationDescriptionTransfer(mapper.AvaxCurrency, mapper.OpCall), nil
}

// Not Native Avax, we require contractInfo in metadata.
if _, ok := firstCurrency.Metadata[mapper.ContractAddressMetadata].(string); !ok {
return nil, errors.New("non-native currency must have contractAddress in metadata")
}

return s.createOperationDescriptionTransfer(firstCurrency, mapper.OpErc20Transfer), nil
return createOperationDescriptionTransfer(firstCurrency, mapper.OpErc20Transfer), nil
}

func (s ConstructionService) CreateUnwrapOperationDescription(
Expand All @@ -1127,10 +1127,22 @@ func (s ConstructionService) CreateUnwrapOperationDescription(
return nil, errors.New("only configured bridge tokens may use try to use unwrap function")
}

return s.createOperationDescriptionBridgeUnwrap(firstCurrency), nil
return []*parser.OperationDescription{
{
Type: mapper.OpErc20Burn,
Account: &parser.AccountDescription{
Exists: true,
},
Amount: &parser.AmountDescription{
Exists: true,
Sign: parser.NegativeAmountSign,
Currency: firstCurrency,
},
},
}, nil
}

func (ConstructionService) createTransferPreprocessOptions(
func createTransferPreprocessOptions(
operationDescriptions []*parser.OperationDescription,
req *types.ConstructionPreprocessRequest,
) (*options, *types.Error) {
Expand Down Expand Up @@ -1168,7 +1180,7 @@ func (ConstructionService) createTransferPreprocessOptions(
}, nil
}

func (ConstructionService) createUnwrapPreprocessOptions(
func createUnwrapPreprocessOptions(
operationDescriptions []*parser.OperationDescription,
req *types.ConstructionPreprocessRequest,
) (*options, *types.Error) {
Expand Down Expand Up @@ -1207,7 +1219,7 @@ func (ConstructionService) createUnwrapPreprocessOptions(
}, nil
}

func (ConstructionService) createOperationDescriptionTransfer(
func createOperationDescriptionTransfer(
currency *types.Currency,
opCode string,
) []*parser.OperationDescription {
Expand Down Expand Up @@ -1237,24 +1249,6 @@ func (ConstructionService) createOperationDescriptionTransfer(
}
}

func (ConstructionService) createOperationDescriptionBridgeUnwrap(
currency *types.Currency,
) []*parser.OperationDescription {
return []*parser.OperationDescription{
{
Type: mapper.OpErc20Burn,
Account: &parser.AccountDescription{
Exists: true,
},
Amount: &parser.AmountDescription{
Exists: true,
Sign: parser.NegativeAmountSign,
Currency: currency,
},
},
}
}

func (s ConstructionService) getNativeTransferGasLimit(
ctx context.Context, toAddress string,
fromAddress string, value *big.Int,
Expand Down Expand Up @@ -1342,7 +1336,7 @@ func (s ConstructionService) getGenericContractCallGasLimit(
return gasLimit, nil
}

func (ConstructionService) createGenericContractCallPreprocessOptions(
func createGenericContractCallPreprocessOptions(
operationDescriptions []*parser.OperationDescription,
req *types.ConstructionPreprocessRequest,
) (*options, *types.Error) {
Expand Down Expand Up @@ -1398,7 +1392,7 @@ func (ConstructionService) createGenericContractCallPreprocessOptions(
}, nil
}

func (s ConstructionService) CreateGenericContractCallOperationDescription(operations []*types.Operation) ([]*parser.OperationDescription, error) {
func createGenericContractCallOperationDescription(operations []*types.Operation) ([]*parser.OperationDescription, error) {
if len(operations) != 2 {
return nil, errors.New("invalid number of operations")
}
Expand Down

0 comments on commit 337a5c5

Please sign in to comment.