@plutohq/pluto-js
Advanced tools
Comparing version 1.6.1 to 1.6.2
@@ -19,25 +19,9 @@ import { PaymentIntent, Pluto, PlutoOptions } from '@plutohq/pluto-node'; | ||
constructor(apiKey?: string, options?: PlutoJSOptions); | ||
payInvoice(invoiceId: string, options?: { | ||
plutoAccount?: string; | ||
}): Promise<PaymentIntent>; | ||
pollPayment(paymentIntentId: string, options?: { | ||
plutoAccount?: string; | ||
}): Promise<PaymentIntent>; | ||
confirmPayment(paymentIntentId: string, options?: { | ||
ethSigner?: Signer | null; | ||
solWallet?: any | null; | ||
solConnection?: Connection | null; | ||
plutoAccount?: string; | ||
}): Promise<{ | ||
payInvoice(invoiceId: string): Promise<PaymentIntent>; | ||
pollPayment(paymentIntentId: string): Promise<PaymentIntent>; | ||
confirmPayment(paymentIntentId: string): Promise<{ | ||
hash: string; | ||
} | undefined>; | ||
processETH(paymentIntent: PaymentIntent, options: { | ||
signer: Signer; | ||
contractAddress: string; | ||
}): Promise<any>; | ||
processSOL(paymentIntent: PaymentIntent, options: { | ||
provider: any; | ||
connection: Connection; | ||
plutoAccount?: string; | ||
}): Promise<{ | ||
processETH(paymentIntent: PaymentIntent): Promise<any>; | ||
processSOL(paymentIntent: PaymentIntent): Promise<{ | ||
hash: string; | ||
@@ -44,0 +28,0 @@ }>; |
@@ -76,22 +76,14 @@ "use strict"; | ||
} | ||
PlutoJS.prototype.payInvoice = function (invoiceId, options) { | ||
PlutoJS.prototype.payInvoice = function (invoiceId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var plutoAccount; | ||
return __generator(this, function (_a) { | ||
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 })]; | ||
return [2 /*return*/, this.pluto.invoices.pay(invoiceId, {}, { plutoAccount: this.plutoAccount })]; | ||
}); | ||
}); | ||
}; | ||
PlutoJS.prototype.pollPayment = function (paymentIntentId, options) { | ||
PlutoJS.prototype.pollPayment = function (paymentIntentId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var plutoAccount; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
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 }) | ||
return [2 /*return*/, this.pluto.paymentIntents.poll(paymentIntentId, {}, { plutoAccount: this.plutoAccount }) | ||
.then(function (paymentIntent) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -103,3 +95,3 @@ var _this = this; | ||
return [2 /*return*/, new Promise(function (resolve) { | ||
setTimeout(function () { return resolve(_this.pollPayment(paymentIntentId, options)); }, 1000); | ||
setTimeout(function () { return resolve(_this.pollPayment(paymentIntentId)); }, 1000); | ||
})]; | ||
@@ -111,5 +103,5 @@ }); | ||
}; | ||
PlutoJS.prototype.confirmPayment = function (paymentIntentId, options) { | ||
PlutoJS.prototype.confirmPayment = function (paymentIntentId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var paymentIntent, signer, provider, connection; | ||
var paymentIntent; | ||
return __generator(this, function (_a) { | ||
@@ -132,27 +124,9 @@ switch (_a.label) { | ||
]), | ||
}, { plutoAccount: options === null || options === void 0 ? void 0 : options.plutoAccount })]; | ||
}, { plutoAccount: this.plutoAccount })]; | ||
case 1: | ||
paymentIntent = _a.sent(); | ||
if (paymentIntent.currency === 'eth') { | ||
signer = (options === null || options === void 0 ? void 0 : options.ethSigner) || this.ethSigner; | ||
if (!signer) | ||
throw new Error('No signer present'); | ||
return [2 /*return*/, this.processETH(paymentIntent, { | ||
signer: signer, | ||
contractAddress: this.contractAddress, | ||
})]; | ||
} | ||
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'); | ||
if (!connection) | ||
throw new Error('No connection present'); | ||
return [2 /*return*/, this.processSOL(paymentIntent, { | ||
provider: provider, | ||
connection: connection, | ||
plutoAccount: (options === null || options === void 0 ? void 0 : options.plutoAccount) || this.plutoAccount, | ||
})]; | ||
} | ||
if (paymentIntent.currency === 'eth') | ||
return [2 /*return*/, this.processETH(paymentIntent)]; | ||
if (paymentIntent.currency === 'sol') | ||
return [2 /*return*/, this.processSOL(paymentIntent)]; | ||
return [2 /*return*/]; | ||
@@ -163,3 +137,3 @@ } | ||
}; | ||
PlutoJS.prototype.processETH = function (paymentIntent, options) { | ||
PlutoJS.prototype.processETH = function (paymentIntent) { | ||
var _a, _b; | ||
@@ -169,2 +143,4 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_c) { | ||
if (!this.ethSigner) | ||
throw new Error('No signer present'); | ||
receivers = __spreadArray(__spreadArray([ | ||
@@ -174,3 +150,3 @@ (_b = (_a = paymentIntent.account.payout_wallets) === null || _a === void 0 ? void 0 : _a.find(function (wallet) { return wallet.chain === 'eth'; })) === null || _b === void 0 ? void 0 : _b.address | ||
amounts = (0, calculate_amounts_1.default)(paymentIntent); | ||
contract = new ethers_1.Contract(options.contractAddress, multitransfer_abi_1.default, options.signer); | ||
contract = new ethers_1.Contract(this.contractAddress, multitransfer_abi_1.default, this.ethSigner); | ||
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) })]; | ||
@@ -180,13 +156,18 @@ }); | ||
}; | ||
PlutoJS.prototype.processSOL = function (paymentIntent, options) { | ||
PlutoJS.prototype.processSOL = function (paymentIntent) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var receivers, recentBlockhash, transaction, amounts, signed, signature; | ||
var _this = this; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (!this.solWallet) | ||
throw new Error('No wallet present'); | ||
if (!this.solConnection) | ||
throw new Error('No connection present'); | ||
receivers = __spreadArray(__spreadArray([ | ||
(_b = (_a = paymentIntent.account.payout_wallets) === null || _a === void 0 ? void 0 : _a.find(function (wallet) { return wallet.chain === 'sol'; })) === null || _b === void 0 ? void 0 : _b.address | ||
], paymentIntent.fees.map(function (fee) { return fee.wallet.address; }), true), paymentIntent.transfers.map(function (transfer) { return transfer.wallet.address; }), true); | ||
return [4 /*yield*/, options.connection.getLatestBlockhash()]; | ||
return [4 /*yield*/, this.solConnection.getLatestBlockhash()]; | ||
case 1: | ||
@@ -196,3 +177,3 @@ recentBlockhash = (_c.sent()).blockhash; | ||
recentBlockhash: recentBlockhash, | ||
feePayer: options.provider.publicKey, | ||
feePayer: this.solWallet.publicKey, | ||
}); | ||
@@ -203,3 +184,3 @@ amounts = (0, calculate_amounts_1.default)(paymentIntent); | ||
transaction.add(web3_js_1.SystemProgram.transfer({ | ||
fromPubkey: options.provider.publicKey, | ||
fromPubkey: _this.solWallet.publicKey, | ||
toPubkey: new web3_js_1.PublicKey(receivers[index] || ''), | ||
@@ -209,9 +190,9 @@ lamports: Math.round(amount * 1e9), | ||
}); | ||
return [4 /*yield*/, options.provider.signTransaction(transaction)]; | ||
return [4 /*yield*/, this.solWallet.signTransaction(transaction)]; | ||
case 2: | ||
signed = _c.sent(); | ||
return [4 /*yield*/, options.connection.sendRawTransaction(signed.serialize())]; | ||
return [4 /*yield*/, this.solConnection.sendRawTransaction(signed.serialize())]; | ||
case 3: | ||
signature = _c.sent(); | ||
return [4 /*yield*/, this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, { plutoAccount: options.plutoAccount })]; | ||
return [4 /*yield*/, this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, { plutoAccount: this.plutoAccount })]; | ||
case 4: | ||
@@ -218,0 +199,0 @@ _c.sent(); |
{ | ||
"name": "@plutohq/pluto-js", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "JS Library for Pluto", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,5 +0,5 @@ | ||
import { Account, Fee, PaymentIntent, Pluto, PlutoOptions, Transfer, Wallet } from '@plutohq/pluto-node'; | ||
import { Contract, Signer } from 'ethers'; | ||
import {Account, Fee, PaymentIntent, Pluto, PlutoOptions, Transfer, Wallet} from '@plutohq/pluto-node'; | ||
import {Contract, Signer} from 'ethers'; | ||
import qs from 'qs'; | ||
import { Connection, PublicKey, SystemProgram, Transaction } from '@solana/web3.js'; | ||
import {Connection, PublicKey, SystemProgram, Transaction} from '@solana/web3.js'; | ||
import contracts from './constants/contracts'; | ||
@@ -41,14 +41,8 @@ import Contracts from './types/contracts'; | ||
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): Promise<PaymentIntent> { | ||
return this.pluto.invoices.pay(invoiceId, {}, { plutoAccount: this.plutoAccount }); | ||
} | ||
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 }) | ||
async pollPayment(paymentIntentId: string): Promise<PaymentIntent> { | ||
return this.pluto.paymentIntents.poll(paymentIntentId, {}, { plutoAccount: this.plutoAccount }) | ||
.then(async (paymentIntent: PaymentIntent) => { | ||
@@ -58,3 +52,3 @@ if (paymentIntent.status !== 'requires_confirmation') return paymentIntent; | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(this.pollPayment(paymentIntentId, options)), 1000); | ||
setTimeout(() => resolve(this.pollPayment(paymentIntentId)), 1000); | ||
}); | ||
@@ -64,6 +58,3 @@ }); | ||
async confirmPayment( | ||
paymentIntentId: string, | ||
options?: { ethSigner?: Signer | null; solWallet?: any | null, solConnection?: Connection | null, plutoAccount?: string } | ||
): Promise<{ hash: string } | undefined> { | ||
async confirmPayment(paymentIntentId: string): Promise<{ hash: string } | undefined> { | ||
const paymentIntent = await this.pluto.paymentIntents.retrieve( | ||
@@ -87,38 +78,14 @@ paymentIntentId, | ||
}, | ||
{ plutoAccount: options?.plutoAccount } | ||
{ plutoAccount: this.plutoAccount } | ||
); | ||
if (paymentIntent.currency === 'eth') { | ||
const signer = options?.ethSigner || this.ethSigner; | ||
if (!signer) throw new Error('No signer present'); | ||
if (paymentIntent.currency === 'eth') return this.processETH(paymentIntent); | ||
if (paymentIntent.currency === 'sol') return this.processSOL(paymentIntent); | ||
return this.processETH(paymentIntent, { | ||
signer, | ||
contractAddress: this.contractAddress, | ||
}); | ||
} | ||
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'); | ||
return this.processSOL( | ||
paymentIntent, | ||
{ | ||
provider, | ||
connection, | ||
plutoAccount: options?.plutoAccount || this.plutoAccount, | ||
}, | ||
); | ||
} | ||
return; | ||
} | ||
async processETH( | ||
paymentIntent: PaymentIntent, | ||
options: { signer: Signer; contractAddress: string } | ||
) { | ||
async processETH(paymentIntent: PaymentIntent) { | ||
if (!this.ethSigner) throw new Error('No signer present'); | ||
const receivers = [ | ||
@@ -130,10 +97,5 @@ (paymentIntent.account as Account).payout_wallets?.find((wallet) => wallet.chain === 'eth') | ||
]; | ||
const amounts = calculateAmounts(paymentIntent); | ||
const contract = new Contract( | ||
options.contractAddress, | ||
abi, | ||
options.signer | ||
); | ||
const contract = new Contract(this.contractAddress, abi, this.ethSigner); | ||
@@ -148,6 +110,6 @@ return contract.send( | ||
async processSOL( | ||
paymentIntent: PaymentIntent, | ||
options: { provider: any; connection: Connection, plutoAccount?: string }, | ||
) { | ||
async processSOL(paymentIntent: PaymentIntent) { | ||
if (!this.solWallet) throw new Error('No wallet present'); | ||
if (!this.solConnection) throw new Error('No connection present'); | ||
const receivers = [ | ||
@@ -160,6 +122,6 @@ (paymentIntent.account as Account).payout_wallets?.find((wallet) => wallet.chain === 'sol') | ||
const recentBlockhash = (await options.connection.getLatestBlockhash()).blockhash; | ||
const recentBlockhash = (await this.solConnection.getLatestBlockhash()).blockhash; | ||
const transaction = new Transaction({ | ||
recentBlockhash, | ||
feePayer: options.provider.publicKey, | ||
feePayer: this.solWallet.publicKey, | ||
}); | ||
@@ -173,3 +135,3 @@ | ||
SystemProgram.transfer({ | ||
fromPubkey: options.provider.publicKey, | ||
fromPubkey: this.solWallet.publicKey, | ||
toPubkey: new PublicKey(receivers[index] || ''), | ||
@@ -181,6 +143,6 @@ lamports: Math.round(amount * 1e9), | ||
const signed = await options.provider.signTransaction(transaction); | ||
const signature = await options.connection.sendRawTransaction(signed.serialize()); | ||
const signed = await this.solWallet.signTransaction(transaction); | ||
const signature = await this.solConnection.sendRawTransaction(signed.serialize()); | ||
await this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, { plutoAccount: options.plutoAccount }); | ||
await this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, { plutoAccount: this.plutoAccount }); | ||
@@ -187,0 +149,0 @@ return { hash: signature }; |
Sorry, the diff of this file is not supported yet
30777
706