@plutohq/pluto-js
Advanced tools
Comparing version 1.6.3 to 1.6.4
@@ -1,2 +0,4 @@ | ||
export default { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = { | ||
dev: '0xCfEB869F69431e42cdB54A4F4f105C19C080A601', | ||
@@ -3,0 +5,0 @@ stg: '0x7e02a9d2926f8602728d9d4b4211d186d8f4bab7', |
@@ -1,2 +0,4 @@ | ||
export default [ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = [ | ||
{ | ||
@@ -3,0 +5,0 @@ inputs: [], |
@@ -1,11 +0,25 @@ | ||
export default function calculateAmounts(paymentIntent) { | ||
const feeAmounts = paymentIntent.fees.map((fee) => Number(((fee.percent / 100) * paymentIntent.amount).toFixed(5))); | ||
const transferAmounts = paymentIntent.transfers.map((transfer) => Number(((transfer.percent / 100) * paymentIntent.amount).toFixed(5))); | ||
const feeSum = feeAmounts.reduce((sum, amount) => sum + amount, 0); | ||
const transferSum = transferAmounts.reduce((sum, amount) => sum + amount, 0); | ||
return [ | ||
Number((paymentIntent.amount - feeSum - transferSum).toFixed(5)), | ||
...feeAmounts, | ||
...transferAmounts, | ||
]; | ||
"use strict"; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function calculateAmounts(paymentIntent) { | ||
var feeAmounts = paymentIntent.fees.map(function (fee) { | ||
return Number(((fee.percent / 100) * paymentIntent.amount).toFixed(5)); | ||
}); | ||
var transferAmounts = paymentIntent.transfers.map(function (transfer) { | ||
return Number(((transfer.percent / 100) * paymentIntent.amount).toFixed(5)); | ||
}); | ||
var feeSum = feeAmounts.reduce(function (sum, amount) { return sum + amount; }, 0); | ||
var transferSum = transferAmounts.reduce(function (sum, amount) { return sum + amount; }, 0); | ||
return __spreadArray(__spreadArray([ | ||
Number((paymentIntent.amount - feeSum - transferSum).toFixed(5)) | ||
], feeAmounts, true), transferAmounts, true); | ||
} | ||
exports.default = calculateAmounts; |
@@ -1,3 +0,6 @@ | ||
export default function toNumberString(number) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function toNumberString(number) { | ||
return number.toLocaleString('fullwide', { useGrouping: false }); | ||
} | ||
exports.default = toNumberString; |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -10,13 +11,54 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
import { Pluto } from '@plutohq/pluto-node'; | ||
import { Contract } from 'ethers'; | ||
import qs from 'qs'; | ||
import { PublicKey, SystemProgram, Transaction } from '@solana/web3.js'; | ||
import contracts from './constants/contracts'; | ||
import calculateAmounts from './helpers/calculate-amounts'; | ||
import toNumberString from './helpers/to-number-string'; | ||
import abi from './constants/multitransfer-abi'; | ||
export class PlutoJS { | ||
constructor(apiKey, options) { | ||
this.pluto = new Pluto(apiKey || '', options); | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.loadPluto = exports.PlutoJS = void 0; | ||
var pluto_node_1 = require("@plutohq/pluto-node"); | ||
var ethers_1 = require("ethers"); | ||
var qs_1 = __importDefault(require("qs")); | ||
var web3_js_1 = require("@solana/web3.js"); | ||
var contracts_1 = __importDefault(require("./constants/contracts")); | ||
var calculate_amounts_1 = __importDefault(require("./helpers/calculate-amounts")); | ||
var to_number_string_1 = __importDefault(require("./helpers/to-number-string")); | ||
var multitransfer_abi_1 = __importDefault(require("./constants/multitransfer-abi")); | ||
var PlutoJS = /** @class */ (function () { | ||
function PlutoJS(apiKey, options) { | ||
this.pluto = new pluto_node_1.Pluto(apiKey || '', options); | ||
this.env = (options === null || options === void 0 ? void 0 : options.env) || 'prd'; | ||
@@ -26,4 +68,4 @@ this.test = !!(apiKey === null || apiKey === void 0 ? void 0 : apiKey.includes('_test_')); | ||
this.test && this.env !== 'dev' | ||
? contracts.stg | ||
: contracts[this.env]; | ||
? contracts_1.default.stg | ||
: contracts_1.default[this.env]; | ||
if (options === null || options === void 0 ? void 0 : options.ethSigner) | ||
@@ -38,94 +80,126 @@ this.ethSigner = options.ethSigner; | ||
} | ||
payInvoice(invoiceId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.pluto.invoices.pay(invoiceId, {}, { plutoAccount: this.plutoAccount }); | ||
PlutoJS.prototype.payInvoice = function (invoiceId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.pluto.invoices.pay(invoiceId, {}, { plutoAccount: this.plutoAccount })]; | ||
}); | ||
}); | ||
} | ||
waitForPayment(paymentIntentId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.pluto.paymentIntents.poll(paymentIntentId, {}, { plutoAccount: this.plutoAccount }) | ||
.then((paymentIntent) => __awaiter(this, void 0, void 0, function* () { | ||
if (paymentIntent.status !== 'requires_confirmation') | ||
return paymentIntent; | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(this.waitForPayment(paymentIntentId)), 1000); | ||
}); | ||
})); | ||
}; | ||
PlutoJS.prototype.waitForPayment = function (paymentIntentId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.pluto.paymentIntents.poll(paymentIntentId, {}, { plutoAccount: this.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.waitForPayment(paymentIntentId)); }, 1000); | ||
})]; | ||
}); | ||
}); })]; | ||
}); | ||
}); | ||
} | ||
confirmPayment(paymentIntentId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const paymentIntent = yield this.pluto.paymentIntents.retrieve(paymentIntentId, { | ||
expand: qs.stringify([ | ||
{ | ||
path: 'account', | ||
populate: { path: 'payout_wallets' }, | ||
}, | ||
{ | ||
path: 'fees', | ||
populate: { path: 'wallet' }, | ||
}, | ||
{ | ||
path: 'transfers', | ||
populate: { path: 'wallet' }, | ||
}, | ||
]), | ||
}, { plutoAccount: this.plutoAccount }); | ||
if (paymentIntent.currency === 'eth') | ||
return this.processETH(paymentIntent); | ||
if (paymentIntent.currency === 'sol') | ||
return this.processSOL(paymentIntent); | ||
return; | ||
}; | ||
PlutoJS.prototype.confirmPayment = function (paymentIntentId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var paymentIntent; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.pluto.paymentIntents.retrieve(paymentIntentId, { | ||
expand: qs_1.default.stringify([ | ||
{ | ||
path: 'account', | ||
populate: { path: 'payout_wallets' }, | ||
}, | ||
{ | ||
path: 'fees', | ||
populate: { path: 'wallet' }, | ||
}, | ||
{ | ||
path: 'transfers', | ||
populate: { path: 'wallet' }, | ||
}, | ||
]), | ||
}, { plutoAccount: this.plutoAccount })]; | ||
case 1: | ||
paymentIntent = _a.sent(); | ||
if (paymentIntent.currency === 'eth') | ||
return [2 /*return*/, this.processETH(paymentIntent)]; | ||
if (paymentIntent.currency === 'sol') | ||
return [2 /*return*/, this.processSOL(paymentIntent)]; | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
processETH(paymentIntent) { | ||
}; | ||
PlutoJS.prototype.processETH = function (paymentIntent) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.ethSigner) | ||
throw new Error('No signer present'); | ||
const receivers = [ | ||
(_b = (_a = paymentIntent.account.payout_wallets) === null || _a === void 0 ? void 0 : _a.find((wallet) => wallet.chain === 'eth')) === null || _b === void 0 ? void 0 : _b.address, | ||
...paymentIntent.fees.map((fee) => fee.wallet.address), | ||
...paymentIntent.transfers.map((transfer) => transfer.wallet.address), | ||
]; | ||
const amounts = calculateAmounts(paymentIntent); | ||
const contract = new Contract(this.contractAddress, abi, this.ethSigner); | ||
return contract.send(receivers, amounts.map((amount) => toNumberString(Math.round(amount * 1e16) * 1e2)), paymentIntent.id, { value: toNumberString(Math.round(paymentIntent.amount * 1e16) * 1e2) }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var receivers, amounts, contract; | ||
return __generator(this, function (_c) { | ||
if (!this.ethSigner) | ||
throw new Error('No signer present'); | ||
receivers = __spreadArray(__spreadArray([ | ||
(_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 | ||
], paymentIntent.fees.map(function (fee) { return fee.wallet.address; }), true), paymentIntent.transfers.map(function (transfer) { return transfer.wallet.address; }), true); | ||
amounts = (0, calculate_amounts_1.default)(paymentIntent); | ||
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) })]; | ||
}); | ||
}); | ||
} | ||
processSOL(paymentIntent) { | ||
}; | ||
PlutoJS.prototype.processSOL = function (paymentIntent) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.solWallet) | ||
throw new Error('No wallet present'); | ||
if (!this.solConnection) | ||
throw new Error('No connection present'); | ||
const receivers = [ | ||
(_b = (_a = paymentIntent.account.payout_wallets) === null || _a === void 0 ? void 0 : _a.find((wallet) => wallet.chain === 'sol')) === null || _b === void 0 ? void 0 : _b.address, | ||
...paymentIntent.fees.map((fee) => fee.wallet.address), | ||
...paymentIntent.transfers.map((transfer) => transfer.wallet.address), | ||
]; | ||
const recentBlockhash = (yield this.solConnection.getLatestBlockhash()).blockhash; | ||
const transaction = new Transaction({ | ||
recentBlockhash, | ||
feePayer: this.solWallet.publicKey, | ||
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*/, this.solConnection.getLatestBlockhash()]; | ||
case 1: | ||
recentBlockhash = (_c.sent()).blockhash; | ||
transaction = new web3_js_1.Transaction({ | ||
recentBlockhash: recentBlockhash, | ||
feePayer: this.solWallet.publicKey, | ||
}); | ||
amounts = (0, calculate_amounts_1.default)(paymentIntent); | ||
amounts.forEach(function (amount, index) { | ||
if (amount) | ||
transaction.add(web3_js_1.SystemProgram.transfer({ | ||
fromPubkey: _this.solWallet.publicKey, | ||
toPubkey: new web3_js_1.PublicKey(receivers[index] || ''), | ||
lamports: Math.round(amount * 1e9), | ||
})); | ||
}); | ||
return [4 /*yield*/, this.solWallet.signTransaction(transaction)]; | ||
case 2: | ||
signed = _c.sent(); | ||
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: this.plutoAccount })]; | ||
case 4: | ||
_c.sent(); | ||
return [2 /*return*/, { hash: signature }]; | ||
} | ||
}); | ||
const amounts = calculateAmounts(paymentIntent); | ||
amounts.forEach((amount, index) => { | ||
if (amount) | ||
transaction.add(SystemProgram.transfer({ | ||
fromPubkey: this.solWallet.publicKey, | ||
toPubkey: new PublicKey(receivers[index] || ''), | ||
lamports: Math.round(amount * 1e9), | ||
})); | ||
}); | ||
const signed = yield this.solWallet.signTransaction(transaction); | ||
const signature = yield this.solConnection.sendRawTransaction(signed.serialize()); | ||
yield this.pluto.paymentIntents.update(paymentIntent.id, { hash: signature }, { plutoAccount: this.plutoAccount }); | ||
return { hash: signature }; | ||
}); | ||
} | ||
} | ||
export function loadPluto(apiKey, options) { | ||
}; | ||
return PlutoJS; | ||
}()); | ||
exports.PlutoJS = PlutoJS; | ||
function loadPluto(apiKey, options) { | ||
return new PlutoJS(apiKey, options); | ||
} | ||
exports.loadPluto = loadPluto; |
@@ -1,1 +0,2 @@ | ||
export {}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
{ | ||
"name": "@plutohq/pluto-js", | ||
"version": "1.6.3", | ||
"version": "1.6.4", | ||
"description": "JS Library for Pluto", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -18,3 +18,3 @@ { | ||
"lib": ["ES2019"], | ||
"target": "es6", | ||
"target": "es5", | ||
"outDir": "dist", | ||
@@ -21,0 +21,0 @@ "rootDir": "src", |
31331
719