Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
feat: backend, get wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
hyacinthus committed Nov 16, 2024
1 parent e8deb6a commit b2d7036
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func Start(ctx context.Context) error {
app.Post("/function/trade", h.VAPIFunctionTrade)
app.Post("/function/confirm", h.VAPIFunctionConfirm)
app.Post("/function/sign", h.VAPIFunctionSign)
app.Post("/function/get-wallet", h.VAPIFunctionGetWallet)

app.Get("/chats/:id/state", h.GetChatState)

Expand Down
16 changes: 16 additions & 0 deletions backend/handler/vapi_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ func (h *Handler) VAPIFunctionSign(c *fiber.Ctx) error {

return c.Status(fiber.StatusOK).JSON(resp)
}

func (h *Handler) VAPIFunctionGetWallet(c *fiber.Ctx) error {
var msg = new(types.VapiServerMessageToolCall)
err := c.BodyParser(msg)
if err != nil {
h.log.Error("VAPIFunctionGetWallet", "error", err)
h.log.Error("VAPIFunctionGetWallet body", "body", string(c.Body()))
return types.NewError(fiber.StatusBadRequest, "BadRequest", err.Error())
}
resp, err := h.s.VAPIFunctionGetWallet(c.Context(), msg)
if err != nil {
return err
}

return c.Status(fiber.StatusOK).JSON(resp)
}
9 changes: 6 additions & 3 deletions backend/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ type Options struct {
}

type Service struct {
config *types.Config
log *slog.Logger
state sync.Map // call id -> state
config *types.Config
log *slog.Logger
state sync.Map // call id -> state
wallet string
balance sync.Map // symbol -> balance
}

func New(options Options) (*Service, error) {
Expand All @@ -33,5 +35,6 @@ func New(options Options) (*Service, error) {
return &Service{
config: options.Config,
log: log,
wallet: "0x1234567890",
}, nil
}
24 changes: 23 additions & 1 deletion backend/service/vapi_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func (s *Service) VAPIFunctionTrade(ctx context.Context, msg *types.VapiServerMe
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "origin_token_amount is required")
}
trade.OriginTokenAmount = amount.(float64)
trade.OriginTokenAmount, ok = amount.(float64)
if !ok {
s.log.Error("VAPIFunctionTrade", "error", "origin_token_amount is not float64")
return vapiToolResponse(id, "error, origin_token_amount is not float64"), nil
}
action, ok := tool.Function.Arguments["destination_token_symbol"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "destination_token_symbol is required")
Expand Down Expand Up @@ -157,6 +161,24 @@ func (s *Service) VAPIFunctionSign(ctx context.Context, msg *types.VapiServerMes
return resp, nil
}

func (s *Service) VAPIFunctionGetWallet(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.ToolResults, error) {
var id string
var resp = new(types.ToolResults)
resp.Results = make([]types.ToolResult, 0)
for _, tool := range msg.Message.ToolCallList {
if tool.Function != nil {
if tool.Function.Name == "get_wallet_address" {
id = tool.Id
break
}
}
}

s.log.Warn("VAPIFunction get wallet called")
vapiToolResponse(id, fmt.Sprintf("Your wallet is %s", s.wallet))
return resp, nil
}

func (s *Service) VAPIFunction(ctx context.Context, genericMessage map[string]interface{}) error {
// req, _ := json.Marshal(genericMessage)
// s.log.Info("VAPIFunctionTrade called", "message", req)
Expand Down

0 comments on commit b2d7036

Please sign in to comment.