-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example partner plugin mint v1 (#51)
- Loading branch information
Showing
3 changed files
with
127 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/book/v1/bookv1grpc" | ||
bookv1 "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/book/v1" | ||
typesv1 "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/types/v1" | ||
"github.com/chain4travel/camino-messenger-bot/internal/metadata" | ||
"google.golang.org/grpc" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
var _ bookv1grpc.MintServiceServer = (*MintServiceV1Server)(nil) | ||
|
||
type MintServiceV1Server struct{} | ||
|
||
func (*MintServiceV1Server) Mint(ctx context.Context, _ *bookv1.MintRequest) (*bookv1.MintResponse, error) { | ||
md := metadata.Metadata{} | ||
|
||
if err := md.ExtractMetadata(ctx); err != nil { | ||
log.Print("error extracting metadata") | ||
} | ||
|
||
md.Stamp(fmt.Sprintf("%s-%s", "ext-system", "response")) | ||
|
||
log.Printf("Responding to request (MintV1): %s", md.RequestID) | ||
|
||
response := bookv1.MintResponse{ | ||
MintId: &typesv1.UUID{Value: md.RequestID}, | ||
BuyableUntil: ×tamppb.Timestamp{ | ||
Seconds: time.Now().Add(5 * time.Minute).Unix(), | ||
}, | ||
Price: &typesv1.Price{ | ||
Value: "1", | ||
Decimals: 9, | ||
}, | ||
} | ||
|
||
log.Printf("CMAccount %s received request from CMAccount %s", md.Recipient, md.Sender) | ||
|
||
grpc.SendHeader(ctx, md.ToGrpcMD()) | ||
|
||
return &response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/book/v2/bookv2grpc" | ||
bookv2 "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/book/v2" | ||
typesv1 "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/types/v1" | ||
typesv2 "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/types/v2" | ||
"github.com/chain4travel/camino-messenger-bot/internal/metadata" | ||
"google.golang.org/grpc" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
var _ bookv2grpc.MintServiceServer = (*MintServiceV2Server)(nil) | ||
|
||
type MintServiceV2Server struct{} | ||
|
||
func (*MintServiceV2Server) Mint(ctx context.Context, _ *bookv2.MintRequest) (*bookv2.MintResponse, error) { | ||
md := metadata.Metadata{} | ||
|
||
if err := md.ExtractMetadata(ctx); err != nil { | ||
log.Print("error extracting metadata") | ||
} | ||
|
||
md.Stamp(fmt.Sprintf("%s-%s", "ext-system", "response")) | ||
|
||
log.Printf("Responding to request: %s (MintV2)", md.RequestID) | ||
|
||
// On-chain payment of 1 nCAM value=1 decimals=9 this currency has denominator 18 on | ||
// | ||
// Columbus and conclusively to mint the value of 1 nCam must be divided by 10^9 = | ||
// 0.000000001 CAM and minted in its smallest fraction by multiplying 0.000000001 * | ||
// 10^18 => 1000000000 aCAM | ||
response := bookv2.MintResponse{ | ||
MintId: &typesv1.UUID{Value: md.RequestID}, | ||
BuyableUntil: ×tamppb.Timestamp{ | ||
Seconds: time.Now().Add(5 * time.Minute).Unix(), | ||
}, | ||
// NATIVE TOKEN EXAMPLE: | ||
Price: &typesv2.Price{ | ||
Value: "12345", | ||
Decimals: 9, | ||
Currency: &typesv2.Currency{ | ||
Currency: &typesv2.Currency_NativeToken{ | ||
NativeToken: &emptypb.Empty{}, | ||
}, | ||
}, | ||
}, | ||
// ISO CURRENCY EXAMPLE: | ||
// Price: &typesv2.Price{ | ||
// Value: "10000", | ||
// Decimals: 2, | ||
// Currency: &typesv2.Currency{ | ||
// Currency: &typesv2.Currency_IsoCurrency{ | ||
// IsoCurrency: typesv2.IsoCurrency_ISO_CURRENCY_EUR, | ||
// }, | ||
// }, | ||
// }, | ||
BookingTokenId: uint64(123456), | ||
ValidationId: &typesv1.UUID{Value: "123456"}, | ||
BookingTokenUri: "https://example.com/booking-token", | ||
} | ||
|
||
log.Printf("CMAccount %s received request from CMAccount %s", md.Recipient, md.Sender) | ||
|
||
grpc.SendHeader(ctx, md.ToGrpcMD()) | ||
|
||
return &response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters