cardano-pab-client
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -148,8 +148,8 @@ import type * as SL from "@emurgo/cardano-serialization-lib-browser"; | ||
*/ | ||
declare type AmountList = SL.Value[]; | ||
declare type UTxOList = SL.TransactionUnspentOutput[]; | ||
type AmountList = SL.Value[]; | ||
type UTxOList = SL.TransactionUnspentOutput[]; | ||
/** | ||
* Coin Selection algorithm return | ||
*/ | ||
declare type SelectionResult = { | ||
type SelectionResult = { | ||
input: UTxOList; | ||
@@ -204,3 +204,3 @@ remaining: UTxOList; | ||
*/ | ||
declare function compare(group: SL.Value, candidate: SL.Value): 1 | -1 | 0 | undefined; | ||
declare function compare(group: SL.Value, candidate: SL.Value): 0 | 1 | -1 | undefined; | ||
export default CoinSelection; |
@@ -42,3 +42,3 @@ /** | ||
*/ | ||
declare type BalanceTxConfig = { | ||
type BalanceTxConfig = { | ||
/** | ||
@@ -57,3 +57,3 @@ * The fee amount that is used by the balancing process. It determines important things like | ||
}; | ||
declare type RebalanceTxConfig = { | ||
type RebalanceTxConfig = { | ||
/** | ||
@@ -60,0 +60,0 @@ * If specified, the rebalancing will take a specific output of the transaction as the one that |
@@ -9,3 +9,3 @@ /** | ||
*/ | ||
export declare type CIP30WalletAPI = { | ||
export type CIP30WalletAPI = { | ||
getNetworkId: () => Promise<number>; | ||
@@ -22,12 +22,12 @@ getUtxos: (amount?: Value, paginate?: Paginate) => Promise<TransactionUnspentOutput[] | null>; | ||
}; | ||
export declare type TransactionUnspentOutput = string; | ||
export declare type Hash64 = string; | ||
export declare type TransactionWitnessSet = string; | ||
export declare type Address = string; | ||
export declare type Value = string; | ||
export declare type Paginate = { | ||
export type TransactionUnspentOutput = string; | ||
export type Hash64 = string; | ||
export type TransactionWitnessSet = string; | ||
export type Address = string; | ||
export type Value = string; | ||
export type Paginate = { | ||
page: number; | ||
limit: number; | ||
}; | ||
export declare type WalletInfo = { | ||
export type WalletInfo = { | ||
utxos: TransactionUnspentOutput[]; | ||
@@ -37,3 +37,3 @@ collateral: TransactionUnspentOutput[]; | ||
}; | ||
declare type SpendingRedeemer = { | ||
type SpendingRedeemer = { | ||
purpose: "spending"; | ||
@@ -46,3 +46,3 @@ data: string; | ||
}; | ||
declare type MintingRedeemer = { | ||
type MintingRedeemer = { | ||
purpose: "minting"; | ||
@@ -52,8 +52,8 @@ data: string; | ||
}; | ||
export declare type ExportTxRedeemer = SpendingRedeemer | MintingRedeemer; | ||
export declare type Amount = { | ||
export type ExportTxRedeemer = SpendingRedeemer | MintingRedeemer; | ||
export type Amount = { | ||
unit: string; | ||
quantity: number; | ||
}; | ||
export declare type Asset = { | ||
export type Asset = { | ||
asset_name: string; | ||
@@ -63,4 +63,4 @@ quantity: number; | ||
}; | ||
export declare type Assets = Array<Asset>; | ||
export declare type ExportTxInput = { | ||
export type Assets = Array<Asset>; | ||
export type ExportTxInput = { | ||
id: string; | ||
@@ -73,3 +73,3 @@ index: number; | ||
}; | ||
export declare type ExportTx = { | ||
export type ExportTx = { | ||
transaction: string; | ||
@@ -79,7 +79,7 @@ inputs: ExportTxInput[]; | ||
}; | ||
export declare type PabLog = { | ||
export type PabLog = { | ||
"_logLevel": string; | ||
"_logMessageContent": string; | ||
}; | ||
export declare type PABStatus = { | ||
export type PABStatus = { | ||
cicDefinition: { | ||
@@ -106,20 +106,20 @@ tag: string; | ||
}; | ||
export declare type ContractId = string; | ||
export declare type Redeemer = { | ||
export type ContractId = string; | ||
export type Redeemer = { | ||
index: number; | ||
tag: "Mint" | "Spend"; | ||
}; | ||
export declare type ExecutionUnits = { | ||
export type ExecutionUnits = { | ||
mem: number; | ||
cpu: number; | ||
}; | ||
export declare type Ok<T> = { | ||
export type Ok<T> = { | ||
status: "Ok"; | ||
value: T; | ||
}; | ||
export declare type Err<E> = { | ||
export type Err<E> = { | ||
status: "Err"; | ||
error: E; | ||
}; | ||
export declare type Result<T, E = string> = Ok<T> | Err<E>; | ||
export type Result<T, E = string> = Ok<T> | Err<E>; | ||
export declare function fail<T, E>(err: E): Result<T, E>; | ||
@@ -126,0 +126,0 @@ /** |
/** | ||
* @packageDocumentation | ||
* A class that uses the PAB to interact with contracts implemented with a | ||
* specific design pattern. | ||
* A class that uses the PAB to interact with contracts implemented with a specific design pattern. | ||
* In summary it provides the following functions | ||
* - `start` to activate a contract instance that yields a transaction. | ||
* - `connect` to activate a contract instance with no yield of transaction. | ||
* - `connect` to activate a 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. | ||
* - `doOperation` for getting the unbalanced transaction of any operation. | ||
* - `stop` for stopping the PAB instance. | ||
*/ | ||
@@ -16,15 +15,5 @@ import PABApi from "../PABApi"; | ||
contractId: ContractId; | ||
reloadFlag: number; | ||
private constructor(); | ||
/** | ||
* Create a new instance of a contract and obtain its unbalanced transaction. | ||
* @param call Name of the endpoint and its parameters to activate the contract or only the | ||
* parameters if the there's only one possible tag. | ||
* @param pabUrl Base URL of the PAB that will be used. | ||
* @returns A new instance of the class and an unbalanced transaction to initiate the contract. | ||
*/ | ||
static start(pabUrl: string, call: { | ||
tag?: string; | ||
contents: unknown; | ||
}): Promise<[ContractEndpoints, Result<ExportTx>]>; | ||
/** | ||
* Connect to an already created instance of a contract on the blockchain. | ||
@@ -42,18 +31,13 @@ * @param call Name of the endpoint and its parameters to connect to the contract or just | ||
* Gets the observable state contained in the status of the PAB. | ||
* @param call Name of the reload endpoint and its required parameters. | ||
* @param name Optional name of the endpoint. Defaults to "reload". | ||
* @returns The observable state if suceeded, or a string error otherwise. | ||
*/ | ||
reload(call: { | ||
tag: string; | ||
contents: unknown; | ||
}): Promise<Result<unknown>>; | ||
reload(name?: string): Promise<Result<unknown>>; | ||
/** | ||
* Performs the specified operation. | ||
* @param callEndpoint Name of the endpoint and its parameters. | ||
* @param name Name of the endpoint. | ||
* @param params Parameter of the endpoint. | ||
* @returns The unbalanced transaction of the given operation or an error if something went wrong. | ||
*/ | ||
doOperation(call: { | ||
tag: string; | ||
contents: unknown; | ||
}): Promise<Result<ExportTx>>; | ||
doOperation(name: string, params: unknown): Promise<Result<ExportTx>>; | ||
/** | ||
@@ -64,3 +48,3 @@ * Stops the contract instance on the PAB. | ||
/** | ||
* @returns The observable state from the PAB status. | ||
* @returns The `info` field of the observable state from the PAB status. | ||
*/ | ||
@@ -67,0 +51,0 @@ private getObservableState; |
@@ -6,9 +6,8 @@ "use strict"; | ||
* @packageDocumentation | ||
* A class that uses the PAB to interact with contracts implemented with a | ||
* specific design pattern. | ||
* A class that uses the PAB to interact with contracts implemented with a specific design pattern. | ||
* In summary it provides the following functions | ||
* - `start` to activate a contract instance that yields a transaction. | ||
* - `connect` to activate a contract instance with no yield of transaction. | ||
* - `connect` to activate a 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. | ||
* - `doOperation` for getting the unbalanced transaction of any operation. | ||
* - `stop` for stopping the PAB instance. | ||
*/ | ||
@@ -34,20 +33,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this.contractId = contractId; | ||
this.reloadFlag = 0; | ||
} | ||
/** | ||
* Create a new instance of a contract and obtain its unbalanced transaction. | ||
* @param call Name of the endpoint and its parameters to activate the contract or only the | ||
* parameters if the there's only one possible tag. | ||
* @param pabUrl Base URL of the PAB that will be used. | ||
* @returns A new instance of the class and an unbalanced transaction to initiate the contract. | ||
*/ | ||
static start(pabUrl, call) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const pabApi = new PABApi_1.default(pabUrl); | ||
const cid = yield pabApi.activate(call); | ||
const inst = new ContractEndpoints(pabApi, cid); | ||
const etx = yield inst.waitForNewExportTx(); | ||
return [inst, etx]; | ||
}); | ||
} | ||
/** | ||
* Connect to an already created instance of a contract on the blockchain. | ||
@@ -68,10 +52,10 @@ * @param call Name of the endpoint and its parameters to connect to the contract or just | ||
* Gets the observable state contained in the status of the PAB. | ||
* @param call Name of the reload endpoint and its required parameters. | ||
* @param name Optional name of the endpoint. Defaults to "reload". | ||
* @returns The observable state if suceeded, or a string error otherwise. | ||
*/ | ||
reload(call) { | ||
reload(name = "reload") { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { tag, contents } = call; | ||
this.reloadFlag += 1; | ||
const oldStatus = yield this.pabApi.status(this.contractId); | ||
yield this.pabApi.endpoint(this.contractId, tag, contents); | ||
yield this.pabApi.endpoint(this.contractId, name, this.reloadFlag); | ||
const observableState = yield this.getObservableState(oldStatus); | ||
@@ -83,10 +67,10 @@ return observableState; | ||
* Performs the specified operation. | ||
* @param callEndpoint Name of the endpoint and its parameters. | ||
* @param name Name of the endpoint. | ||
* @param params Parameter of the endpoint. | ||
* @returns The unbalanced transaction of the given operation or an error if something went wrong. | ||
*/ | ||
doOperation(call) { | ||
doOperation(name, params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { tag, contents } = call; | ||
const oldStatus = yield this.pabApi.status(this.contractId); | ||
yield this.pabApi.endpoint(this.contractId, tag, contents); | ||
yield this.pabApi.endpoint(this.contractId, name, params); | ||
const etx = this.waitForNewExportTx(oldStatus); | ||
@@ -105,3 +89,3 @@ return etx; | ||
/** | ||
* @returns The observable state from the PAB status. | ||
* @returns The `info` field of the observable state from the PAB status. | ||
*/ | ||
@@ -112,5 +96,12 @@ getObservableState(oldStatus) { | ||
const currStErr = oldStatus ? oldStatus.cicCurrentState.err : null; | ||
const status = yield this.pollStatusUntil((_status) => _status.cicCurrentState.observableState !== null | ||
|| (_status.cicCurrentState.logs.length !== logLen | ||
&& last(_status.cicCurrentState.logs)._logLevel === "Error") || _status.cicCurrentState.err !== currStErr); | ||
const status = yield this.pollStatusUntil((_status) => { | ||
const { cicCurrentState } = _status; | ||
const { observableState, logs, err } = cicCurrentState; | ||
if (!hasReloadFlag(observableState)) { | ||
throw new Error("The observable state MUST have *info* and *reloadFlag* fields!"); | ||
} | ||
return (observableState !== null && observableState.reloadFlag === this.reloadFlag) | ||
|| (logLen !== logs.length && last(logs)._logLevel === "Error") | ||
|| err !== currStErr; | ||
}); | ||
const { observableState, err, logs } = status.cicCurrentState; | ||
@@ -121,4 +112,4 @@ const lastLog = last(logs); | ||
} | ||
if (observableState) { | ||
return (0, common_1.success)(observableState); | ||
if (observableState && hasReloadFlag(observableState)) { | ||
return (0, common_1.success)(observableState.info); | ||
} | ||
@@ -176,1 +167,8 @@ if (logs.length !== logLen && lastLog._logLevel === "Error") { | ||
} | ||
function hasReloadFlag(observableState) { | ||
if (observableState === null) { | ||
return true; | ||
} | ||
return typeof observableState === "object" | ||
&& typeof (observableState === null || observableState === void 0 ? void 0 : observableState.reloadFlag) === "number"; | ||
} |
import { Plutus } from "../PABApi/types/Plutus"; | ||
export declare type WalletAddress = { | ||
export type WalletAddress = { | ||
waPayment: Plutus.PubKeyHash; | ||
@@ -4,0 +4,0 @@ waStaking?: Plutus.PubKeyHash; |
@@ -66,3 +66,3 @@ import { Plutus } from "./Plutus"; | ||
} | ||
declare type Tuple<S, T> = [S, T]; | ||
type Tuple<S, T> = [S, T]; | ||
export {}; |
@@ -7,3 +7,3 @@ /** | ||
import { ExecutionUnits, ExportTxInput, Redeemer, Result } from "./common"; | ||
declare type TxBudgetAPIConfig = { | ||
type TxBudgetAPIConfig = { | ||
baseUrl: string; | ||
@@ -10,0 +10,0 @@ /** |
@@ -19,3 +19,3 @@ /** | ||
*/ | ||
declare type NeededWindow = Window & typeof globalThis & { | ||
type NeededWindow = Window & typeof globalThis & { | ||
cardano?: Record<SupportedWallet, CIP30WalletInitialAPI>; | ||
@@ -26,7 +26,7 @@ }; | ||
*/ | ||
declare type SupportedWallet = "eternl" | "nami" | "ccvault"; | ||
type SupportedWallet = "eternl" | "nami" | "ccvault"; | ||
/** | ||
* See https://cips.cardano.org/cips/cip30/#initialapi. | ||
*/ | ||
declare type CIP30WalletInitialAPI = { | ||
type CIP30WalletInitialAPI = { | ||
enable: () => Promise<CIP30WalletAPI>; | ||
@@ -33,0 +33,0 @@ isEnabled: () => Promise<boolean>; |
{ | ||
"name": "cardano-pab-client", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A set of tools to develop frontends that interact with the Plutus Application Backend.", | ||
@@ -45,3 +45,3 @@ "author": "Plank", | ||
"typedoc-plugin-missing-exports": "^1.0.0", | ||
"typescript": "^4.8.4" | ||
"typescript": "^4.9.0" | ||
}, | ||
@@ -48,0 +48,0 @@ "dependencies": { |
@@ -33,7 +33,7 @@ # Cardano PAB client library | ||
// Try to get unbalanced transaction from PAB | ||
const [endpoints, pabResponse] = await ContractEndpoints.start( | ||
pabUrl, | ||
{ endpointTag: "Init", params: [] }, | ||
); | ||
// connect to a existing contract | ||
const pabUrl = "http://localhost:9080"; | ||
const endpoints = await ContractEndpoints.connect(pabUrl, { tag: "Connect", contents: [] }); | ||
// try to get unbalanced transaction from PAB | ||
const pabResponse = endpoints.doOperation({ tag: "operation", contents: "some needed parameter" }); | ||
@@ -51,3 +51,3 @@ if (failed(pabResponse)) { | ||
const txBudgetApi = new TxBudgetAPI({ | ||
baseUrl: "http//:localhost:3001", | ||
baseUrl: "http://localhost:3001", | ||
timeout: 10000, | ||
@@ -54,0 +54,0 @@ }); |
188828
3806