Skip to content

Commit

Permalink
chore: added withdraw from specific windows function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikelle committed Jul 8, 2024
1 parent 586a9ac commit c49dda6
Show file tree
Hide file tree
Showing 7 changed files with 650 additions and 301 deletions.
701 changes: 400 additions & 301 deletions p2p/gen/go/bidderapi/v1/bidderapi.pb.go

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions p2p/gen/go/bidderapi/v1/bidderapi.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions p2p/gen/go/bidderapi/v1/bidderapi_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions p2p/gen/openapi/bidderapi/v1/bidderapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ paths:
required: false
type: string
format: uint64
/v1/bidder/withdraw_from_specific_windows:
post:
summary: WithdrawFromSpecificWindows
description: WithdrawFromSpecificWindows is called by the bidder node to withdraw funds from specific windows.
operationId: Bidder_WithdrawFromSpecificWindows
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/v1AutoDepositResponse'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/googlerpcStatus'
parameters:
- name: windowNumbers
description: Window numbers for withdrawing deposits.
in: query
required: true
type: array
items:
type: string
format: uint64
collectionFormat: multi
definitions:
bidderapiv1AutoDeposit:
type: object
Expand Down
65 changes: 65 additions & 0 deletions p2p/pkg/rpc/bidder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type BidderRegistryContract interface {
DepositForNWindows(*bind.TransactOpts, *big.Int, uint16) (*types.Transaction, error)
WithdrawBidderAmountFromWindow(*bind.TransactOpts, common.Address, *big.Int) (*types.Transaction, error)
GetDeposit(*bind.CallOpts, common.Address, *big.Int) (*big.Int, error)
WithdrawFromSpecificWindows(*bind.TransactOpts, []*big.Int) (*types.Transaction, error)
ParseBidderRegistered(types.Log) (*bidderregistry.BidderregistryBidderRegistered, error)
ParseBidderWithdrawal(types.Log) (*bidderregistry.BidderregistryBidderWithdrawal, error)
}
Expand Down Expand Up @@ -438,6 +439,70 @@ func (s *Service) CancelAndWithdrawAutoDeposit(
}, nil
}

func (s *Service) WithdrawFromSpecificWindows(
ctx context.Context,
r *bidderapiv1.WithdrawFromSpecificWindowsRequest,
) (*bidderapiv1.AutoDepositResponse, error) {
err := s.validator.Validate(r)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "validating withdraw from n windows request: %v", err)
}

if s.autoDepositTracker.IsWorking() {
return nil, status.Error(codes.FailedPrecondition, "auto deposit is already running, stop and then withdraw")
}

opts, err := s.optsGetter(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "getting transact opts: %v", err)
}

windows := make([]*big.Int, len(r.WindowNumbers))
for i, w := range r.WindowNumbers {
windows[i] = new(big.Int).SetUint64(w.Value)
}

tx, err := s.registryContract.WithdrawFromSpecificWindows(opts, windows)
if err != nil {
return nil, status.Errorf(codes.Internal, "withdrawing deposit: %v", err)
}

receipt, err := s.watcher.WaitForReceipt(ctx, tx)
if err != nil {
return nil, status.Errorf(codes.Internal, "waiting for receipt: %v", err)
}

if receipt.Status != types.ReceiptStatusSuccessful {
return nil, status.Errorf(codes.Internal, "receipt status: %v", receipt.Status)
}

var amountsAndWindows []*bidderapiv1.AutoDeposit
for _, log := range receipt.Logs {
if withdrawal, err := s.registryContract.ParseBidderWithdrawal(*log); err == nil {
s.logger.Info("withdrawal successful", "amount", withdrawal.Amount.String(), "window", withdrawal.Window.String())
amountsAndWindows = append(amountsAndWindows, &bidderapiv1.AutoDeposit{
Amount: withdrawal.Amount.String(),
WindowNumber: wrapperspb.UInt64(withdrawal.Window.Uint64()),
})
}
}

if len(amountsAndWindows) > 0 {
return &bidderapiv1.AutoDepositResponse{
AmountsAndWindowNumbers: amountsAndWindows,
}, nil
}

s.logger.Error(
"withdraw successful but missing log",
"txHash", receipt.TxHash.Hex(),
"window", r.WindowNumbers,
"logs", receipt.Logs,
)

return nil, status.Errorf(codes.Internal, "missing log for withdrawal")
}

func (s *Service) AutoDepositStatus(
ctx context.Context,
_ *bidderapiv1.EmptyMessage,
Expand Down
4 changes: 4 additions & 0 deletions p2p/pkg/rpc/bidder/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func (t *testRegistryContract) ParseBidderWithdrawal(_ types.Log) (*bidderregist
}, nil
}

func (t *testRegistryContract) WithdrawFromSpecificWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) {
return types.NewTransaction(3, common.Address{}, nil, 0, nil, nil), nil
}

type testAutoDepositTracker struct {
deposits map[uint64]bool
isWorking bool
Expand Down
26 changes: 26 additions & 0 deletions p2p/rpc/bidderapi/v1/bidderapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ service Bidder {
option (google.api.http) = {post: "/v1/bidder/cancel_and_withdraw_auto_deposit"};
}

// WithdrawFromSpecificWindows
//
// WithdrawFromSpecificWindows is called by the bidder node to withdraw funds from specific windows.
rpc WithdrawFromSpecificWindows(WithdrawFromSpecificWindowsRequest) returns (AutoDepositResponse) {
option (google.api.http) = {post: "/v1/bidder/withdraw_from_specific_windows"};
}

// GetDeposit
//
// GetDeposit is called by the bidder to get its deposit in the bidder registry.
Expand Down Expand Up @@ -207,6 +214,25 @@ message WithdrawResponse {
google.protobuf.UInt64Value window_number = 2;
};

message WithdrawFromSpecificWindowsRequest {
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_schema) = {
json_schema: {
title: "Withdraw from N windows request"
description: "Withdraw deposit from the bidder registry."
required: ["window_numbers"]
}
example: "{\"window_numbers\": [1, 2, 3]}"
};
repeated google.protobuf.UInt64Value window_numbers = 1 [
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
description: "Window numbers for withdrawing deposits."
}, (buf.validate.field).cel = {
id: "window_numbers",
message: "window_numbers must be a valid array of positive integers.",
expression: "this.all(r, r > 0) && size(this) > 0"
}];
};

message Bid {
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_schema) = {
json_schema: {
Expand Down

0 comments on commit c49dda6

Please sign in to comment.