Skip to content

Commit

Permalink
Merge pull request #145 from Egge21M/naming
Browse files Browse the repository at this point in the history
Naming Methods Mint/Melt
  • Loading branch information
callebtc authored Jul 16, 2024
2 parents 6c6cf0d + 142d671 commit 55dc4fa
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions migration-1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ To reduce complexity, simplify error handling and to prepare for token V4, this

Utility functions now have an `options` object for optional parameters, instead of passing them directly

**`requestMint(amount: number)` --> `mintQuote(amount: number)`**
**`requestMint(amount: number)` --> `createMintQuote(amount: number)`**
Now returns the following:

```typescript
Expand All @@ -51,7 +51,7 @@ where `request` is the invoice to be paid, and `quote` is the identifier used to

---

**`getMeltQuote(invoice: string)`** is now used to get fee estimation and conversion quotes instead of `getFee()` and returns:
**`createMeltQuote(invoice: string)`** is now used to get fee estimation and conversion quotes instead of `getFee()` and returns:

```typescript
type MeltQuoteResponse = {
Expand Down
24 changes: 12 additions & 12 deletions src/CashuMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CashuMint {
* @param customRequest
* @returns the mint will create and return a new mint quote containing a payment request for the specified amount and unit
*/
public static async mintQuote(
public static async createMintQuote(
mintUrl: string,
mintQuotePayload: MintQuotePayload,
customRequest?: typeof request
Expand All @@ -130,8 +130,8 @@ class CashuMint {
* @param mintQuotePayload Payload for creating a new mint quote
* @returns the mint will create and return a new mint quote containing a payment request for the specified amount and unit
*/
async mintQuote(mintQuotePayload: MintQuotePayload): Promise<MintQuoteResponse> {
return CashuMint.mintQuote(this._mintUrl, mintQuotePayload, this._customRequest);
async createMintQuote(mintQuotePayload: MintQuotePayload): Promise<MintQuoteResponse> {
return CashuMint.createMintQuote(this._mintUrl, mintQuotePayload, this._customRequest);
}

/**
Expand All @@ -141,7 +141,7 @@ class CashuMint {
* @param customRequest
* @returns the mint will create and return a Lightning invoice for the specified amount
*/
public static async getMintQuote(
public static async checkMintQuote(
mintUrl: string,
quote: string,
customRequest?: typeof request
Expand All @@ -160,8 +160,8 @@ class CashuMint {
* @param quote Quote ID
* @returns the mint will create and return a Lightning invoice for the specified amount
*/
async getMintQuote(quote: string): Promise<MintQuoteResponse> {
return CashuMint.getMintQuote(this._mintUrl, quote, this._customRequest);
async checkMintQuote(quote: string): Promise<MintQuoteResponse> {
return CashuMint.checkMintQuote(this._mintUrl, quote, this._customRequest);
}

/**
Expand Down Expand Up @@ -204,7 +204,7 @@ class CashuMint {
* @param MeltQuotePayload
* @returns
*/
public static async meltQuote(
public static async createMeltQuote(
mintUrl: string,
meltQuotePayload: MeltQuotePayload,
customRequest?: typeof request
Expand Down Expand Up @@ -233,8 +233,8 @@ class CashuMint {
* @param MeltQuotePayload
* @returns
*/
async meltQuote(meltQuotePayload: MeltQuotePayload): Promise<MeltQuoteResponse> {
return CashuMint.meltQuote(this._mintUrl, meltQuotePayload, this._customRequest);
async createMeltQuote(meltQuotePayload: MeltQuotePayload): Promise<MeltQuoteResponse> {
return CashuMint.createMeltQuote(this._mintUrl, meltQuotePayload, this._customRequest);
}

/**
Expand All @@ -243,7 +243,7 @@ class CashuMint {
* @param quote Quote ID
* @returns
*/
public static async getMeltQuote(
public static async checkMeltQuote(
mintUrl: string,
quote: string,
customRequest?: typeof request
Expand Down Expand Up @@ -274,8 +274,8 @@ class CashuMint {
* @param quote Quote ID
* @returns
*/
async getMeltQuote(quote: string): Promise<MeltQuoteResponse> {
return CashuMint.getMeltQuote(this._mintUrl, quote, this._customRequest);
async checkMeltQuote(quote: string): Promise<MeltQuoteResponse> {
return CashuMint.checkMeltQuote(this._mintUrl, quote, this._customRequest);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,21 +341,21 @@ class CashuWallet {
* @param amount Amount requesting for mint.
* @returns the mint will return a mint quote with a Lightning invoice for minting tokens of the specified amount and unit
*/
async mintQuote(amount: number) {
async createMintQuote(amount: number) {
const mintQuotePayload: MintQuotePayload = {
unit: this._unit,
amount: amount
};
return await this.mint.mintQuote(mintQuotePayload);
return await this.mint.createMintQuote(mintQuotePayload);
}

/**
* Gets an existing mint quote from the mint.
* @param quote Quote ID
* @returns the mint will create and return a Lightning invoice for the specified amount
*/
async getMintQuote(quote: string) {
return await this.mint.getMintQuote(quote);
async checkMintQuote(quote: string) {
return await this.mint.checkMintQuote(quote);
}

/**
Expand Down Expand Up @@ -397,12 +397,12 @@ class CashuWallet {
* @param invoice LN invoice that needs to get a fee estimate
* @returns the mint will create and return a melt quote for the invoice with an amount and fee reserve
*/
async meltQuote(invoice: string): Promise<MeltQuoteResponse> {
async createMeltQuote(invoice: string): Promise<MeltQuoteResponse> {
const meltQuotePayload: MeltQuotePayload = {
unit: this._unit,
request: invoice
};
const meltQuote = await this.mint.meltQuote(meltQuotePayload);
const meltQuote = await this.mint.createMeltQuote(meltQuotePayload);
return meltQuote;
}

Expand All @@ -411,8 +411,8 @@ class CashuWallet {
* @param quote ID of the melt quote
* @returns the mint will return an existing melt quote
*/
async getMeltQuote(quote: string): Promise<MeltQuoteResponse> {
const meltQuote = await this.mint.getMeltQuote(quote);
async checkMeltQuote(quote: string): Promise<MeltQuoteResponse> {
const meltQuote = await this.mint.checkMeltQuote(quote);
return meltQuote;
}

Expand Down Expand Up @@ -476,7 +476,7 @@ class CashuWallet {
}
): Promise<MeltTokensResponse> {
if (!meltQuote) {
meltQuote = await this.mint.meltQuote({ unit: this._unit, request: invoice });
meltQuote = await this.mint.createMeltQuote({ unit: this._unit, request: invoice });
}
return await this.meltTokens(meltQuote, proofsToSend, {
keysetId: options?.keysetId,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { encodeBase64ToJson, encodeJsonToBase64 } from './base64.js';
import { AmountPreference, Keys, Proof, Token, TokenEntry, TokenV2 } from './model/types/index.js';
import { AmountPreference, Keys, Proof, Token, TokenV2 } from './model/types/index.js';
import { TOKEN_PREFIX, TOKEN_VERSION } from './utils/Constants.js';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { sha256 } from '@noble/hashes/sha256';
Expand Down
36 changes: 18 additions & 18 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ describe('mint api', () => {
test('request mint', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(100);
const request = await wallet.createMintQuote(100);
expect(request).toBeDefined();
const mintQuote = await wallet.getMintQuote(request.quote);
const mintQuote = await wallet.checkMintQuote(request.quote);
expect(mintQuote).toBeDefined();
});
test('mint tokens', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(1337);
const request = await wallet.createMintQuote(1337);
expect(request).toBeDefined();
expect(request.request).toContain('lnbc1337');
const tokens = await wallet.mintTokens(1337, request.quote);
Expand All @@ -55,34 +55,34 @@ describe('mint api', () => {
test('get fee for local invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(100);
const fee = (await wallet.meltQuote(request.request)).fee_reserve;
const request = await wallet.createMintQuote(100);
const fee = (await wallet.createMeltQuote(request.request)).fee_reserve;
expect(fee).toBeDefined();
// because local invoice, fee should be 0
expect(fee).toBe(0);
});
test('get fee for external invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const fee = (await wallet.meltQuote(externalInvoice)).fee_reserve;
const fee = (await wallet.createMeltQuote(externalInvoice)).fee_reserve;
expect(fee).toBeDefined();
// because external invoice, fee should be > 0
expect(fee).toBeGreaterThan(0);
});
test('pay local invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(100);
const request = await wallet.createMintQuote(100);
const tokens = await wallet.mintTokens(100, request.quote);

// expect no fee because local invoice
const mintQuote = await wallet.mintQuote(10);
const quote = await wallet.meltQuote(mintQuote.request);
const mintQuote = await wallet.createMintQuote(10);
const quote = await wallet.createMeltQuote(mintQuote.request);
const fee = quote.fee_reserve;
expect(fee).toBe(0);

// get the quote from the mint
const quote_ = await wallet.getMeltQuote(quote.quote);
const quote_ = await wallet.checkMeltQuote(quote.quote);
expect(quote_).toBeDefined();

const sendResponse = await wallet.send(10, tokens.proofs);
Expand All @@ -104,15 +104,15 @@ describe('mint api', () => {
test('pay external invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(3000);
const request = await wallet.createMintQuote(3000);
const tokens = await wallet.mintTokens(3000, request.quote);

const meltQuote = await wallet.meltQuote(externalInvoice);
const meltQuote = await wallet.createMeltQuote(externalInvoice);
const fee = meltQuote.fee_reserve;
expect(fee).toBeGreaterThan(0);

// get the quote from the mint
const quote_ = await wallet.getMeltQuote(meltQuote.quote);
const quote_ = await wallet.checkMeltQuote(meltQuote.quote);
expect(quote_).toBeDefined();

const sendResponse = await wallet.send(2000 + fee, tokens.proofs);
Expand All @@ -135,7 +135,7 @@ describe('mint api', () => {
test('test send tokens exact without previous split', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(64);
const request = await wallet.createMintQuote(64);
const tokens = await wallet.mintTokens(64, request.quote);

const sendResponse = await wallet.send(64, tokens.proofs);
Expand All @@ -148,7 +148,7 @@ describe('mint api', () => {
test('test send tokens with change', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(100);
const request = await wallet.createMintQuote(100);
const tokens = await wallet.mintTokens(100, request.quote);

const sendResponse = await wallet.send(10, tokens.proofs);
Expand All @@ -161,7 +161,7 @@ describe('mint api', () => {
test('receive tokens with previous split', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(100);
const request = await wallet.createMintQuote(100);
const tokens = await wallet.mintTokens(100, request.quote);

const sendResponse = await wallet.send(10, tokens.proofs);
Expand All @@ -174,7 +174,7 @@ describe('mint api', () => {
test('receive tokens with previous mint', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint, { unit });
const request = await wallet.mintQuote(64);
const request = await wallet.createMintQuote(64);
const tokens = await wallet.mintTokens(64, request.quote);
const encoded = getEncodedToken({
token: [{ mint: mintUrl, proofs: tokens.proofs }]
Expand All @@ -192,7 +192,7 @@ describe('mint api', () => {
const privKeyBob = secp256k1.utils.randomPrivateKey();
const pubKeyBob = secp256k1.getPublicKey(privKeyBob);

const request = await wallet.mintQuote(64);
const request = await wallet.createMintQuote(64);
const tokens = await wallet.mintTokens(64, request.quote);

const { send } = await wallet.send(64, tokens.proofs, { pubkey: bytesToHex(pubKeyBob) });
Expand Down
4 changes: 2 additions & 2 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('requests', () => {
});

const wallet = new CashuWallet(mint, { unit });
await wallet.getMeltQuote('test');
await wallet.checkMeltQuote('test');

expect(request).toBeDefined();
// expect(request!['content-type']).toContain('application/json');
Expand All @@ -61,7 +61,7 @@ describe('requests', () => {
const wallet = new CashuWallet(mint, { unit });
setGlobalRequestOptions({ headers: { 'x-cashu': 'xyz-123-abc' } });

await wallet.getMeltQuote('test');
await wallet.checkMeltQuote('test');

expect(request).toBeDefined();
expect(request!['x-cashu']).toContain('xyz-123-abc');
Expand Down
6 changes: 3 additions & 3 deletions test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('test fees', () => {
} as MeltQuoteResponse);
const wallet = new CashuWallet(mint, { unit });

const fee = await wallet.getMeltQuote('test');
const fee = await wallet.checkMeltQuote('test');
const amount = 2000;

expect(fee.fee_reserve + amount).toEqual(2020);
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('payLnInvoice', () => {
} as MeltQuoteResponse);

const wallet = new CashuWallet(mint, { unit });
const meltQuote = await wallet.getMeltQuote('test');
const meltQuote = await wallet.checkMeltQuote('test');

const result = await wallet.payLnInvoice(invoice, proofs, meltQuote);

Expand Down Expand Up @@ -291,7 +291,7 @@ describe('payLnInvoice', () => {
});

const wallet = new CashuWallet(mint, { unit });
const meltQuote = await wallet.getMeltQuote('test');
const meltQuote = await wallet.checkMeltQuote('test');
const result = await wallet.payLnInvoice(invoice, [{ ...proofs[0], amount: 3 }], meltQuote);

expect(result.isPaid).toBe(true);
Expand Down

0 comments on commit 55dc4fa

Please sign in to comment.