@plutohq/pluto-js
Advanced tools
Comparing version 1.5.7 to 1.6.0
import { PaymentIntent, Pluto, PlutoOptions } from '@plutohq/pluto-node'; | ||
import { Signer } from 'ethers'; | ||
import { Connection } from '@solana/web3.js'; | ||
export declare type PlutoJSOptions = PlutoOptions & { | ||
ethSigner?: Signer | null; | ||
solWallet?: any | null; | ||
solConnection?: Connection | null; | ||
plutoAccount?: string; | ||
}; | ||
export default class PlutoJS { | ||
@@ -8,25 +14,29 @@ pluto: Pluto; | ||
contractAddress: string; | ||
constructor(apiKey?: string, options?: PlutoOptions); | ||
retrieveSubscription(subscriptionId: string, options?: { | ||
plutoAccount: string; | ||
}): Promise<import("@plutohq/pluto-node").Subscription>; | ||
ethSigner?: Signer; | ||
solWallet?: any; | ||
solConnection?: Connection; | ||
plutoAccount?: string; | ||
constructor(apiKey?: string, options?: PlutoJSOptions); | ||
payInvoice(invoiceId: string, options?: { | ||
plutoAccount: string; | ||
plutoAccount?: string; | ||
}): Promise<PaymentIntent>; | ||
completePayment(paymentIntentId: string, transactionOptions: { | ||
pollPayment(paymentIntentId: string, options?: { | ||
plutoAccount?: string; | ||
}): Promise<PaymentIntent>; | ||
confirmPayment(paymentIntentId: string, options?: { | ||
ethSigner?: Signer | null; | ||
solWallet?: any | null; | ||
solConnection?: Connection | null; | ||
}, options?: { | ||
plutoAccount: string; | ||
}): Promise<any>; | ||
processETH(paymentIntent: PaymentIntent, transactionOptions: { | ||
plutoAccount?: string; | ||
}): Promise<{ | ||
hash: string; | ||
} | undefined>; | ||
processETH(paymentIntent: PaymentIntent, options: { | ||
signer: Signer; | ||
contractAddress: string; | ||
}): Promise<any>; | ||
processSOL(paymentIntent: PaymentIntent, transactionOptions: { | ||
wallet: any; | ||
processSOL(paymentIntent: PaymentIntent, options: { | ||
provider: any; | ||
connection: Connection; | ||
}, options?: { | ||
plutoAccount: string; | ||
plutoAccount?: string; | ||
}): Promise<{ | ||
@@ -33,0 +43,0 @@ hash: string; |
@@ -67,20 +67,47 @@ "use strict"; | ||
: contracts_1.default[this.env]; | ||
if (options === null || options === void 0 ? void 0 : options.ethSigner) | ||
this.ethSigner = options.ethSigner; | ||
if (options === null || options === void 0 ? void 0 : options.solWallet) | ||
this.solWallet = options.solWallet; | ||
if (options === null || options === void 0 ? void 0 : options.solConnection) | ||
this.solConnection = options.solConnection; | ||
if (options === null || options === void 0 ? void 0 : options.plutoAccount) | ||
this.plutoAccount = options.plutoAccount; | ||
} | ||
PlutoJS.prototype.retrieveSubscription = function (subscriptionId, options) { | ||
PlutoJS.prototype.payInvoice = function (invoiceId, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var plutoAccount; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.pluto.subscriptions.retrieve(subscriptionId, {}, options)]; | ||
plutoAccount = (options === null || options === void 0 ? void 0 : options.plutoAccount) || this.plutoAccount; | ||
if (!plutoAccount) | ||
throw new Error('No account specified'); | ||
return [2 /*return*/, this.pluto.invoices.pay(invoiceId, {}, { plutoAccount: plutoAccount })]; | ||
}); | ||
}); | ||
}; | ||
PlutoJS.prototype.payInvoice = function (invoiceId, options) { | ||
PlutoJS.prototype.pollPayment = function (paymentIntentId, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var plutoAccount; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.pluto.invoices.pay(invoiceId, {}, options)]; | ||
plutoAccount = (options === null || options === void 0 ? void 0 : options.plutoAccount) || this.plutoAccount; | ||
if (!plutoAccount) | ||
throw new Error('No account specified'); | ||
return [2 /*return*/, this.pluto.paymentIntents.poll(paymentIntentId, {}, { plutoAccount: plutoAccount }) | ||
.then(function (paymentIntent) { return __awaiter(_this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (paymentIntent.status !== 'requires_confirmation') | ||
return [2 /*return*/, paymentIntent]; | ||
return [2 /*return*/, new Promise(function (resolve) { | ||
setTimeout(function () { return resolve(_this.pollPayment(paymentIntentId, options)); }, 1000); | ||
})]; | ||
}); | ||
}); })]; | ||
}); | ||
}); | ||
}; | ||
PlutoJS.prototype.completePayment = function (paymentIntentId, transactionOptions, options) { | ||
PlutoJS.prototype.confirmPayment = function (paymentIntentId, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var paymentIntent, signer, wallet, connection; | ||
var paymentIntent, signer, provider, connection; | ||
return __generator(this, function (_a) { | ||
@@ -103,7 +130,7 @@ switch (_a.label) { | ||
]), | ||
}, options)]; | ||
}, { plutoAccount: options === null || options === void 0 ? void 0 : options.plutoAccount })]; | ||
case 1: | ||
paymentIntent = _a.sent(); | ||
if (paymentIntent.currency === 'eth') { | ||
signer = transactionOptions.ethSigner; | ||
signer = (options === null || options === void 0 ? void 0 : options.ethSigner) || this.ethSigner; | ||
if (!signer) | ||
@@ -116,6 +143,6 @@ throw new Error('No signer present'); | ||
} | ||
if (paymentIntent.currency === 'sol' && transactionOptions.solConnection) { | ||
wallet = transactionOptions.solWallet; | ||
connection = transactionOptions.solConnection; | ||
if (!wallet) | ||
if (paymentIntent.currency === 'sol') { | ||
provider = (options === null || options === void 0 ? void 0 : options.solWallet) || this.solWallet; | ||
connection = (options === null || options === void 0 ? void 0 : options.solConnection) || this.solConnection; | ||
if (!provider) | ||
throw new Error('No wallet present'); | ||
@@ -125,7 +152,8 @@ if (!connection) | ||
return [2 /*return*/, this.processSOL(paymentIntent, { | ||
wallet: transactionOptions.solWallet, | ||
connection: transactionOptions.solConnection, | ||
}, options)]; | ||
provider: provider, | ||
connection: connection, | ||
plutoAccount: (options === null || options === void 0 ? void 0 : options.plutoAccount) || this.plutoAccount, | ||
})]; | ||
} | ||
return [2 /*return*/, false]; | ||
return [2 /*return*/]; | ||
} | ||
@@ -135,3 +163,3 @@ }); | ||
}; | ||
PlutoJS.prototype.processETH = function (paymentIntent, transactionOptions) { | ||
PlutoJS.prototype.processETH = function (paymentIntent, options) { | ||
var _a, _b; | ||
@@ -145,3 +173,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
amounts = (0, calculate_amounts_1.default)(paymentIntent); | ||
contract = new ethers_1.Contract(transactionOptions.contractAddress, multitransfer_abi_1.default, transactionOptions.signer); | ||
contract = new ethers_1.Contract(options.contractAddress, multitransfer_abi_1.default, options.signer); | ||
return [2 /*return*/, contract.send(receivers, amounts.map(function (amount) { return (0, to_number_string_1.default)(Math.round(amount * 1e16) * 1e2); }), paymentIntent.id, { value: (0, to_number_string_1.default)(Math.round(paymentIntent.amount * 1e16) * 1e2) })]; | ||
@@ -151,3 +179,3 @@ }); | ||
}; | ||
PlutoJS.prototype.processSOL = function (paymentIntent, transactionOptions, options) { | ||
PlutoJS.prototype.processSOL = function (paymentIntent, options) { | ||
var _a, _b; | ||
@@ -162,3 +190,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
], paymentIntent.fees.map(function (fee) { return fee.wallet.address; }), true), paymentIntent.transfers.map(function (transfer) { return transfer.wallet.address; }), true); | ||
return [4 /*yield*/, transactionOptions.connection.getLatestBlockhash()]; | ||
return [4 /*yield*/, options.connection.getLatestBlockhash()]; | ||
case 1: | ||
@@ -168,3 +196,3 @@ recentBlockhash = (_c.sent()).blockhash; | ||
recentBlockhash: recentBlockhash, | ||
feePayer: transactionOptions.wallet.publicKey, | ||
feePayer: options.provider.publicKey, | ||
}); | ||
@@ -175,3 +203,3 @@ amounts = (0, calculate_amounts_1.default)(paymentIntent); | ||
transaction.add(web3_js_1.SystemProgram.transfer({ | ||
fromPubkey: transactionOptions.wallet.publicKey, | ||
fromPubkey: options.provider.publicKey, | ||
toPubkey: new web3_js_1.PublicKey(receivers[index] || ''), | ||
@@ -181,9 +209,9 @@ lamports: Math.round(amount * 1e9), | ||
}); | ||
return [4 /*yield*/, transactionOptions.wallet.signTransaction(transaction)]; | ||
return [4 /*yield*/, options.provider.signTransaction(transaction)]; | ||
case 2: | ||
signed = _c.sent(); | ||
return [4 /*yield*/, transactionOptions.connection.sendRawTransaction(signed.serialize())]; | ||
return [4 /*yield*/, options.connection.sendRawTransaction(signed.serialize())]; | ||
case 3: | ||
signature = _c.sent(); | ||
return [4 /*yield*/, this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, options)]; | ||
return [4 /*yield*/, this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, { plutoAccount: options.plutoAccount })]; | ||
case 4: | ||
@@ -190,0 +218,0 @@ _c.sent(); |
{ | ||
"name": "@plutohq/pluto-js", | ||
"version": "1.5.7", | ||
"version": "1.6.0", | ||
"description": "JS Library for Pluto", | ||
@@ -14,3 +14,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@plutohq/pluto-node": "^1.4.5", | ||
"@plutohq/pluto-node": "^1.4.9", | ||
"@solana/web3.js": "^1.44.0", | ||
@@ -17,0 +17,0 @@ "ethers": "^5.6.8", |
@@ -11,2 +11,9 @@ import { Account, Fee, PaymentIntent, Pluto, PlutoOptions, Transfer, Wallet } from '@plutohq/pluto-node'; | ||
export type PlutoJSOptions = PlutoOptions & { | ||
ethSigner?: Signer | null; | ||
solWallet?: any | null; // TODO: Find a better type than `any`. Currently can't use type from Solana wallet adapter, the types are incompatible with multiple implementations | ||
solConnection?: Connection | null; | ||
plutoAccount?: string; | ||
} | ||
export default class PlutoJS { | ||
@@ -16,4 +23,8 @@ pluto: Pluto; | ||
contractAddress: string; | ||
ethSigner?: Signer; | ||
solWallet?: any; | ||
solConnection?: Connection; | ||
plutoAccount?: string; | ||
constructor(apiKey?: string, options?: PlutoOptions) { | ||
constructor(apiKey?: string, options?: PlutoJSOptions) { | ||
this.pluto = new Pluto(apiKey || '', options); | ||
@@ -25,17 +36,34 @@ this.env = options?.env || 'prd'; | ||
: contracts[this.env as keyof Contracts]; | ||
if (options?.ethSigner) this.ethSigner = options.ethSigner; | ||
if (options?.solWallet) this.solWallet = options.solWallet; | ||
if (options?.solConnection) this.solConnection = options.solConnection; | ||
if (options?.plutoAccount) this.plutoAccount = options.plutoAccount; | ||
} | ||
async retrieveSubscription(subscriptionId: string, options?: { plutoAccount: string }) { | ||
return this.pluto.subscriptions.retrieve(subscriptionId, {}, options); | ||
async payInvoice(invoiceId: string, options?: { plutoAccount?: string }): Promise<PaymentIntent> { | ||
const plutoAccount = options?.plutoAccount || this.plutoAccount; | ||
if (!plutoAccount) throw new Error('No account specified'); | ||
return this.pluto.invoices.pay(invoiceId, {}, { plutoAccount }); | ||
} | ||
async payInvoice(invoiceId: string, options?: { plutoAccount: string }) { | ||
return this.pluto.invoices.pay(invoiceId, {}, options); | ||
async pollPayment(paymentIntentId: string, options?: { plutoAccount?: string }): Promise<PaymentIntent> { | ||
const plutoAccount = options?.plutoAccount || this.plutoAccount; | ||
if (!plutoAccount) throw new Error('No account specified'); | ||
return this.pluto.paymentIntents.poll(paymentIntentId, {}, { plutoAccount }) | ||
.then(async (paymentIntent: PaymentIntent) => { | ||
if (paymentIntent.status !== 'requires_confirmation') return paymentIntent; | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(this.pollPayment(paymentIntentId, options)), 1000); | ||
}); | ||
}); | ||
} | ||
async completePayment( | ||
async confirmPayment( | ||
paymentIntentId: string, | ||
transactionOptions: { ethSigner?: Signer | null; solWallet?: any | null, solConnection?: Connection | null }, | ||
options?: { plutoAccount: string } | ||
) { | ||
options?: { ethSigner?: Signer | null; solWallet?: any | null, solConnection?: Connection | null, plutoAccount?: string } | ||
): Promise<{ hash: string } | undefined> { | ||
const paymentIntent = await this.pluto.paymentIntents.retrieve( | ||
@@ -59,7 +87,7 @@ paymentIntentId, | ||
}, | ||
options | ||
{ plutoAccount: options?.plutoAccount } | ||
); | ||
if (paymentIntent.currency === 'eth') { | ||
const signer = transactionOptions.ethSigner; | ||
const signer = options?.ethSigner || this.ethSigner; | ||
if (!signer) throw new Error('No signer present'); | ||
@@ -73,6 +101,6 @@ | ||
if (paymentIntent.currency === 'sol' && transactionOptions.solConnection) { | ||
const wallet = transactionOptions.solWallet; | ||
const connection = transactionOptions.solConnection; | ||
if (!wallet) throw new Error('No wallet present'); | ||
if (paymentIntent.currency === 'sol') { | ||
const provider = options?.solWallet || this.solWallet; | ||
const connection = options?.solConnection || this.solConnection; | ||
if (!provider) throw new Error('No wallet present'); | ||
if (!connection) throw new Error('No connection present'); | ||
@@ -83,10 +111,10 @@ | ||
{ | ||
wallet: transactionOptions.solWallet, | ||
connection: transactionOptions.solConnection, | ||
provider, | ||
connection, | ||
plutoAccount: options?.plutoAccount || this.plutoAccount, | ||
}, | ||
options | ||
); | ||
} | ||
return false; | ||
return; | ||
} | ||
@@ -96,3 +124,3 @@ | ||
paymentIntent: PaymentIntent, | ||
transactionOptions: { signer: Signer; contractAddress: string } | ||
options: { signer: Signer; contractAddress: string } | ||
) { | ||
@@ -109,5 +137,5 @@ const receivers = [ | ||
const contract = new Contract( | ||
transactionOptions.contractAddress, | ||
options.contractAddress, | ||
abi, | ||
transactionOptions.signer | ||
options.signer | ||
); | ||
@@ -125,4 +153,3 @@ | ||
paymentIntent: PaymentIntent, | ||
transactionOptions: { wallet: any; connection: Connection }, | ||
options?: { plutoAccount: string } | ||
options: { provider: any; connection: Connection, plutoAccount?: string }, | ||
) { | ||
@@ -136,6 +163,6 @@ const receivers = [ | ||
const recentBlockhash = (await transactionOptions.connection.getLatestBlockhash()).blockhash; | ||
const recentBlockhash = (await options.connection.getLatestBlockhash()).blockhash; | ||
const transaction = new Transaction({ | ||
recentBlockhash, | ||
feePayer: transactionOptions.wallet.publicKey, | ||
feePayer: options.provider.publicKey, | ||
}); | ||
@@ -149,3 +176,3 @@ | ||
SystemProgram.transfer({ | ||
fromPubkey: transactionOptions.wallet.publicKey, | ||
fromPubkey: options.provider.publicKey, | ||
toPubkey: new PublicKey(receivers[index] || ''), | ||
@@ -157,6 +184,6 @@ lamports: Math.round(amount * 1e9), | ||
const signed = await transactionOptions.wallet.signTransaction(transaction); | ||
const signature = await transactionOptions.connection.sendRawTransaction(signed.serialize()); | ||
const signed = await options.provider.signTransaction(transaction); | ||
const signature = await options.connection.sendRawTransaction(signed.serialize()); | ||
await this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, options); | ||
await this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, { plutoAccount: options.plutoAccount }); | ||
@@ -163,0 +190,0 @@ return { hash: signature }; |
Sorry, the diff of this file is not supported yet
34292
30
775
Updated@plutohq/pluto-node@^1.4.9