pnp-sdk 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/{chunk-JBFSLIVK.js → chunk-RJ2IJGCX.js} +141 -7
- package/dist/{chunk-JBFSLIVK.js.map → chunk-RJ2IJGCX.js.map} +1 -1
- package/dist/cli.cjs +133 -6
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +140 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +76 -2
- package/dist/index.d.ts +76 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pnp-sdk
|
|
2
2
|
|
|
3
3
|
Production-ready SDK for Solana prediction markets: create markets, trade, and redeem positions.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm i
|
|
8
|
+
npm i pnp-sdk
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Usage (Node)
|
|
@@ -33,7 +33,7 @@ QUOTE_MINT=YourQuoteMintAddressHere
|
|
|
33
33
|
### With Base58 Private Key
|
|
34
34
|
|
|
35
35
|
```ts
|
|
36
|
-
import { PNPClient } from "
|
|
36
|
+
import { PNPClient } from "pnp-sdk";
|
|
37
37
|
import bs58 from "bs58";
|
|
38
38
|
import * as dotenv from "dotenv";
|
|
39
39
|
|
|
@@ -51,7 +51,7 @@ const global = await client.fetchGlobalConfig();
|
|
|
51
51
|
### With Private Key Array
|
|
52
52
|
|
|
53
53
|
```ts
|
|
54
|
-
import { PNPClient } from "
|
|
54
|
+
import { PNPClient } from "pnp-sdk";
|
|
55
55
|
import * as dotenv from "dotenv";
|
|
56
56
|
|
|
57
57
|
dotenv.config();
|
|
@@ -9828,6 +9828,7 @@ var pdas_exports = {};
|
|
|
9828
9828
|
__export(pdas_exports, {
|
|
9829
9829
|
deriveAta: () => deriveAta,
|
|
9830
9830
|
deriveCreatorFeeTreasuryPda: () => deriveCreatorFeeTreasuryPda,
|
|
9831
|
+
deriveCreatorFeeTreasuryPdaLegacy: () => deriveCreatorFeeTreasuryPdaLegacy,
|
|
9831
9832
|
deriveGlobalConfigPda: () => deriveGlobalConfigPda,
|
|
9832
9833
|
deriveMarketPda: () => deriveMarketPda,
|
|
9833
9834
|
deriveMarketPdaFromMints: () => deriveMarketPdaFromMints,
|
|
@@ -9855,8 +9856,17 @@ function derivePositionPda(market, owner, programId) {
|
|
|
9855
9856
|
function deriveGlobalConfigPda(programId) {
|
|
9856
9857
|
return PublicKey3.findProgramAddressSync([Buffer.from("global_config")], programId);
|
|
9857
9858
|
}
|
|
9858
|
-
function deriveCreatorFeeTreasuryPda(creator, programId) {
|
|
9859
|
-
return PublicKey3.findProgramAddressSync(
|
|
9859
|
+
function deriveCreatorFeeTreasuryPda(creator, programId, collateralMint) {
|
|
9860
|
+
return PublicKey3.findProgramAddressSync(
|
|
9861
|
+
[Buffer.from("creator_fee_treasury"), creator.toBuffer(), collateralMint.toBuffer()],
|
|
9862
|
+
programId
|
|
9863
|
+
);
|
|
9864
|
+
}
|
|
9865
|
+
function deriveCreatorFeeTreasuryPdaLegacy(creator, programId) {
|
|
9866
|
+
return PublicKey3.findProgramAddressSync(
|
|
9867
|
+
[Buffer.from("creator_fee_treasury"), creator.toBuffer()],
|
|
9868
|
+
programId
|
|
9869
|
+
);
|
|
9860
9870
|
}
|
|
9861
9871
|
function deriveAta(owner, mint, allowOwnerOffCurve = false, tokenProgramId = TOKEN_PROGRAM_ID, ataProgramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
|
|
9862
9872
|
return getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve, tokenProgramId, ataProgramId);
|
|
@@ -10086,7 +10096,7 @@ var MarketModule = class {
|
|
|
10086
10096
|
const creator = payer;
|
|
10087
10097
|
const payerCollateralTokenAccount = deriveAta(payer, collateralMint);
|
|
10088
10098
|
const marketReserveVault = deriveAta(market, collateralMint, true);
|
|
10089
|
-
const [creatorFeeTreasury] = deriveCreatorFeeTreasuryPda(creator, this.client.programId);
|
|
10099
|
+
const [creatorFeeTreasury] = deriveCreatorFeeTreasuryPda(creator, this.client.programId, collateralMint);
|
|
10090
10100
|
const accounts = {
|
|
10091
10101
|
payer,
|
|
10092
10102
|
creator,
|
|
@@ -10839,7 +10849,7 @@ var AnchorMarketModule = class {
|
|
|
10839
10849
|
// allowOwnerOffCurve
|
|
10840
10850
|
);
|
|
10841
10851
|
const [creatorFeeTreasury] = PublicKey11.findProgramAddressSync(
|
|
10842
|
-
[Buffer.from("creator_fee_treasury"), creatorOverride.toBuffer()],
|
|
10852
|
+
[Buffer.from("creator_fee_treasury"), creatorOverride.toBuffer(), collateralTokenMint.toBuffer()],
|
|
10843
10853
|
this.anchorClient.program.programId
|
|
10844
10854
|
);
|
|
10845
10855
|
try {
|
|
@@ -10973,7 +10983,7 @@ var AnchorMarketV3Module = class {
|
|
|
10973
10983
|
creatorOverride
|
|
10974
10984
|
);
|
|
10975
10985
|
const [creatorFeeTreasury] = PublicKey12.findProgramAddressSync(
|
|
10976
|
-
[Buffer.from("creator_fee_treasury"), creatorOverride.toBuffer()],
|
|
10986
|
+
[Buffer.from("creator_fee_treasury"), creatorOverride.toBuffer(), collateralTokenMint.toBuffer()],
|
|
10977
10987
|
this.anchorClient.program.programId
|
|
10978
10988
|
);
|
|
10979
10989
|
try {
|
|
@@ -11180,6 +11190,24 @@ var PNPClient = class _PNPClient {
|
|
|
11180
11190
|
detectedYoutubeUrl: youtubeUrl || detected.youtubeUrl
|
|
11181
11191
|
};
|
|
11182
11192
|
}
|
|
11193
|
+
/**
|
|
11194
|
+
* Create a P2P market with DeFiLlama protocol/metric detection (high-level wrapper).
|
|
11195
|
+
* Automatically formats the question with DeFiLlama reference.
|
|
11196
|
+
* Serializes PublicKeys to base58 strings to match P2PMarketResponse.
|
|
11197
|
+
*/
|
|
11198
|
+
async createP2PMarketDefiLlama(params) {
|
|
11199
|
+
const { question, protocolName, metric, ...rest } = params;
|
|
11200
|
+
const finalQuestion = `${question}${question.endsWith(" ") ? "" : " "}df ${protocolName} ${metric}`;
|
|
11201
|
+
const result = await this.createP2PMarketGeneral({
|
|
11202
|
+
...rest,
|
|
11203
|
+
question: finalQuestion
|
|
11204
|
+
});
|
|
11205
|
+
return {
|
|
11206
|
+
...result,
|
|
11207
|
+
detectedProtocol: protocolName,
|
|
11208
|
+
detectedMetric: metric
|
|
11209
|
+
};
|
|
11210
|
+
}
|
|
11183
11211
|
/**
|
|
11184
11212
|
* Simple helper to create a P2P market with a YouTube URL using UI-friendly parameters.
|
|
11185
11213
|
* - Accepts amount in USDC (number) and converts to raw units internally (assumes 6 decimals).
|
|
@@ -11301,6 +11329,33 @@ var PNPClient = class _PNPClient {
|
|
|
11301
11329
|
detectedYoutubeUrl: youtubeUrl || detected.youtubeUrl
|
|
11302
11330
|
};
|
|
11303
11331
|
}
|
|
11332
|
+
/**
|
|
11333
|
+
* Create a V2 (AMM) market with DeFiLlama protocol/metric detection (high-level wrapper).
|
|
11334
|
+
* Automatically formats the question with DeFiLlama reference.
|
|
11335
|
+
* Serializes PublicKeys to base58 strings.
|
|
11336
|
+
*/
|
|
11337
|
+
async createMarketDefiLlama(params) {
|
|
11338
|
+
const { question, protocolName, metric, initialLiquidity, endTime, collateralTokenMint } = params;
|
|
11339
|
+
if (!this.market) {
|
|
11340
|
+
throw new Error("MarketModule not available. Initialize client with a signer.");
|
|
11341
|
+
}
|
|
11342
|
+
const finalQuestion = `${question}${question.endsWith(" ") ? "" : " "}df ${protocolName} ${metric}`;
|
|
11343
|
+
const result = await this.market.createMarket({
|
|
11344
|
+
question: finalQuestion,
|
|
11345
|
+
initialLiquidity,
|
|
11346
|
+
endTime,
|
|
11347
|
+
baseMint: collateralTokenMint
|
|
11348
|
+
});
|
|
11349
|
+
if (!result.signature) {
|
|
11350
|
+
throw new Error("Transaction failed: No signature returned");
|
|
11351
|
+
}
|
|
11352
|
+
return {
|
|
11353
|
+
signature: result.signature,
|
|
11354
|
+
market: result.market.toBase58(),
|
|
11355
|
+
detectedProtocol: protocolName,
|
|
11356
|
+
detectedMetric: metric
|
|
11357
|
+
};
|
|
11358
|
+
}
|
|
11304
11359
|
/**
|
|
11305
11360
|
* Create a P2P market with Twitter URL detection (high-level wrapper).
|
|
11306
11361
|
* Automatically detects and extracts Twitter URLs from the question.
|
|
@@ -11386,6 +11441,55 @@ var PNPClient = class _PNPClient {
|
|
|
11386
11441
|
creator
|
|
11387
11442
|
});
|
|
11388
11443
|
}
|
|
11444
|
+
/**
|
|
11445
|
+
* Simple helper to create a P2P market with DeFiLlama protocol/metric using UI-friendly parameters.
|
|
11446
|
+
* - Accepts amount in USDC (number) and converts to raw units internally (assumes 6 decimals).
|
|
11447
|
+
* - Defaults to mainnet USDC as collateral mint if not provided.
|
|
11448
|
+
* - Defaults creatorSideCap to 5x the initial amount if not provided.
|
|
11449
|
+
* - Defaults end time to `daysUntilEnd` days from now (30 days by default).
|
|
11450
|
+
*/
|
|
11451
|
+
async createP2PMarketDefiLlamaSimple(params) {
|
|
11452
|
+
const {
|
|
11453
|
+
question,
|
|
11454
|
+
protocolName,
|
|
11455
|
+
metric,
|
|
11456
|
+
side,
|
|
11457
|
+
amountUsdc,
|
|
11458
|
+
daysUntilEnd = 30,
|
|
11459
|
+
creatorSideCapMultiplier = 5,
|
|
11460
|
+
collateralTokenMint,
|
|
11461
|
+
maxPotRatio,
|
|
11462
|
+
creator
|
|
11463
|
+
} = params;
|
|
11464
|
+
if (!Number.isFinite(amountUsdc) || amountUsdc <= 0) {
|
|
11465
|
+
throw new Error("amountUsdc must be a positive number");
|
|
11466
|
+
}
|
|
11467
|
+
if (!["yes", "no"].includes(side)) {
|
|
11468
|
+
throw new Error("side must be 'yes' or 'no'");
|
|
11469
|
+
}
|
|
11470
|
+
const defaultUsdcMint = new PublicKey13(
|
|
11471
|
+
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
|
|
11472
|
+
);
|
|
11473
|
+
const collateralMintPk = collateralTokenMint ?? defaultUsdcMint;
|
|
11474
|
+
const initialAmount = this.uiToRaw(amountUsdc, 6);
|
|
11475
|
+
const creatorCapUsdc = amountUsdc * creatorSideCapMultiplier;
|
|
11476
|
+
const creatorSideCap = this.uiToRaw(creatorCapUsdc, 6);
|
|
11477
|
+
const nowSeconds = Math.floor(Date.now() / 1e3);
|
|
11478
|
+
const endTimeSeconds = nowSeconds + daysUntilEnd * 24 * 60 * 60;
|
|
11479
|
+
const endTime = BigInt(endTimeSeconds);
|
|
11480
|
+
return this.createP2PMarketDefiLlama({
|
|
11481
|
+
question,
|
|
11482
|
+
protocolName,
|
|
11483
|
+
metric,
|
|
11484
|
+
initialAmount,
|
|
11485
|
+
side,
|
|
11486
|
+
creatorSideCap,
|
|
11487
|
+
endTime,
|
|
11488
|
+
maxPotRatio,
|
|
11489
|
+
collateralTokenMint: collateralMintPk,
|
|
11490
|
+
creator
|
|
11491
|
+
});
|
|
11492
|
+
}
|
|
11389
11493
|
/**
|
|
11390
11494
|
* Detects if a question contains a Twitter URL or tweet ID format after the question mark
|
|
11391
11495
|
* @param questionText The question text to analyze
|
|
@@ -11467,6 +11571,36 @@ var PNPClient = class _PNPClient {
|
|
|
11467
11571
|
}
|
|
11468
11572
|
return { question: questionText };
|
|
11469
11573
|
}
|
|
11574
|
+
/**
|
|
11575
|
+
* Detects if a question contains a DeFiLlama format after the question mark
|
|
11576
|
+
* @param questionText The question text to analyze
|
|
11577
|
+
* @returns Object containing the cleaned question and DeFiLlama protocol/metric info if present
|
|
11578
|
+
* @example
|
|
11579
|
+
* detectDefiLlamaUrl("Will Uniswap TVL exceed $5B? df uniswap-v3 tvl");
|
|
11580
|
+
*/
|
|
11581
|
+
static detectDefiLlamaUrl(questionText) {
|
|
11582
|
+
const questionMarkIndex = questionText.indexOf("?");
|
|
11583
|
+
if (questionMarkIndex === -1) {
|
|
11584
|
+
return { question: questionText };
|
|
11585
|
+
}
|
|
11586
|
+
const afterQuestionMark = questionText.substring(questionMarkIndex + 1).trim();
|
|
11587
|
+
if (!afterQuestionMark) {
|
|
11588
|
+
return { question: questionText };
|
|
11589
|
+
}
|
|
11590
|
+
const defillamaPattern = /^\s*df\s+(\S+(?:\s+\S+)*)\s+(\S+)\s*$/i;
|
|
11591
|
+
const match = afterQuestionMark.match(defillamaPattern);
|
|
11592
|
+
if (match && match[1] && match[2]) {
|
|
11593
|
+
const protocolParts = match[1].trim().split(/\s+/);
|
|
11594
|
+
const metric = match[2].trim();
|
|
11595
|
+
const protocol = protocolParts.join(" ");
|
|
11596
|
+
return {
|
|
11597
|
+
question: questionText.substring(0, questionMarkIndex + 1).trim(),
|
|
11598
|
+
protocol,
|
|
11599
|
+
metric
|
|
11600
|
+
};
|
|
11601
|
+
}
|
|
11602
|
+
return { question: questionText };
|
|
11603
|
+
}
|
|
11470
11604
|
/**
|
|
11471
11605
|
* Parse a private key from a string that could be a JSON array or base58 encoded
|
|
11472
11606
|
* @param keyString Private key as JSON array or base58 string
|
|
@@ -11734,7 +11868,7 @@ Cause: ${err.message}`);
|
|
|
11734
11868
|
TOKEN_PROGRAM_ID6
|
|
11735
11869
|
);
|
|
11736
11870
|
const [creatorFeeTreasury] = PublicKey13.findProgramAddressSync(
|
|
11737
|
-
[Buffer.from("creator_fee_treasury"), creator.toBuffer()],
|
|
11871
|
+
[Buffer.from("creator_fee_treasury"), creator.toBuffer(), collateralTokenMint.toBuffer()],
|
|
11738
11872
|
this.client.programId
|
|
11739
11873
|
);
|
|
11740
11874
|
try {
|
|
@@ -12325,4 +12459,4 @@ export {
|
|
|
12325
12459
|
RedemptionModule,
|
|
12326
12460
|
PNPClient
|
|
12327
12461
|
};
|
|
12328
|
-
//# sourceMappingURL=chunk-
|
|
12462
|
+
//# sourceMappingURL=chunk-RJ2IJGCX.js.map
|