Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/StringPay.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface StringPayload {
name: string;
assetName: string;
collection?: string;
currency: string;
price: number;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/StringPay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createServices, type Services } from "./services";

export interface StringPayload {
name: string;
assetName: string;
collection?: string;
currency: string;
price: number;
Expand Down
61 changes: 33 additions & 28 deletions src/lib/services/apiClient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ export function createApiClient({ baseUrl, apiKey }: ApiClientOptions): ApiClien
}
}

async function getQuote(payload: QuoteRequestPayload) {
async function getQuote(payload: TransactionRequest) {
try {
const request = () => httpClient.post(`/quotes`, payload);
const { data } = await authInterceptor<{ data: TransactPayload }>(request);
const { data } = await authInterceptor<{ data: Quote }>(request);

return data;
} catch (e: any) {
Expand All @@ -157,9 +157,9 @@ export function createApiClient({ baseUrl, apiKey }: ApiClientOptions): ApiClien
}
}

async function transact(transactPayload: TransactPayload) {
async function transact(payload: ExecutionRequest) {
try {
const request = () => httpClient.post(`/transactions`, transactPayload);
const request = () => httpClient.post(`/transactions`, payload);
const { data } = await authInterceptor<{
data: TransactionResponse;
}>(request);
Expand Down Expand Up @@ -225,8 +225,8 @@ export interface ApiClient {
refreshToken: (walletAddress: string) => Promise<AuthResponse>;
logoutUser: () => Promise<void>;
getUserStatus: (userId: string) => Promise<{ status: string }>;
getQuote: (payload: QuoteRequestPayload) => Promise<TransactPayload>;
transact: (quote: TransactPayload) => Promise<TransactionResponse>;
getQuote: (request: TransactionRequest) => Promise<Quote>;
transact: (request: ExecutionRequest) => Promise<TransactionResponse>;
setWalletAddress: (walletAddress: string) => void;
}

Expand Down Expand Up @@ -272,44 +272,49 @@ export interface VisitorData {
requestId?: string;
}

export interface Quote {
export interface TransactionRequest {
userAddress: string;
assetName: string;
chainID: number;
contractAddress: string;
contractFunction: string;
contractReturn: string;
contractParameters: string[];
txValue: string;
gasLimit: string;
}

export interface Estimate {
timestamp: number;
baseUSD: string;
gasUSD: string;
tokenUSD: string;
serviceUSD: string;
totalUSD: string;
}

export interface Quote {
transactionRequest: TransactionRequest;
estimate: Estimate;
signature: string;
}

export interface TransactPayload extends Quote {
userAddress: string;
chainID: number;
contractAddress: string;
contractFunction: string;
contractReturn: string;
contractParameters: string[];
txValue: string;
gasLimit: string;
cardToken: string;
export interface PaymentInfo {
cardToken?: string;
cardId?: string;
cvv?: string;
}

export interface ExecutionRequest {
quote: Quote;
paymentInfo: PaymentInfo;
}

export interface TransactionResponse {
txID: string;
txUrl: string;
}

export interface QuoteRequestPayload {
chainID: number;
userAddress: string;
contractAddress: string;
contractFunction: string;
contractReturn: string;
contractParameters: string[];
txValue: string;
gasLimit: string;
}

export interface ApiClientOptions {
baseUrl: string;
apiKey: string;
Expand Down
15 changes: 9 additions & 6 deletions src/lib/services/events.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StringPay, StringPayload } from "../StringPay";
import type { ApiClient, QuoteRequestPayload, TransactPayload, User, UserUpdate } from "./apiClient.service";
import type { ApiClient, ExecutionRequest, TransactionRequest, Quote, PaymentInfo, User, UserUpdate } from "./apiClient.service";
import type { AuthService } from "./auth.service";
import type { LocationService } from "./location.service";
import type { QuoteService } from "./quote.service";
Expand Down Expand Up @@ -188,9 +188,9 @@ export function createEventsService(iframeUrl: string, authService: AuthService,
async function onQuoteStart(event: StringEvent, { frame, payload }: StringPay) {
if (!frame) throw new Error("Iframe not ready");

const quotePayload = <QuoteRequestPayload>payload;
const quotePayload = <TransactionRequest>payload;

const callback = (quote: TransactPayload) => sendEvent(frame, Events.QUOTE_CHANGED, { quote });
const callback = (quote: Quote) => sendEvent(frame, Events.QUOTE_CHANGED, { quote });
quoteService.startQuote(quotePayload, callback);
}

Expand All @@ -202,7 +202,10 @@ export function createEventsService(iframeUrl: string, authService: AuthService,
if (!frame) throw new Error("Iframe not ready");

try {
const data = <TransactPayload>event.data;
let paymentInfo = <PaymentInfo>{};
paymentInfo.cardToken = event.data.cardToken;
let data = <ExecutionRequest>event.data;
data.paymentInfo = paymentInfo;
const txHash = await apiClient.transact(data);
sendEvent(frame, Events.RECEIVE_CONFIRM_TRANSACTION, txHash);
} catch (error: any) {
Expand Down Expand Up @@ -233,7 +236,7 @@ export function createEventsService(iframeUrl: string, authService: AuthService,
// Parse payload before sending it to the iframe
function createIframePayload(payload: StringPayload, _user: User | null): IframePayload {
const nft: NFT = {
name: payload.name,
assetName: payload.assetName,
price: payload.price,
currency: payload.currency,
collection: payload.collection ?? "",
Expand Down Expand Up @@ -263,7 +266,7 @@ export interface StringEvent {
}

export interface NFT {
name: string;
assetName: string;
price: number;
currency: string;
collection?: string;
Expand Down
10 changes: 5 additions & 5 deletions src/lib/services/quote.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ApiClient, QuoteRequestPayload, TransactPayload } from './apiClient.service';
import type { ApiClient, TransactionRequest, Quote } from './apiClient.service';

export function createQuoteService(apiClient: ApiClient): QuoteService {
let interval: NodeJS.Timer | undefined;

async function startQuote(payload: QuoteRequestPayload, callback: (payload: TransactPayload) => void) {
async function startQuote(payload: TransactionRequest, callback: (payload: Quote) => void) {
refreshQuote(payload, callback);

if (interval) {
Expand All @@ -18,7 +18,7 @@ export function createQuoteService(apiClient: ApiClient): QuoteService {
interval = undefined;
}

async function refreshQuote(payload: QuoteRequestPayload, callback: (payload: TransactPayload) => void) {
async function refreshQuote(payload: TransactionRequest, callback: (payload: Quote) => void) {
try {
const quote = await apiClient.getQuote(payload);
callback(quote);
Expand All @@ -35,7 +35,7 @@ export function createQuoteService(apiClient: ApiClient): QuoteService {
}

export interface QuoteService {
startQuote: (payload: QuoteRequestPayload, callback: (payload: TransactPayload) => void) => Promise<void>;
startQuote: (payload: TransactionRequest, callback: (payload: Quote) => void) => Promise<void>;
stopQuote: () => void;
refreshQuote: (payload: QuoteRequestPayload, callback: (payload: TransactPayload) => void) => Promise<void>;
refreshQuote: (payload: TransactionRequest, callback: (payload: Quote) => void) => Promise<void>;
}
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const STR_NFT_SRC = `${IPFS_GATEWAY}${IPFS_CID}/Demo_Character_1.png`

$: payload = {
name: "String Test NFT [AVAX]",
assetName: "String Test NFT [AVAX]",
collection: "String Demo",
imageSrc: STR_NFT_SRC,
imageAlt: "String NFT",
Expand Down
2 changes: 1 addition & 1 deletion src/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { StringPayload } from '../lib/StringPay';
const userAddr = "0x000000"

export const testPayload: StringPayload = {
name: "String Demo NFT",
assetName: "String Demo NFT",
collection: "String Demo",
imageSrc: "https://gateway.pinata.cloud/ipfs/bafybeibtmy26mac47n5pp6srds76h74riqs76erw24p5yvdhmwu7pxlcx4/STR_Logo_1.png",
imageAlt: "NFT",
Expand Down