Skip to content

Commit

Permalink
[API] complete flow transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
myth committed May 16, 2024
1 parent 46f8fd4 commit 5d4e587
Show file tree
Hide file tree
Showing 58 changed files with 1,813 additions and 413 deletions.
6 changes: 5 additions & 1 deletion api/protos/common/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ enum TopicEvent {
TOPIC_EVENT_NONE = 0;
TOPIC_EVENT_APPROVAL = 1;
TOPIC_EVENT_TRANSFER = 2;
TOPIC_EVENT_SEND_TRANSACTION = 3;
TOPIC_EVENT_SEND_MY_TOKEN_TRANSACTION = 3;
TOPIC_EVENT_SEND_NATIVE_TOKEN_TRANSACTION = 4;
}

message User {
string username = 1;
string id = 2;

string wallet_address = 3;
string nonce = 4;
}

message Approval {
Expand Down
27 changes: 22 additions & 5 deletions api/protos/contract/contract_reader.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ service ContractReaderService {
rpc RetrieveBalanceOf(RetrieveBalanceOfRequest)
returns (RetrieveBalanceOfResponse) {
option (google.api.http) = {
get : "/v1/addresses/{address}/balances"
get : "/v1/balances"
};
}

rpc SendTransaction(SendTransactionRequest)
returns (SendTransactionResponse) {
option (google.api.http) = {
post : "/v1/transactions"
post : "/v1/transfers"
body : "*"
};
}

rpc SendTransactionV2(SendTransactionV2Request)
returns (SendTransactionResponse) {
option (google.api.http) = {
post : "/v2/transfers"
body : "*"
};
}
Expand All @@ -57,13 +65,22 @@ message RetrieveLatestBlockResponse {
int64 timestamp = 6;
}

message RetrieveBalanceOfRequest { string address = 1; }
message RetrieveBalanceOfResponse { uint64 balance = 1; }
message RetrieveBalanceOfRequest {}
message RetrieveBalanceOfResponse {
uint64 balance = 1;
uint64 native_balance = 2;
}

message SendTransactionRequest {
string priv_key = 1;
string to = 2;
string amount = 3;
}

message SendTransactionResponse {}
message SendTransactionResponse {}

message SendTransactionV2Request {
string signature = 1;
string to = 2;
string amount = 3;
}
1 change: 1 addition & 0 deletions api/protos/user/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ message RegisterRequest {
message RegisterResponse {
string private_key = 1;
string public_key = 2;
string wallet_address = 3;
}

message LoginRequest {
Expand Down
12 changes: 11 additions & 1 deletion api/protos/user/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import "google/api/annotations.proto";
import "common/common.proto";

service UserService {
// internal rpc
rpc GetUserPrivateKeyByID(GetUserPrivateKeyByIDRequest)
returns (GetUserPrivateKeyByIDResponse);

rpc GetUserByID(GetUserByIDRequest) returns (GetUserByIDResponse) {
option (google.api.http) = {
get : "/v1/users/{user_id}"
Expand Down Expand Up @@ -55,4 +59,10 @@ message GetListUserResponse {

message UpdateUserRequest { common.User user = 1; }

message UpdateUserResponse { bool success = 1; }
message UpdateUserResponse { bool success = 1; }

message GetUserPrivateKeyByIDRequest { string user_id = 1; }
message GetUserPrivateKeyByIDResponse {
string private_key = 1;
string nonce = 2;
}
18 changes: 17 additions & 1 deletion cmd/srv/contract_reader/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"github.com/google/uuid"

pb "openmyth/blockchain/idl/pb/contract"
userPb "openmyth/blockchain/idl/pb/user"
"openmyth/blockchain/internal/contract/repositories"
"openmyth/blockchain/internal/contract/repositories/eth"
"openmyth/blockchain/internal/contract/repositories/mongo"
"openmyth/blockchain/internal/contract/services"
"openmyth/blockchain/pkg/eth_client"
"openmyth/blockchain/pkg/grpc_client"
"openmyth/blockchain/pkg/grpc_server"
"openmyth/blockchain/pkg/iface/processor"
"openmyth/blockchain/pkg/iface/pubsub"
Expand All @@ -26,6 +28,9 @@ type Server struct {
approvalRepo repositories.ApprovalRepository
transferRepo repositories.TransferRepository
blockchainRepo repositories.BlockchainRepository
myTokenRepo repositories.MyTokenRepository

userClient userPb.UserServiceClient

contractReaderService pb.ContractReaderServiceServer

Expand All @@ -38,6 +43,15 @@ func NewServer() *Server {
}
}

func (s *Server) loadClients() {
userConn := grpc_client.NewGrpcClient(s.service.Cfg.UserService)

s.userClient = userPb.NewUserServiceClient(userConn)

s.service.WithFactories(userConn) // contractReaderConn,

}

func (s *Server) loadDatabases() {
s.mongoClient = mongoclient.NewMongoClient(s.service.Cfg.MongoDB.Address())

Expand All @@ -63,14 +77,15 @@ func (s *Server) loadRepositories() {
s.approvalRepo = mongo.NewApprovalRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
s.transferRepo = mongo.NewTransferRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
s.blockchainRepo = eth.NewBlockchainRepository(s.ethClient)
s.myTokenRepo = eth.NewMyTokenRepository(s.ethClient, s.ethClient, s.service.Cfg.ContractAddress)
}

// loadServices initializes the contract reader service with the necessary repositories and publisher.
//
// No parameters.
// No return value.
func (s *Server) loadServices() {
s.contractReaderService = services.NewContractReaderService(s.approvalRepo, s.transferRepo, s.blockchainRepo, s.publisher)
s.contractReaderService = services.NewContractReaderService(s.approvalRepo, s.transferRepo, s.blockchainRepo, s.myTokenRepo, s.userClient, s.publisher)
}

// loadServer initializes the gRPC server for the Contract Reader Service.
Expand All @@ -95,6 +110,7 @@ func (s *Server) Run(ctx context.Context) {

s.loadDatabases()
s.loadEthClient(ctx)
s.loadClients()
s.loadPublisher()
s.loadRepositories()
s.loadServices()
Expand Down
21 changes: 14 additions & 7 deletions cmd/srv/contract_writer/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ type Server struct {

subscriber pubsub.Subscriber

approvalRepo repositories.ApprovalRepository
transferRepo repositories.TransferRepository
myTokenRepo repositories.MyTokenRepository
approvalRepo repositories.ApprovalRepository
transferRepo repositories.TransferRepository
myTokenRepo repositories.MyTokenRepository
blockchainRepo repositories.BlockchainRepository

contractWriter *services.ContractWriterService

Expand All @@ -45,7 +46,7 @@ func (s *Server) loadDatabases() {
s.service.WithFactories(s.mongoClient)
}

func (s *Server) loadEthClient(ctx context.Context) {
func (s *Server) loadEthClient(_ context.Context) {
cfg := s.service.Cfg

s.ethClient = eth_client.NewDialClient(cfg.ETHClient.Address())
Expand All @@ -57,11 +58,12 @@ func (s *Server) loadEthClient(ctx context.Context) {
func (s *Server) loadRepositories() {
s.approvalRepo = mongo.NewApprovalRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
s.transferRepo = mongo.NewTransferRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
s.myTokenRepo = eth.NewMyTokenRepository(s.ethClient, s.wsEthClient, s.service.Cfg.PrivateKey)
s.myTokenRepo = eth.NewMyTokenRepository(s.ethClient, s.wsEthClient, s.service.Cfg.ContractAddress)
s.blockchainRepo = eth.NewBlockchainRepository(s.ethClient)
}

func (s *Server) loadServices() {
s.contractWriter = services.NewContractWriterService(s.approvalRepo, s.transferRepo, s.myTokenRepo)
s.contractWriter = services.NewContractWriterService(s.approvalRepo, s.transferRepo, s.myTokenRepo, s.blockchainRepo)
}

func (s *Server) loadSubscriber() {
Expand All @@ -71,13 +73,18 @@ func (s *Server) loadSubscriber() {
[]string{
pb.TopicEvent_TOPIC_EVENT_APPROVAL.String(),
pb.TopicEvent_TOPIC_EVENT_TRANSFER.String(),
pb.TopicEvent_TOPIC_EVENT_SEND_TRANSACTION.String(),
pb.TopicEvent_TOPIC_EVENT_SEND_MY_TOKEN_TRANSACTION.String(),
pb.TopicEvent_TOPIC_EVENT_SEND_NATIVE_TOKEN_TRANSACTION.String(),
}, s.contractWriter.Subscribe,
)

s.service.WithProcessors(s.subscriber)
}

// Run runs the server with the provided context.
//
// ctx: the context.Context for the server.
// No return value.
func (s *Server) Run(ctx context.Context) {
s.service.LoadLogger()
s.service.LoadConfig()
Expand Down
2 changes: 1 addition & 1 deletion cmd/srv/frontend/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *Server) loadServer() {
port := s.service.Cfg.Frontend.Port
handler := http.FileServer(http.Dir("/html"))
log.Printf("server listening in port: %v", port)
if err := http.ListenAndServe(port, handler); err != nil {
if err := http.ListenAndServe(s.service.Cfg.Frontend.Address(), handler); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
Expand Down
21 changes: 17 additions & 4 deletions cmd/srv/user/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package user

import (
"context"
"log"

"github.com/google/uuid"

pb "openmyth/blockchain/idl/pb/user"
"openmyth/blockchain/internal/user-mgnt/repositories"
"openmyth/blockchain/internal/user-mgnt/repositories/mongo"
"openmyth/blockchain/internal/user-mgnt/services"
"openmyth/blockchain/pkg/grpc_server"
"openmyth/blockchain/pkg/iface/processor"
"openmyth/blockchain/pkg/iface/pubsub"
"openmyth/blockchain/pkg/kafka"
mongoclient "openmyth/blockchain/pkg/mongo_client"
)

type Server struct {
mongoClient *mongoclient.MongoClient

publisher pubsub.Publisher

service *processor.Service

userRepo repositories.UserRepository
Expand All @@ -35,7 +40,6 @@ func NewServer() *Server {
// No parameters.
// No return value.
func (s *Server) loadDatabases() {
log.Println("s.service.Cfg.MongoDB.Address()", s.service.Cfg.MongoDB.Address())
s.mongoClient = mongoclient.NewMongoClient(s.service.Cfg.MongoDB.Address())

s.service.WithFactories(s.mongoClient)
Expand All @@ -48,9 +52,17 @@ func (s *Server) loadDatabases() {
func (s *Server) loadRepositories() {
s.userRepo = mongo.NewUserRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
}

func (s *Server) loadPublisher() {
clientID := uuid.NewString()
s.publisher = kafka.NewPublisher(clientID, s.service.Cfg.Kafka.Address())

s.service.WithFactories(s.publisher)
}

func (s *Server) loadServices() {
s.userService = services.NewUserService()
s.authService = services.NewAuthService(s.userRepo)
s.userService = services.NewUserService(s.userRepo)
s.authService = services.NewAuthService(s.userRepo, s.publisher, s.service.Cfg.PrivateKey)
}

func (s *Server) loadServer() {
Expand All @@ -68,6 +80,7 @@ func (s *Server) Run(ctx context.Context) {

s.loadDatabases()
s.loadRepositories()
s.loadPublisher()
s.loadServices()
s.loadServer()

Expand Down
2 changes: 0 additions & 2 deletions cmd/srv/watcher/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package watcher

import (
"context"
"log"

"github.com/google/uuid"

Expand Down Expand Up @@ -49,7 +48,6 @@ func (s *Server) loadPublisher() {
//
// It sets the myTokenRepo for the server with a new MyTokenRepository based on the ethClient, wsClient, and ContractAddress from the service configuration.
func (s *Server) loadRepositories() {
log.Print("s.service.Cfg.ContractAddress", s.service.Cfg.ContractAddress)
s.myTokenRepo = eth.NewMyTokenRepository(s.ethClient, s.wsClient, s.service.Cfg.ContractAddress)
}

Expand Down
2 changes: 1 addition & 1 deletion config/common/config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
contract_address: 0x71F6d3C2843185174BCEDaB98Eed5ce06F71852E
contract_address: 0x061cB8BAc25A9DC48d667c7441F23C5fe8704693
private_key: 822394ad1caf8ec4bf4e9406a6952f62ec6b787d2086b9c45c8b2f0e44ba5dfc
3 changes: 3 additions & 0 deletions config/contract_reader/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ ws_eth_client:
contract_reader_service:
host: ""
port: "5002"
user_service:
host: "user"
port: "5001"
mongo_db:
schema: mongodb
host: mongodb
Expand Down
3 changes: 3 additions & 0 deletions config/frontend/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
frontend:
host: ""
port: "9999"
5 changes: 4 additions & 1 deletion config/user/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ mongo_db:
schema: mongodb
host: mongodb
port: "27017"
database: user
database: user
kafka:
host: kafka
port: "9092"
3 changes: 3 additions & 0 deletions developments/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ COPY . .
RUN go mod download
RUN go build -o app-exe

FROM golang:1.22.0-alpine AS main-stage
COPY --from=build-stage /app/app-exe /app/app-exe

ENTRYPOINT [ "/app/app-exe" ]

4 changes: 2 additions & 2 deletions developments/docker-compose.all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
# - user
# - contract_reader
volumes:
- "../config/gateway:/app/config"
- "../config/frontend:/app/config"
- "../config/common:/app/common"
- "../html:/html"

Expand All @@ -30,7 +30,7 @@ services:
volumes:
- "../config/gateway:/app/config"
- "../config/common:/app/common"
- "../html:/html"

# user service
user:
image: openmyth/blockchain
Expand Down
Loading

0 comments on commit 5d4e587

Please sign in to comment.