cardano-pab-client
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -161,3 +161,3 @@ import type * as SL from "@emurgo/cardano-serialization-lib-browser"; | ||
* CoinSelection Module. | ||
* @module src/lib/CoinSelection | ||
* @packageDocumentation src/lib/CoinSelection | ||
*/ | ||
@@ -164,0 +164,0 @@ declare const CoinSelection: { |
@@ -42,3 +42,3 @@ "use strict"; | ||
* CoinSelection Module. | ||
* @module src/lib/CoinSelection | ||
* @packageDocumentation src/lib/CoinSelection | ||
*/ | ||
@@ -45,0 +45,0 @@ const CoinSelection = { |
@@ -0,1 +1,6 @@ | ||
/** | ||
* @packageDocumentation | ||
* An in-browser balancer with functionality for balancing unbalanced transactions, | ||
* supporting Alonso scripts. | ||
*/ | ||
import type * as S from "@emurgo/cardano-serialization-lib-browser"; | ||
@@ -79,10 +84,10 @@ import { ExecutionUnits, ExportTx, ExportTxInput, Redeemer, WalletInfo } from "../common"; | ||
* unbalanced transaction. | ||
* @param {ExportTx | Omit<ExportTx, "redeemers">} param0 The unbalanced transaction and the | ||
* @param {ExportTx | Omit<ExportTx, "redeemers">} exportTx The unbalanced transaction and the | ||
* script inputs complete information. This format is the same yielded by the PAB. | ||
* @param {WalletInfo} param1 Information about the wallet. | ||
* @param {WalletInfo} walletInfo Information about the wallet. | ||
* @param {BalanceTxConfig & RebalanceTxConfig} config Configuration of both balanceTx and | ||
* rebalanceTx methods within the same object. | ||
* @param calculateExUnits Function that exposes the balanced transaction and the complete | ||
* information of the inputs to allow the calculation of the execution units of each redeemer | ||
* (this calculation could be done using the tx budget service, or with any other method). | ||
* @param {BalanceTxConfig & RebalanceTxConfig} param3 Configuration of both balanceTx and | ||
* rebalanceTx methods merged in the same object. | ||
* @returns The fully-balanced transaction ready to be signed by a wallet. | ||
@@ -94,3 +99,3 @@ */ | ||
* @param unbalancedTx The cbor representation of the unbalanced transaction. | ||
* @param scriptInputsInfo Extra information about the scripts inputs apart from their reference. | ||
* @param inputsInfo Extra information about the scripts inputs apart from their reference. | ||
* @param walletInfo Information about the wallet used to balance the transaction i.e. available | ||
@@ -97,0 +102,0 @@ * UTxOs for paying the fees, its collateral utxo, and address that will receive the change. |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* An in-browser balancer with functionality for balancing unbalanced transactions, | ||
* supporting Alonso scripts. | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
@@ -68,10 +73,10 @@ if (k2 === undefined) k2 = k; | ||
* unbalanced transaction. | ||
* @param {ExportTx | Omit<ExportTx, "redeemers">} param0 The unbalanced transaction and the | ||
* @param {ExportTx | Omit<ExportTx, "redeemers">} exportTx The unbalanced transaction and the | ||
* script inputs complete information. This format is the same yielded by the PAB. | ||
* @param {WalletInfo} param1 Information about the wallet. | ||
* @param {WalletInfo} walletInfo Information about the wallet. | ||
* @param {BalanceTxConfig & RebalanceTxConfig} config Configuration of both balanceTx and | ||
* rebalanceTx methods within the same object. | ||
* @param calculateExUnits Function that exposes the balanced transaction and the complete | ||
* information of the inputs to allow the calculation of the execution units of each redeemer | ||
* (this calculation could be done using the tx budget service, or with any other method). | ||
* @param {BalanceTxConfig & RebalanceTxConfig} param3 Configuration of both balanceTx and | ||
* rebalanceTx methods merged in the same object. | ||
* @returns The fully-balanced transaction ready to be signed by a wallet. | ||
@@ -92,3 +97,3 @@ */ | ||
* @param unbalancedTx The cbor representation of the unbalanced transaction. | ||
* @param scriptInputsInfo Extra information about the scripts inputs apart from their reference. | ||
* @param inputsInfo Extra information about the scripts inputs apart from their reference. | ||
* @param walletInfo Information about the wallet used to balance the transaction i.e. available | ||
@@ -104,3 +109,2 @@ * UTxOs for paying the fees, its collateral utxo, and address that will receive the change. | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// eslint-disable-next-line object-curly-newline | ||
const { linearFee, minUtxo, maxTxSize, costModels } = this.protocolParameters; | ||
@@ -181,6 +185,7 @@ // parse tx and wallet utxos cbors to serialization library structures | ||
let rebalancedTx = this.S.Transaction.from_hex(balancedTx); | ||
const oldFee = Number(rebalancedTx.body().fee().to_str()); | ||
const amountOfVkeyWitnesses = this.calculateNeededVkeyWitnesses(inputsInfo, collateral); | ||
const newFee = calculateFee(rebalancedTx, amountOfVkeyWitnesses, { linearFee, priceStep, priceMem }); | ||
rebalancedTx = this.setFee(rebalancedTx, newFee); | ||
rebalancedTx = this.rebuildChangeOutput(rebalancedTx, newFee, changeAddress, changeOutputIndex); | ||
rebalancedTx = this.rebuildChangeOutput(rebalancedTx, oldFee, newFee, changeAddress, changeOutputIndex); | ||
return rebalancedTx.to_hex(); | ||
@@ -299,3 +304,4 @@ } | ||
if (!change.is_zero()) { | ||
console.log(`PAYING ${(0, serLibPrinter_1.showValue)(change)} OF CHANGE TO ${changeAddress}`); | ||
const changeAddrInBech32 = this.S.Address.from_hex(changeAddress).to_bech32(); | ||
console.log(`PAYING ${(0, serLibPrinter_1.showValue)(change)} OF CHANGE TO ${changeAddrInBech32}`); | ||
if (change.coin().compare(this.S.BigNum.from_str("2")) < 0) { | ||
@@ -308,3 +314,3 @@ // the change value is less than minAda so look for a suitable utxo to receive it | ||
// create a change utxo | ||
outputs.add(this.S.TransactionOutput.new(this.S.Address.from_bech32(changeAddress), change)); | ||
outputs.add(this.S.TransactionOutput.new(this.S.Address.from_hex(changeAddress), change)); | ||
} | ||
@@ -369,6 +375,3 @@ } | ||
const output = outputs.get(i); | ||
// TODO see if to_bech32 without prefix works fine | ||
console.log("output.address().to_bech32()", output.address().to_bech32()); | ||
console.log("compare to", addr); | ||
if (output.address().to_bech32() !== addr) { | ||
if (output.address().to_hex() !== addr) { | ||
result.add(output); | ||
@@ -486,6 +489,5 @@ } | ||
*/ | ||
rebuildChangeOutput(tx, newFee, changeAddress, changeOutputIndex) { | ||
rebuildChangeOutput(tx, oldFee, newFee, changeAddress, changeOutputIndex) { | ||
const outputs = tx.body().outputs(); | ||
const { changeOutput, index } = this.lookupChangeOutputCandidate(outputs, changeAddress, changeOutputIndex); | ||
const oldFee = Number(tx.body().fee().to_str()); | ||
const newChangeOutput = this.buildNewChangeOutput(changeOutput, oldFee, newFee); | ||
@@ -515,5 +517,3 @@ const newOutputs = this.updateNthOutput(outputs, index, newChangeOutput); | ||
const selectedOutput = outputs.get(changeOutputIndex); | ||
// TO DO: CHECK IF THIS COMPARISON WORKS | ||
console.log("Comparing change output address with wallet change address..."); | ||
if (selectedOutput.address().to_hex() === this.S.Address.from_bech32(changeAddress).to_hex()) { | ||
if (selectedOutput.address().to_hex() === changeAddress) { | ||
console.log("Change output:", (0, serLibPrinter_1.showTransactionOutput)(selectedOutput)); | ||
@@ -526,4 +526,4 @@ return { | ||
throw new Error(`The address of the selected change output doesn't match with the change address of the wallet wallet: | ||
Change output address: ${outputs.get(changeOutputIndex).address().to_hex()} | ||
Change address of the wallet in hex: ${this.S.Address.from_bech32(changeAddress).to_hex()}`); | ||
Change output address: ${outputs.get(changeOutputIndex).address().to_bech32()} | ||
Change address of the wallet in bech32: ${this.S.Address.from_hex(changeAddress).to_bech32()}`); | ||
} | ||
@@ -537,3 +537,3 @@ else { | ||
const currentOutput = outputs.get(currentIndex); | ||
if (currentOutput.address().to_hex() === this.S.Address.from_bech32(changeAddress).to_hex()) { | ||
if (currentOutput.address().to_hex() === changeAddress) { | ||
// the current output is a suitable change output | ||
@@ -547,3 +547,3 @@ return { | ||
throw new Error(`No suitable output change found. | ||
Change address of the wallet: ${this.S.Address.from_bech32(changeAddress).to_hex()} | ||
Change address of the wallet in bech32: ${this.S.Address.from_hex(changeAddress).to_bech32()} | ||
Outputs: ${(0, serLibPrinter_1.showTransactionOutputs)(outputs)} | ||
@@ -550,0 +550,0 @@ `); |
@@ -0,1 +1,7 @@ | ||
/** | ||
* @packageDocumentation | ||
* A module to pretty-print serialization library objects. This was completely | ||
* necessary when the serialziation library doesn't had methods to convert to js values. So it could | ||
* be reviewed since for the version of the ser lib that we'll use now may not be necessary. | ||
*/ | ||
import type * as S from "@emurgo/cardano-serialization-lib-browser"; | ||
@@ -2,0 +8,0 @@ import * as T from "./types"; |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* A module to pretty-print serialization library objects. This was completely | ||
* necessary when the serialziation library doesn't had methods to convert to js values. So it could | ||
* be reviewed since for the version of the ser lib that we'll use now may not be necessary. | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
@@ -3,0 +9,0 @@ if (k2 === undefined) k2 = k; |
@@ -31,6 +31,8 @@ import type * as S from "@emurgo/cardano-serialization-lib-browser"; | ||
/** | ||
* Asynchronous method to initialize a Balancer instance. Needed like this because of the | ||
* Asynchronous method to initialize a TxInputsInfo instance. Needed like this because of the | ||
* loading of the serialization library. | ||
* @param protocolParameters The protocol parameters subset that the Balancer needs. | ||
* @returns A Balancer instance wrapped into a Promise. | ||
* @param transaction Transaction in cbor format. | ||
* @param scriptInputsInfo Complete information about the script inputs. | ||
* @param walletUTxOs Wallet utxos used to complete the information about the wallet inputs of the | ||
* transaction. | ||
*/ | ||
@@ -37,0 +39,0 @@ static init(transaction: string, scriptInputsInfo: ExportTxInput[], walletUTxOs: string[]): Promise<TxInputsInfo>; |
@@ -102,6 +102,8 @@ "use strict"; | ||
/** | ||
* Asynchronous method to initialize a Balancer instance. Needed like this because of the | ||
* Asynchronous method to initialize a TxInputsInfo instance. Needed like this because of the | ||
* loading of the serialization library. | ||
* @param protocolParameters The protocol parameters subset that the Balancer needs. | ||
* @returns A Balancer instance wrapped into a Promise. | ||
* @param transaction Transaction in cbor format. | ||
* @param scriptInputsInfo Complete information about the script inputs. | ||
* @param walletUTxOs Wallet utxos used to complete the information about the wallet inputs of the | ||
* transaction. | ||
*/ | ||
@@ -108,0 +110,0 @@ static init(transaction, scriptInputsInfo, walletUTxOs) { |
@@ -0,3 +1,12 @@ | ||
/** | ||
* @packageDocumentation | ||
* A CIP30 Wallet wrapper providing the necessary methods for retrieving information from | ||
* wallets and submitting transactions. | ||
*/ | ||
import type * as S from "@emurgo/cardano-serialization-lib-browser"; | ||
import { Address, Hash64, Paginate, Result, TransactionUnspentOutput, TransactionWitnessSet, Value, WalletInfo } from "../common"; | ||
/** | ||
* Class that has the CIP-30 wallet methods plus some other useful ones for the usage of the | ||
* library itself, such as `getWalletInfo` and `signAndSubmit`. | ||
*/ | ||
export default class CIP30WalletWrapper { | ||
@@ -17,11 +26,41 @@ private wallet; | ||
static init(walletObject: unknown): Promise<CIP30WalletWrapper>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetnetworkidpromisenumber. | ||
*/ | ||
getNetworkId(): Promise<number>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetutxosamountcborvalueundefinedpaginatepaginateundefinedpromisetransactionunspentoutputnull. | ||
*/ | ||
getUtxos(amount?: Value, paginate?: Paginate): Promise<TransactionUnspentOutput[] | null>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetcollateralparamsamountcborcoinpromisetransactionunspentoutputnull. | ||
*/ | ||
getCollateral(): Promise<TransactionUnspentOutput[] | null>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetbalancepromisecborvalue. | ||
*/ | ||
getBalance(): Promise<Value>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetusedaddressespaginatepaginateundefinedpromiseaddress. | ||
*/ | ||
getUsedAddresses(paginate?: Paginate): Promise<Address[]>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetunusedaddressespromiseaddress. | ||
*/ | ||
getUnusedAddresses(): Promise<Address[]>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetchangeaddresspromiseaddress. | ||
*/ | ||
getChangeAddress(): Promise<Address>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetrewardaddressespromiseaddress. | ||
*/ | ||
getRewardAddresses(): Promise<Address[]>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apisigntxtxcbortransactionpartialsignboolfalsepromisecbortransactionwitnessset. | ||
*/ | ||
signTx(tx: string, partialSign?: boolean): Promise<TransactionWitnessSet>; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apisubmittxtxcbortransactionpromisehash32. | ||
*/ | ||
submitTx(tx: string): Promise<Hash64>; | ||
@@ -41,2 +80,16 @@ getWalletId(): Promise<string>; | ||
signAndSubmit(transaction: string): Promise<Result<Hash64>>; | ||
/** | ||
* Auxiliar function to catch possible errors of the `signTx` method of the wallet and attach | ||
* the witness given by it (since signTx only returns the witnesses, the attaching to the tx is | ||
* done here). | ||
* @param transaction The transaction cbor. | ||
* @returns The transaction with the witnesses attached to it or an error if something went wrong. | ||
*/ | ||
private signTxCheckedAndAttachItToTheTx; | ||
/** | ||
* Auxiliar function to catch possible errors of the `submitTx` method of the wallet. | ||
* @param transaction The transaction cbor. | ||
* @returns The hash of the transaction succesfully submited, or an error if happened. | ||
*/ | ||
private submitTxChecked; | ||
} |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* A CIP30 Wallet wrapper providing the necessary methods for retrieving information from | ||
* wallets and submitting transactions. | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
@@ -40,2 +45,6 @@ if (k2 === undefined) k2 = k; | ||
const common_1 = require("../common"); | ||
/** | ||
* Class that has the CIP-30 wallet methods plus some other useful ones for the usage of the | ||
* library itself, such as `getWalletInfo` and `signAndSubmit`. | ||
*/ | ||
class CIP30WalletWrapper { | ||
@@ -62,2 +71,5 @@ constructor(walletObject, serLib) { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetnetworkidpromisenumber. | ||
*/ | ||
getNetworkId() { | ||
@@ -68,2 +80,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetutxosamountcborvalueundefinedpaginatepaginateundefinedpromisetransactionunspentoutputnull. | ||
*/ | ||
getUtxos(amount, paginate) { | ||
@@ -74,2 +89,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetcollateralparamsamountcborcoinpromisetransactionunspentoutputnull. | ||
*/ | ||
getCollateral() { | ||
@@ -80,2 +98,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetbalancepromisecborvalue. | ||
*/ | ||
getBalance() { | ||
@@ -86,2 +107,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetusedaddressespaginatepaginateundefinedpromiseaddress. | ||
*/ | ||
getUsedAddresses(paginate) { | ||
@@ -92,2 +116,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetunusedaddressespromiseaddress. | ||
*/ | ||
getUnusedAddresses() { | ||
@@ -98,2 +125,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetchangeaddresspromiseaddress. | ||
*/ | ||
getChangeAddress() { | ||
@@ -104,2 +134,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apigetrewardaddressespromiseaddress. | ||
*/ | ||
getRewardAddresses() { | ||
@@ -110,2 +143,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apisigntxtxcbortransactionpartialsignboolfalsepromisecbortransactionwitnessset. | ||
*/ | ||
signTx(tx, partialSign) { | ||
@@ -116,2 +152,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#apisubmittxtxcbortransactionpromisehash32. | ||
*/ | ||
submitTx(tx) { | ||
@@ -158,2 +197,24 @@ return __awaiter(this, void 0, void 0, function* () { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield this.signTxCheckedAndAttachItToTheTx(transaction); | ||
if ((0, common_1.failed)(result)) { | ||
return result; | ||
} | ||
const signedTxCbor = result.value; | ||
console.log("Signed tx:", signedTxCbor); | ||
const txSubmission = yield this.submitTxChecked(signedTxCbor); | ||
if ((0, common_1.failed)(txSubmission)) { | ||
return txSubmission; | ||
} | ||
return txSubmission; | ||
}); | ||
} | ||
/** | ||
* Auxiliar function to catch possible errors of the `signTx` method of the wallet and attach | ||
* the witness given by it (since signTx only returns the witnesses, the attaching to the tx is | ||
* done here). | ||
* @param transaction The transaction cbor. | ||
* @returns The transaction with the witnesses attached to it or an error if something went wrong. | ||
*/ | ||
signTxCheckedAndAttachItToTheTx(transaction) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let signTxResponse; | ||
@@ -185,8 +246,15 @@ try { | ||
const signedTx = this.S.Transaction.new(tx.body(), witnessSet, tx.auxiliary_data()); | ||
const signedTxCbor = signedTx.to_hex(); | ||
console.log("Signed tx:", signedTxCbor); | ||
// now submit the tx | ||
return (0, common_1.success)(signedTx.to_hex()); | ||
}); | ||
} | ||
/** | ||
* Auxiliar function to catch possible errors of the `submitTx` method of the wallet. | ||
* @param transaction The transaction cbor. | ||
* @returns The hash of the transaction succesfully submited, or an error if happened. | ||
*/ | ||
submitTxChecked(transaction) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let submitTxResponse; | ||
try { | ||
submitTxResponse = yield this.submitTx(signedTxCbor); | ||
submitTxResponse = yield this.submitTx(transaction); | ||
} | ||
@@ -193,0 +261,0 @@ catch (error) { |
/** | ||
* @packageDocumentation | ||
* Contains declarations shared between various modules of the library. | ||
*/ | ||
/** | ||
* Type definition for a CIP30-compliant wallet object. | ||
@@ -77,3 +81,3 @@ * See https://cips.cardano.org/cips/cip30/#fullapi. | ||
}; | ||
export declare type PabStatus = { | ||
export declare type PABStatus = { | ||
cicDefinition: { | ||
@@ -80,0 +84,0 @@ tag: string; |
"use strict"; | ||
/* | ||
* Module that contains definitions shared between various modules. | ||
/** | ||
* @packageDocumentation | ||
* Contains declarations shared between various modules of the library. | ||
*/ | ||
@@ -5,0 +6,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,1 +1,10 @@ | ||
/** | ||
* @packageDocumentation | ||
* A class that uses the PAB to interact with contracts implemented with a | ||
* specific design pattern. This can be resumed like so | ||
* - `start` to initialize a new contract. | ||
* - `connect` to connect to an already existing contract instance. | ||
* - `reload` to get the observable state defined by the contract in the Haskell off-chain code. | ||
* - `doOperation` for getting the unbalanced transaction of any other operation. | ||
*/ | ||
import PABApi from "./PABApi"; | ||
@@ -8,28 +17,36 @@ import { EndpointCall, ExportTx, Result, ContractId } from "./common"; | ||
/** | ||
* Create a new instance of a contract and obtain the required transaction. | ||
* @param walletId Id of the wallet being used to activate the contract | ||
* @param callInit Name of the endpoint and its parameters to activate the contract | ||
* @param pabApi Api instance for this contract to use. | ||
* @returns a new instance of the class and an unbalanced transaction to initialice the contract. | ||
*/ | ||
* Create a new instance of a contract and obtain its unbalanced transaction. | ||
* @param walletId Id of the wallet being used to activate the contract. | ||
* @param callInit Name of the endpoint and its parameters to activate the contract. | ||
* @param pabApi Api instance for this contract to use. | ||
* @returns A new instance of the class and an unbalanced transaction to initiate the contract. | ||
*/ | ||
static start(walletId: string, callInit: EndpointCall<unknown>, pabApi: PABApi): Promise<[ContractEndpoints, Result<ExportTx>]>; | ||
/** | ||
* Connect to an already created instance of a contract on the blockchain. | ||
* @param walletId Id of the wallet being used to activate the contract | ||
* @param callConnect Name of the endpoint and its parameters to connect to the contract | ||
* @param pabApi Api instance for this contract to use. | ||
* @returns a new instance of the class. | ||
*/ | ||
* Connect to an already created instance of a contract on the blockchain. | ||
* @param walletId Id of the wallet being used to activate the contract. | ||
* @param callConnect Name of the endpoint and its parameters to connect to the contract. | ||
* @param pabApi PAB API instance for this contract to use. | ||
* @returns a new instance of the class. | ||
*/ | ||
static connect(walletId: string, callConnect: EndpointCall<unknown>, pabApi: PABApi): Promise<ContractEndpoints>; | ||
/** | ||
* Gets the observable state contained in the status of the PAB. | ||
* @param callReload Name of the reload endpoint and its required parameters. | ||
* @returns The observable state if suceeded, or a string error otherwise. | ||
*/ | ||
reload(callReload: EndpointCall<unknown>): Promise<Result<unknown>>; | ||
/** | ||
* Create a new instance of a contract and obtain the required transaction. | ||
* @param callEndpoint Name of the endpoint and its parameters to perform an operation | ||
* @returns the unbalanced transaction to perform the operation. | ||
*/ | ||
* Performs the specified operation. | ||
* @param callEndpoint Name of the endpoint and its parameters. | ||
* @returns The unbalanced transaction of the given operation or an error if something went wrong. | ||
*/ | ||
doOperation(callEndpoint: EndpointCall<unknown>): Promise<Result<ExportTx>>; | ||
/** | ||
* Stop the contract instance on the PAB. | ||
*/ | ||
* Stops the contract instance on the PAB. | ||
*/ | ||
stop(): Promise<void>; | ||
/** | ||
* @returns The observable state from the PAB status. | ||
*/ | ||
private getReloadStatus; | ||
@@ -36,0 +53,0 @@ private waitForNewExportTx; |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* A class that uses the PAB to interact with contracts implemented with a | ||
* specific design pattern. This can be resumed like so | ||
* - `start` to initialize a new contract. | ||
* - `connect` to connect to an already existing contract instance. | ||
* - `reload` to get the observable state defined by the contract in the Haskell off-chain code. | ||
* - `doOperation` for getting the unbalanced transaction of any other operation. | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -19,8 +28,8 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
/** | ||
* Create a new instance of a contract and obtain the required transaction. | ||
* @param walletId Id of the wallet being used to activate the contract | ||
* @param callInit Name of the endpoint and its parameters to activate the contract | ||
* @param pabApi Api instance for this contract to use. | ||
* @returns a new instance of the class and an unbalanced transaction to initialice the contract. | ||
*/ | ||
* Create a new instance of a contract and obtain its unbalanced transaction. | ||
* @param walletId Id of the wallet being used to activate the contract. | ||
* @param callInit Name of the endpoint and its parameters to activate the contract. | ||
* @param pabApi Api instance for this contract to use. | ||
* @returns A new instance of the class and an unbalanced transaction to initiate the contract. | ||
*/ | ||
static start(walletId, callInit, pabApi) { | ||
@@ -35,8 +44,8 @@ return __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
* Connect to an already created instance of a contract on the blockchain. | ||
* @param walletId Id of the wallet being used to activate the contract | ||
* @param callConnect Name of the endpoint and its parameters to connect to the contract | ||
* @param pabApi Api instance for this contract to use. | ||
* @returns a new instance of the class. | ||
*/ | ||
* Connect to an already created instance of a contract on the blockchain. | ||
* @param walletId Id of the wallet being used to activate the contract. | ||
* @param callConnect Name of the endpoint and its parameters to connect to the contract. | ||
* @param pabApi PAB API instance for this contract to use. | ||
* @returns a new instance of the class. | ||
*/ | ||
static connect(walletId, callConnect, pabApi) { | ||
@@ -49,2 +58,7 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* Gets the observable state contained in the status of the PAB. | ||
* @param callReload Name of the reload endpoint and its required parameters. | ||
* @returns The observable state if suceeded, or a string error otherwise. | ||
*/ | ||
reload(callReload) { | ||
@@ -58,6 +72,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
* Create a new instance of a contract and obtain the required transaction. | ||
* @param callEndpoint Name of the endpoint and its parameters to perform an operation | ||
* @returns the unbalanced transaction to perform the operation. | ||
*/ | ||
* Performs the specified operation. | ||
* @param callEndpoint Name of the endpoint and its parameters. | ||
* @returns The unbalanced transaction of the given operation or an error if something went wrong. | ||
*/ | ||
doOperation(callEndpoint) { | ||
@@ -72,4 +86,4 @@ return __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
* Stop the contract instance on the PAB. | ||
*/ | ||
* Stops the contract instance on the PAB. | ||
*/ | ||
stop() { | ||
@@ -80,2 +94,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* @returns The observable state from the PAB status. | ||
*/ | ||
getReloadStatus() { | ||
@@ -100,5 +117,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const currStErr = oldStatus ? oldStatus.cicCurrentState.err : null; | ||
const status = yield this.pollStatusUntil((statusPar) => (statusPar.cicYieldedExportTxs.length !== yieldLen | ||
|| (statusPar.cicCurrentState.logs.length !== logLen | ||
&& (0, common_1.last)(statusPar.cicCurrentState.logs)._logLevel === "Error") || statusPar.cicCurrentState.err !== currStErr)); | ||
const status = yield this.pollStatusUntil((_status) => (_status.cicYieldedExportTxs.length !== yieldLen | ||
|| (_status.cicCurrentState.logs.length !== logLen | ||
&& (0, common_1.last)(_status.cicCurrentState.logs)._logLevel === "Error") || _status.cicCurrentState.err !== currStErr)); | ||
if (status.cicYieldedExportTxs.length !== yieldLen) { | ||
@@ -105,0 +122,0 @@ return (0, common_1.success)((0, common_1.last)(status.cicYieldedExportTxs)); |
@@ -0,8 +1,14 @@ | ||
/** | ||
* @packageDocumentation | ||
* The top-level module of the library. Exports the functionalities of the entire library. | ||
*/ | ||
import CIP30WalletWrapper from "./CIP30WalletWrapper"; | ||
import Balancer from "./Balancer"; | ||
import CIP30WalletWrapper from "./CIP30WalletWrapper"; | ||
import PABApi from "./PABApi"; | ||
import ContractEndpoints from "./ContractEndpoints"; | ||
import TxBudgetAPI from "./TxBudgetApi"; | ||
import ContractEndpoints from "./ContractEndpoints"; | ||
import PABApi from "./PABApi"; | ||
import SerLibLoader from "./SerLibLoader"; | ||
import TxInputsInfo from "./Balancer/TxInputsInfo"; | ||
import { getWalletInitialAPI, getProtocolParamsFromBlockfrost } from "./utilities"; | ||
export { CIP30WalletWrapper, Balancer, PABApi, ContractEndpoints, TxBudgetAPI, SerLibLoader, getWalletInitialAPI, getProtocolParamsFromBlockfrost, }; | ||
import { failed, succeeded } from "./common"; | ||
export { CIP30WalletWrapper, Balancer, PABApi, ContractEndpoints, TxBudgetAPI, SerLibLoader, TxInputsInfo, getWalletInitialAPI, getProtocolParamsFromBlockfrost, succeeded, failed, }; |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* The top-level module of the library. Exports the functionalities of the entire library. | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,17 +10,22 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getProtocolParamsFromBlockfrost = exports.getWalletInitialAPI = exports.SerLibLoader = exports.TxBudgetAPI = exports.ContractEndpoints = exports.PABApi = exports.Balancer = exports.CIP30WalletWrapper = void 0; | ||
exports.failed = exports.succeeded = exports.getProtocolParamsFromBlockfrost = exports.getWalletInitialAPI = exports.TxInputsInfo = exports.SerLibLoader = exports.TxBudgetAPI = exports.ContractEndpoints = exports.PABApi = exports.Balancer = exports.CIP30WalletWrapper = void 0; | ||
const CIP30WalletWrapper_1 = __importDefault(require("./CIP30WalletWrapper")); | ||
exports.CIP30WalletWrapper = CIP30WalletWrapper_1.default; | ||
const Balancer_1 = __importDefault(require("./Balancer")); | ||
exports.Balancer = Balancer_1.default; | ||
const CIP30WalletWrapper_1 = __importDefault(require("./CIP30WalletWrapper")); | ||
exports.CIP30WalletWrapper = CIP30WalletWrapper_1.default; | ||
const PABApi_1 = __importDefault(require("./PABApi")); | ||
exports.PABApi = PABApi_1.default; | ||
const ContractEndpoints_1 = __importDefault(require("./ContractEndpoints")); | ||
exports.ContractEndpoints = ContractEndpoints_1.default; | ||
const TxBudgetApi_1 = __importDefault(require("./TxBudgetApi")); | ||
exports.TxBudgetAPI = TxBudgetApi_1.default; | ||
const ContractEndpoints_1 = __importDefault(require("./ContractEndpoints")); | ||
exports.ContractEndpoints = ContractEndpoints_1.default; | ||
const PABApi_1 = __importDefault(require("./PABApi")); | ||
exports.PABApi = PABApi_1.default; | ||
const SerLibLoader_1 = __importDefault(require("./SerLibLoader")); | ||
exports.SerLibLoader = SerLibLoader_1.default; | ||
const TxInputsInfo_1 = __importDefault(require("./Balancer/TxInputsInfo")); | ||
exports.TxInputsInfo = TxInputsInfo_1.default; | ||
const utilities_1 = require("./utilities"); | ||
Object.defineProperty(exports, "getWalletInitialAPI", { enumerable: true, get: function () { return utilities_1.getWalletInitialAPI; } }); | ||
Object.defineProperty(exports, "getProtocolParamsFromBlockfrost", { enumerable: true, get: function () { return utilities_1.getProtocolParamsFromBlockfrost; } }); | ||
const common_1 = require("./common"); | ||
Object.defineProperty(exports, "failed", { enumerable: true, get: function () { return common_1.failed; } }); | ||
Object.defineProperty(exports, "succeeded", { enumerable: true, get: function () { return common_1.succeeded; } }); |
@@ -1,29 +0,40 @@ | ||
import { PabStatus, ContractId, EndpointCall } from "./common"; | ||
/** | ||
* @packageDocumentation | ||
* An API for abstracting the PAB, making easy the connection between a frontend and the | ||
* queries and responses from the PAB. | ||
*/ | ||
import { PABStatus, ContractId, EndpointCall } from "./common"; | ||
/** | ||
* A class that contains the PAB endpoints as its methods. | ||
*/ | ||
export default class PABApi { | ||
pabUrl: string; | ||
/** | ||
* @param pabUrl PAB base url. | ||
*/ | ||
constructor(pabUrl: string); | ||
/** | ||
* Wrapper of the activate endpoint of the PAB. | ||
* @param walletId Id of the wallet being used to activate the contract | ||
* @param callActivate name of the endpoint and its parameters for activate the contract | ||
* @returns a contract ID to the activated contract. | ||
*/ | ||
* Wrapper of the activate endpoint of the PAB. | ||
* @param walletId Id of the wallet being used to activate the contract. | ||
* @param callActivate Name of the endpoint and its parameters for activate the contract. | ||
* @returns A contract ID to the activated contract. | ||
*/ | ||
activate(walletId: string, callActivate: EndpointCall<unknown>): Promise<ContractId>; | ||
/** | ||
* Wrapper of the stop endpoint of the PAB. | ||
* @param contractId Id of the contract being stopped. | ||
*/ | ||
* Wrapper of the stop endpoint of the PAB. | ||
* @param contractId The contract instance ID being stopped. | ||
*/ | ||
stop(contractId: ContractId): Promise<void>; | ||
/** | ||
* Performs the specified operation in a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @param callEndpoint Name of the endpoint being called and its parameters. | ||
*/ | ||
* Performs the specified operation in a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @param callEndpoint Name of the endpoint being called and its parameters. | ||
*/ | ||
endpoint(contractId: ContractId, callEndpoint: EndpointCall<unknown>): Promise<void>; | ||
/** | ||
* Obtains and returns the status of a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @returns The complete status response from the PAB endpoint | ||
*/ | ||
status(contractId: ContractId): Promise<PabStatus>; | ||
* Obtains and returns the status of a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @returns The complete status response from the PAB endpoint | ||
*/ | ||
status(contractId: ContractId): Promise<PABStatus>; | ||
} |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* An API for abstracting the PAB, making easy the connection between a frontend and the | ||
* queries and responses from the PAB. | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -16,3 +21,9 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
const axios_1 = __importDefault(require("axios")); | ||
/** | ||
* A class that contains the PAB endpoints as its methods. | ||
*/ | ||
class PABApi { | ||
/** | ||
* @param pabUrl PAB base url. | ||
*/ | ||
constructor(pabUrl) { | ||
@@ -22,7 +33,7 @@ this.pabUrl = pabUrl; | ||
/** | ||
* Wrapper of the activate endpoint of the PAB. | ||
* @param walletId Id of the wallet being used to activate the contract | ||
* @param callActivate name of the endpoint and its parameters for activate the contract | ||
* @returns a contract ID to the activated contract. | ||
*/ | ||
* Wrapper of the activate endpoint of the PAB. | ||
* @param walletId Id of the wallet being used to activate the contract. | ||
* @param callActivate Name of the endpoint and its parameters for activate the contract. | ||
* @returns A contract ID to the activated contract. | ||
*/ | ||
activate(walletId, callActivate) { | ||
@@ -47,5 +58,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
* Wrapper of the stop endpoint of the PAB. | ||
* @param contractId Id of the contract being stopped. | ||
*/ | ||
* Wrapper of the stop endpoint of the PAB. | ||
* @param contractId The contract instance ID being stopped. | ||
*/ | ||
stop(contractId) { | ||
@@ -60,6 +71,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
* Performs the specified operation in a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @param callEndpoint Name of the endpoint being called and its parameters. | ||
*/ | ||
* Performs the specified operation in a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @param callEndpoint Name of the endpoint being called and its parameters. | ||
*/ | ||
endpoint(contractId, callEndpoint) { | ||
@@ -76,6 +87,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
* Obtains and returns the status of a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @returns The complete status response from the PAB endpoint | ||
*/ | ||
* Obtains and returns the status of a contract. | ||
* @param contractId Id of the instance of the contract on which the endpoint is being called. | ||
* @returns The complete status response from the PAB endpoint | ||
*/ | ||
status(contractId) { | ||
@@ -82,0 +93,0 @@ return __awaiter(this, void 0, void 0, function* () { |
@@ -7,3 +7,9 @@ import type * as S from "@emurgo/cardano-serialization-lib-browser"; | ||
private wasm; | ||
/** | ||
* Dynamically imports the serialziation library wasm module. | ||
*/ | ||
load(): Promise<void>; | ||
/** | ||
* Gets the serialization library as an object. | ||
*/ | ||
get lib(): typeof S; | ||
@@ -10,0 +16,0 @@ } |
@@ -39,2 +39,5 @@ "use strict"; | ||
class SerLibLoader { | ||
/** | ||
* Dynamically imports the serialziation library wasm module. | ||
*/ | ||
load() { | ||
@@ -55,2 +58,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* Gets the serialization library as an object. | ||
*/ | ||
get lib() { | ||
@@ -57,0 +63,0 @@ if (!this.wasm) { |
@@ -0,13 +1,43 @@ | ||
/** | ||
* @packageDocumentation | ||
* An API for connecting frontend code with a tx budget service, needed for calculating | ||
* mem and cpu units of Alonso scripts. | ||
*/ | ||
import { ExecutionUnits, ExportTxInput, Redeemer, Result } from "./common"; | ||
declare type TxBudgetAPIConfig = { | ||
baseUrl: string; | ||
/** | ||
* The service runs the validator/s for calculating mem and CPU units. | ||
* If the given tx fails in the validation phase, we can either throw an error | ||
* OR just return some mock units to set them in the tx anyway. | ||
* If you submit a transaction that fails in validation phase to the blockchain, | ||
* will consume the collateral utxo. | ||
*/ | ||
returnExUnitsIfValidatorFails?: boolean; | ||
/** | ||
* The request to the service is halted if this timeout passed with no response. Defaults | ||
* to 10 secs. | ||
*/ | ||
timeout?: number; | ||
}; | ||
/** | ||
* Class that exposes methods to interact with the Tx budget service API. | ||
*/ | ||
export default class TxBudgetAPI { | ||
config: TxBudgetAPIConfig; | ||
static defConf: TxBudgetAPIConfig; | ||
static defaultConfig: TxBudgetAPIConfig; | ||
/** | ||
* @param config The desired configuration. The specified fields overwrite a default | ||
* configuration. | ||
*/ | ||
constructor(config: TxBudgetAPIConfig); | ||
/** | ||
* Endpoint to estimate the execution units of each redeemer of the given transaction. | ||
* @param tx The transaction cbor. | ||
* @param inputsInfo Complete information about the inputs of the transaction. | ||
* @returns If suceeded, returns the asociated execution units (mem and cpu units) to each | ||
* of the redeemers (represented as a tag and index). | ||
*/ | ||
estimate(tx: string, inputsInfo: ExportTxInput[]): Promise<Result<Array<[Redeemer, ExecutionUnits]>>>; | ||
} | ||
export {}; |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* An API for connecting frontend code with a tx budget service, needed for calculating | ||
* mem and cpu units of Alonso scripts. | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -21,8 +26,22 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
} | ||
/** | ||
* Class that exposes methods to interact with the Tx budget service API. | ||
*/ | ||
class TxBudgetAPI { | ||
/** | ||
* @param config The desired configuration. The specified fields overwrite a default | ||
* configuration. | ||
*/ | ||
constructor(config) { | ||
this.config = Object.assign(Object.assign({}, TxBudgetAPI.defConf), config); | ||
this.config = Object.assign(Object.assign({}, TxBudgetAPI.defaultConfig), config); | ||
} | ||
// // GET request method for checking if the service is up | ||
// public async health() { ... } | ||
/** | ||
* Endpoint to estimate the execution units of each redeemer of the given transaction. | ||
* @param tx The transaction cbor. | ||
* @param inputsInfo Complete information about the inputs of the transaction. | ||
* @returns If suceeded, returns the asociated execution units (mem and cpu units) to each | ||
* of the redeemers (represented as a tag and index). | ||
*/ | ||
estimate(tx, inputsInfo) { | ||
@@ -81,6 +100,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
exports.default = TxBudgetAPI; | ||
TxBudgetAPI.defConf = { | ||
baseUrl: "localhost:3001", | ||
TxBudgetAPI.defaultConfig = { | ||
baseUrl: "http://localhost:3001", | ||
returnExUnitsIfValidatorFails: false, | ||
timeout: 10000, | ||
}; |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @packageDocumentation | ||
* An utilities module that exposes useful functionalities for a dApp frontend. | ||
*/ | ||
import { ProtocolParameters } from "./Balancer"; | ||
@@ -21,3 +25,3 @@ import { CIP30WalletAPI } from "./common"; | ||
*/ | ||
declare type SupportedWallet = "eternl" | "nami"; | ||
declare type SupportedWallet = "eternl" | "nami" | "ccvault"; | ||
/** | ||
@@ -29,3 +33,3 @@ * See https://cips.cardano.org/cips/cip30/#initialapi. | ||
isEnabled: () => Promise<boolean>; | ||
name: () => SupportedWallet; | ||
name: () => string; | ||
icon: () => string; | ||
@@ -32,0 +36,0 @@ apiVersion: () => string; |
"use strict"; | ||
/** | ||
* @packageDocumentation | ||
* An utilities module that exposes useful functionalities for a dApp frontend. | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -3,0 +7,0 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
{ | ||
"name": "cardano-pab-client", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "A set of tools to develop frontends that interact with the Plutus Application Backend.", | ||
@@ -11,3 +11,4 @@ "main": "dist/src/index.js", | ||
"lint": "./node_modules/.bin/eslint . --ext .ts", | ||
"local-package": "rm -rf dist && npm run build && npm pack" | ||
"local-package": "rm -rf dist && npm run build && npm pack", | ||
"generate-docs": "npx typedoc --plugin typedoc-plugin-markdown --plugin typedoc-plugin-missing-exports" | ||
}, | ||
@@ -24,3 +25,3 @@ "repository": { | ||
"author": "Plank", | ||
"license": "ISC", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
@@ -41,2 +42,5 @@ "url": "https://github.com/joinplank/cardano-pab-client/issues" | ||
"ts-jest": "^29.0.3", | ||
"typedoc": "^0.23.22", | ||
"typedoc-plugin-markdown": "^3.14.0", | ||
"typedoc-plugin-missing-exports": "^1.0.0", | ||
"typescript": "^4.8.4" | ||
@@ -43,0 +47,0 @@ }, |
248269
56
3841
14