Skip to content

Commit

Permalink
chore: tests script for fedimint-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 21, 2024
1 parent e58fa2e commit fad3ff4
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 207 deletions.
222 changes: 120 additions & 102 deletions wrappers/fedimint-ts/FedimintClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import type {
} from "./types/common";
import type {
AwaitInvoiceRequest,
AwaitLnPayRequest,
Gateway,
LnInvoiceRequest,
LnInvoiceResponse,
LnPayRequest,
LnPayResponse,
SwitchGatewayRequest,
} from "./types/modules/ln";
import type {
CombineRequest,
Expand Down Expand Up @@ -92,7 +90,7 @@ class FedimintClient {
private activeFederationId: string;

constructor(baseUrl: string, password: string, activeFederationId: string) {
this.baseUrl = baseUrl + "/fedimint/v2";
this.baseUrl = baseUrl + "/v2";
this.password = password;
this.activeFederationId = activeFederationId;
}
Expand Down Expand Up @@ -211,22 +209,22 @@ class FedimintClient {
* Returns an array of federation IDs that the client is now connected to
*/
public async join(
inviteCode: string,
setDefault: boolean
inviteCode: string
): FedimintResponse<FederationIdsResponse> {
return await this.post<FederationIdsResponse>("/admin/join", {
inviteCode,
setDefault,
});
}

/**
* Outputs a list of operations that have been performed on the federation
*/
public async listOperations(
request: ListOperationsRequest,
limit: number,
federationId?: string
): FedimintResponse<OperationOutput[]> {
const request: ListOperationsRequest = { limit };

return await this.postWithId<OperationOutput[]>(
"/admin/list-operations",
request,
Expand All @@ -242,170 +240,190 @@ class FedimintClient {
* Creates a lightning invoice to receive payment via gateway
*/
createInvoice: async (
request: LnInvoiceRequest,
amountMsat: number,
description: string,
expiryTime?: number,
federationId?: string
): FedimintResponse<LnInvoiceResponse> =>
await this.postWithId<LnInvoiceResponse>(
): FedimintResponse<LnInvoiceResponse> => {
const request: LnInvoiceRequest = { amountMsat, description, expiryTime };

return await this.postWithId<LnInvoiceResponse>(
"/ln/invoice",
request,
federationId
),
);
},

/**
* Waits for a lightning invoice to be paid
*/
awaitInvoice: async (
request: AwaitInvoiceRequest,
operationId: string,
federationId?: string
): FedimintResponse<InfoResponse> =>
await this.postWithId<InfoResponse>(
): FedimintResponse<InfoResponse> => {
const request: AwaitInvoiceRequest = { operationId };

return await this.postWithId<InfoResponse>(
"/ln/await-invoice",
request,
federationId
),
);
},

/**
* Pays a lightning invoice or lnurl via a gateway
*/
pay: async (
request: LnPayRequest,
paymentInfo: string,
amountMsat?: number,
lnurlComment?: string,
federationId?: string
): FedimintResponse<LnPayResponse> =>
await this.postWithId<LnPayResponse>("/ln/pay", request, federationId),

/**
* Waits for a lightning payment to complete
*/
awaitPay: async (
request: AwaitLnPayRequest,
federationId?: string
): FedimintResponse<LnPayResponse> =>
await this.postWithId<LnPayResponse>(
"/ln/await-pay",
): FedimintResponse<LnPayResponse> => {
const request: LnPayRequest = {
paymentInfo,
amountMsat,
lnurlComment,
};

return await this.postWithId<LnPayResponse>(
"/ln/pay",
request,
federationId
),
);
},

/**
* Outputs a list of registered lighting lightning gateways
*/
listGateways: async (): FedimintResponse<Gateway[]> =>
await this.postWithId<Gateway[]>("/ln/list-gateways", {}),

/**
* Switches the active lightning gateway
*/
switchGateway: async (
request: SwitchGatewayRequest,
federationId?: string
): FedimintResponse<Gateway> =>
await this.postWithId<Gateway>(
"/ln/switch-gateway",
request,
federationId
),
};

/**
* A module for creating a bitcoin deposit address
* A module for interacting with an ecash mint
*/
public wallet = {
public mint = {
/**
* Creates a new bitcoin deposit address
* Reissues an ecash note
*/
createDepositAddress: async (
request: DepositAddressRequest,
reissue: async (
notes: string,
federationId?: string
): FedimintResponse<DepositAddressResponse> =>
await this.postWithId<DepositAddressResponse>(
"/wallet/deposit-address",
): FedimintResponse<ReissueResponse> => {
const request: ReissueRequest = { notes };

return await this.postWithId<ReissueResponse>(
"/mint/reissue",
request,
federationId
),
);
},

/**
* Waits for a bitcoin deposit to be confirmed
* Spends an ecash note
*/
awaitDeposit: async (
request: AwaitDepositRequest,
spend: async (
amountMsat: number,
allowOverpay: boolean,
timeout: number,
federationId?: string
): FedimintResponse<AwaitDepositResponse> =>
await this.postWithId<AwaitDepositResponse>(
"/wallet/await-deposit",
): FedimintResponse<SpendResponse> => {
const request: SpendRequest = { amountMsat, allowOverpay, timeout };

return await this.postWithId<SpendResponse>(
"/mint/spend",
request,
federationId
),
);
},

/**
* Withdraws bitcoin from the federation
* Validates an ecash note
*/
withdraw: async (
request: WithdrawRequest,
validate: async (
notes: string,
federationId?: string
): FedimintResponse<WithdrawResponse> =>
await this.postWithId<WithdrawResponse>(
"/wallet/withdraw",
): FedimintResponse<ValidateResponse> => {
const request: ValidateRequest = { notes };

return await this.postWithId<ValidateResponse>(
"/mint/validate",
request,
federationId
),
);
},

/**
* Splits an ecash note into smaller notes
*/
split: async (notes: string): FedimintResponse<SplitResponse> => {
const request: SplitRequest = { notes };

return await this.post<SplitResponse>("/mint/split", request);
},

/**
* Combines ecash notes
*/
combine: async (notesVec: string[]): FedimintResponse<CombineResponse> => {
const request: CombineRequest = { notesVec };

return await this.post<CombineResponse>("/mint/combine", request);
},
};

/**
* A module for interacting with an ecash mint
* A module for onchain bitcoin operations
*/
public mint = {
public onchain = {
/**
* Reissues an ecash note
* Creates a new bitcoin deposit address
*/
reissue: async (
request: ReissueRequest,
createDepositAddress: async (
timeout: number,
federationId?: string
): FedimintResponse<ReissueResponse> =>
await this.postWithId<ReissueResponse>(
"/mint/reissue",
): FedimintResponse<DepositAddressResponse> => {
const request: DepositAddressRequest = { timeout };

return await this.postWithId<DepositAddressResponse>(
"/wallet/deposit-address",
request,
federationId
),
);
},

/**
* Spends an ecash note
* Waits for a bitcoin deposit to be confirmed
*/
spend: async (
request: SpendRequest,
awaitDeposit: async (
operationId: string,
federationId?: string
): FedimintResponse<SpendResponse> =>
await this.postWithId<SpendResponse>(
"/mint/spend",
): FedimintResponse<AwaitDepositResponse> => {
const request: AwaitDepositRequest = { operationId };

return await this.postWithId<AwaitDepositResponse>(
"/wallet/await-deposit",
request,
federationId
),
);
},

/**
* Validates an ecash note
* Withdraws bitcoin from the federation
*/
validate: async (
request: ValidateRequest,
withdraw: async (
address: string,
amountMsat: number | "all",
federationId?: string
): FedimintResponse<ValidateResponse> =>
await this.postWithId<ValidateResponse>(
"/mint/validate",
): FedimintResponse<WithdrawResponse> => {
const request: WithdrawRequest = { address, amountMsat };

return await this.postWithId<WithdrawResponse>(
"/wallet/withdraw",
request,
federationId
),

/**
* Splits an ecash note
*/
split: async (request: SplitRequest): FedimintResponse<SplitResponse> =>
await this.post<SplitResponse>("/mint/split", request),

/**
* Combines ecash notes
*/
combine: async (
request: CombineRequest
): FedimintResponse<CombineResponse> =>
await this.post<CombineResponse>("/mint/combine", request),
);
},
};
}

Expand Down
Loading

0 comments on commit fad3ff4

Please sign in to comment.