@lavarage/sdk 8.0.4 → 8.0.6
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/dist/index.js +5461 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5461 -26
- package/dist/index.mjs.map +1 -1
- package/index.ts +95 -28
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { Lavarage as LavarageSOL } from "./idl/lavarageSOL";
|
|
|
17
17
|
import { Lavarage as LavarageUSDC } from "./idl/lavarageUSDC";
|
|
18
18
|
import { UserVault } from "./idl/userVault";
|
|
19
19
|
import userVaultIDL from "./idl/userVault.json";
|
|
20
|
+
import lavarageUSDCJson from "./idl/lavarageUSDC.json";
|
|
20
21
|
import bs58 from "bs58";
|
|
21
22
|
import {
|
|
22
23
|
AccountInfo,
|
|
@@ -79,6 +80,37 @@ export function getPda(seed: Buffer | Buffer[], programId: PublicKey) {
|
|
|
79
80
|
|
|
80
81
|
return PublicKey.findProgramAddressSync(seedsBuffer, programId)[0];
|
|
81
82
|
}
|
|
83
|
+
|
|
84
|
+
/** Converts snake_case to camelCase for IDL name/path strings so Anchor accepts camelCase account names in accountsStrict(). */
|
|
85
|
+
function idlAccountNamesToCamelCase(idl: unknown): unknown {
|
|
86
|
+
if (idl === null || idl === undefined) return idl;
|
|
87
|
+
if (Array.isArray(idl)) return idl.map(idlAccountNamesToCamelCase);
|
|
88
|
+
if (typeof idl === "string") return idl;
|
|
89
|
+
if (typeof idl === "object") {
|
|
90
|
+
const out: Record<string, unknown> = {};
|
|
91
|
+
for (const [k, v] of Object.entries(idl)) {
|
|
92
|
+
let newV = idlAccountNamesToCamelCase(v);
|
|
93
|
+
if (
|
|
94
|
+
(k === "name" || k === "path") &&
|
|
95
|
+
typeof newV === "string" &&
|
|
96
|
+
newV.includes("_")
|
|
97
|
+
) {
|
|
98
|
+
newV = newV
|
|
99
|
+
.split(".")
|
|
100
|
+
.map((s) => s.replace(/_([a-z])/g, (_, c) => c.toUpperCase()))
|
|
101
|
+
.join(".");
|
|
102
|
+
}
|
|
103
|
+
out[k] = newV;
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
return idl;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const LAVARAGE_USDC_IDL_CAMEL = idlAccountNamesToCamelCase(
|
|
111
|
+
lavarageUSDCJson
|
|
112
|
+
) as LavarageUSDC;
|
|
113
|
+
|
|
82
114
|
/**
|
|
83
115
|
* Generates a Position Account PDA for a specific offer and user
|
|
84
116
|
*
|
|
@@ -1354,7 +1386,8 @@ export const openTradeV2 = async (
|
|
|
1354
1386
|
) => {
|
|
1355
1387
|
let partnerFeeMarkupAsPkey;
|
|
1356
1388
|
const referralVaultProgram = new Program<UserVault>(userVaultIDL, lavarageProgram.provider);
|
|
1357
|
-
|
|
1389
|
+
const program = new Program<LavarageUSDC>(LAVARAGE_USDC_IDL_CAMEL, lavarageProgram.provider);
|
|
1390
|
+
|
|
1358
1391
|
if (partnerFeeMarkup) {
|
|
1359
1392
|
const feeBuffer = Buffer.alloc(8);
|
|
1360
1393
|
feeBuffer.writeBigUInt64LE(BigInt(partnerFeeMarkup));
|
|
@@ -1364,7 +1397,7 @@ export const openTradeV2 = async (
|
|
|
1364
1397
|
}
|
|
1365
1398
|
// assuming all token accounts are created prior
|
|
1366
1399
|
const positionAccount = getPositionAccountPDA(
|
|
1367
|
-
|
|
1400
|
+
program,
|
|
1368
1401
|
offer,
|
|
1369
1402
|
randomSeed.publicKey
|
|
1370
1403
|
);
|
|
@@ -1372,19 +1405,19 @@ export const openTradeV2 = async (
|
|
|
1372
1405
|
console.log("SDK: positionAccount", positionAccount.toBase58());
|
|
1373
1406
|
|
|
1374
1407
|
const quoteMintAccount = optionalRPCResults?.quoteMintAccountInfo ??
|
|
1375
|
-
await
|
|
1408
|
+
await program.provider.connection.getAccountInfo(quoteToken);
|
|
1376
1409
|
const quoteTokenProgram = quoteMintAccount?.owner;
|
|
1377
1410
|
|
|
1378
1411
|
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(
|
|
1379
|
-
|
|
1380
|
-
|
|
1412
|
+
program,
|
|
1413
|
+
program.provider.publicKey!,
|
|
1381
1414
|
offer.account.collateralType,
|
|
1382
1415
|
tokenProgram,
|
|
1383
1416
|
optionalRPCResults?.tokenAccountConfirmCreatedAddresses
|
|
1384
1417
|
);
|
|
1385
1418
|
|
|
1386
1419
|
const toTokenAccount = await getTokenAccountOrCreateIfNotExists(
|
|
1387
|
-
|
|
1420
|
+
program,
|
|
1388
1421
|
positionAccount,
|
|
1389
1422
|
offer.account.collateralType,
|
|
1390
1423
|
tokenProgram,
|
|
@@ -1392,7 +1425,7 @@ export const openTradeV2 = async (
|
|
|
1392
1425
|
);
|
|
1393
1426
|
|
|
1394
1427
|
const platformFeeRecipientAccount = platformFeeRecipient ? await getTokenAccountOrCreateIfNotExists(
|
|
1395
|
-
|
|
1428
|
+
program,
|
|
1396
1429
|
platformFeeRecipient,
|
|
1397
1430
|
offer.account.collateralType,
|
|
1398
1431
|
tokenProgram,
|
|
@@ -1438,7 +1471,7 @@ export const openTradeV2 = async (
|
|
|
1438
1471
|
keys: string[]
|
|
1439
1472
|
): Promise<AddressLookupTableAccount[]> => {
|
|
1440
1473
|
const addressLookupTableAccountInfos =
|
|
1441
|
-
optionalRPCResults?.addressLookupTableAccounts ?? await
|
|
1474
|
+
optionalRPCResults?.addressLookupTableAccounts ?? await program.provider.connection.getMultipleAccountsInfo(
|
|
1442
1475
|
keys.map((key) => new PublicKey(key))
|
|
1443
1476
|
);
|
|
1444
1477
|
|
|
@@ -1469,7 +1502,7 @@ export const openTradeV2 = async (
|
|
|
1469
1502
|
);
|
|
1470
1503
|
|
|
1471
1504
|
const { blockhash } =
|
|
1472
|
-
await
|
|
1505
|
+
await program.provider.connection.getLatestBlockhash("finalized");
|
|
1473
1506
|
|
|
1474
1507
|
const useReferral = discountBps !== undefined && referralBps !== undefined;
|
|
1475
1508
|
|
|
@@ -1501,7 +1534,7 @@ export const openTradeV2 = async (
|
|
|
1501
1534
|
);
|
|
1502
1535
|
|
|
1503
1536
|
// Check both accounts at the same time
|
|
1504
|
-
const [vaultAccountInfo, vaultTokenAccountInfo] = await
|
|
1537
|
+
const [vaultAccountInfo, vaultTokenAccountInfo] = await program.provider.connection.getMultipleAccountsInfo([
|
|
1505
1538
|
userVaultPda,
|
|
1506
1539
|
vaultTokenAccount
|
|
1507
1540
|
]);
|
|
@@ -1513,7 +1546,7 @@ export const openTradeV2 = async (
|
|
|
1513
1546
|
.accountsStrict({
|
|
1514
1547
|
userVault: userVaultPda,
|
|
1515
1548
|
user: partnerFeeRecipient!,
|
|
1516
|
-
funder:
|
|
1549
|
+
funder: program.provider.publicKey!,
|
|
1517
1550
|
systemProgram: SystemProgram.programId,
|
|
1518
1551
|
})
|
|
1519
1552
|
.instruction();
|
|
@@ -1522,7 +1555,7 @@ export const openTradeV2 = async (
|
|
|
1522
1555
|
// Create token account if it doesn't exist
|
|
1523
1556
|
if (!vaultTokenAccountInfo) {
|
|
1524
1557
|
partnerFeeRecipientTokenAccountCreateIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1525
|
-
|
|
1558
|
+
program.provider.publicKey!,
|
|
1526
1559
|
vaultTokenAccount,
|
|
1527
1560
|
userVaultPda,
|
|
1528
1561
|
quoteToken,
|
|
@@ -1531,12 +1564,12 @@ export const openTradeV2 = async (
|
|
|
1531
1564
|
);
|
|
1532
1565
|
}
|
|
1533
1566
|
} else if (partnerFeeRecipient && partnerDirectAta) {
|
|
1534
|
-
const [partnerDirectAtaInfo] = await
|
|
1567
|
+
const [partnerDirectAtaInfo] = await program.provider.connection.getMultipleAccountsInfo([
|
|
1535
1568
|
partnerDirectAta
|
|
1536
1569
|
]);
|
|
1537
1570
|
if (!partnerDirectAtaInfo) {
|
|
1538
1571
|
partnerFeeRecipientTokenAccountCreateIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1539
|
-
|
|
1572
|
+
program.provider.publicKey!,
|
|
1540
1573
|
partnerDirectAta,
|
|
1541
1574
|
partnerFeeRecipient!,
|
|
1542
1575
|
quoteToken,
|
|
@@ -1545,18 +1578,52 @@ export const openTradeV2 = async (
|
|
|
1545
1578
|
);
|
|
1546
1579
|
}
|
|
1547
1580
|
}
|
|
1548
|
-
|
|
1581
|
+
console.log("SDK: tradingOpenBorrowInstruction accounts", {
|
|
1582
|
+
nodeWallet: offer.account.nodeWallet,
|
|
1583
|
+
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
1584
|
+
tradingPool: offer.publicKey,
|
|
1585
|
+
positionAccount,
|
|
1586
|
+
trader: program.provider.publicKey!,
|
|
1587
|
+
systemProgram: SystemProgram.programId,
|
|
1588
|
+
clock: SYSVAR_CLOCK_PUBKEY,
|
|
1589
|
+
randomAccountAsId: randomSeed.publicKey.toBase58(),
|
|
1590
|
+
feeTokenAccount: getAssociatedTokenAddressSync(
|
|
1591
|
+
quoteToken,
|
|
1592
|
+
new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
|
|
1593
|
+
true,
|
|
1594
|
+
quoteTokenProgram
|
|
1595
|
+
),
|
|
1596
|
+
toTokenAccount: getAssociatedTokenAddressSync(
|
|
1597
|
+
quoteToken,
|
|
1598
|
+
program.provider.publicKey!,
|
|
1599
|
+
true,
|
|
1600
|
+
quoteTokenProgram
|
|
1601
|
+
),
|
|
1602
|
+
tokenProgram: quoteTokenProgram!,
|
|
1603
|
+
fromTokenAccount: getAssociatedTokenAddressSync(
|
|
1604
|
+
quoteToken,
|
|
1605
|
+
offer.account.nodeWallet,
|
|
1606
|
+
true,
|
|
1607
|
+
quoteTokenProgram
|
|
1608
|
+
),
|
|
1609
|
+
positionTokenAccount: toTokenAccount.account!.address,
|
|
1610
|
+
collateralTokenProgram: tokenProgram,
|
|
1611
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
1612
|
+
collateralMint: offer.account.collateralType,
|
|
1613
|
+
quoteMint: quoteToken,
|
|
1614
|
+
feeRecipient: new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
|
|
1615
|
+
})
|
|
1549
1616
|
|
|
1550
1617
|
|
|
1551
1618
|
const updateOracleInstructions = await getUpdateOracleIxs(
|
|
1552
1619
|
// @ts-expect-error IDL mismatch
|
|
1553
|
-
|
|
1554
|
-
u8ArrayToString(offer.account.feedId),
|
|
1620
|
+
program,
|
|
1621
|
+
u8ArrayToString(offer.account.feedId), program.provider.publicKey!,
|
|
1555
1622
|
apiKeys
|
|
1556
1623
|
);
|
|
1557
1624
|
|
|
1558
1625
|
const tradingOpenBorrowInstruction = useReferral
|
|
1559
|
-
? await
|
|
1626
|
+
? await program.methods
|
|
1560
1627
|
.tradingOpenBorrow(
|
|
1561
1628
|
new BN((marginSOL.toNumber() * leverage).toFixed(0)),
|
|
1562
1629
|
marginSOL,
|
|
@@ -1568,7 +1635,7 @@ export const openTradeV2 = async (
|
|
|
1568
1635
|
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
1569
1636
|
tradingPool: offer.publicKey,
|
|
1570
1637
|
positionAccount,
|
|
1571
|
-
trader:
|
|
1638
|
+
trader: program.provider.publicKey!,
|
|
1572
1639
|
systemProgram: SystemProgram.programId,
|
|
1573
1640
|
clock: SYSVAR_CLOCK_PUBKEY,
|
|
1574
1641
|
randomAccountAsId: randomSeed.publicKey.toBase58(),
|
|
@@ -1580,7 +1647,7 @@ export const openTradeV2 = async (
|
|
|
1580
1647
|
),
|
|
1581
1648
|
toTokenAccount: getAssociatedTokenAddressSync(
|
|
1582
1649
|
quoteToken,
|
|
1583
|
-
|
|
1650
|
+
program.provider.publicKey!,
|
|
1584
1651
|
true,
|
|
1585
1652
|
quoteTokenProgram
|
|
1586
1653
|
),
|
|
@@ -1615,7 +1682,7 @@ export const openTradeV2 = async (
|
|
|
1615
1682
|
: []
|
|
1616
1683
|
)
|
|
1617
1684
|
.instruction()
|
|
1618
|
-
: await
|
|
1685
|
+
: await program.methods
|
|
1619
1686
|
.tradingOpenBorrow(
|
|
1620
1687
|
new BN((marginSOL.toNumber() * leverage).toFixed(0)),
|
|
1621
1688
|
marginSOL
|
|
@@ -1625,7 +1692,7 @@ export const openTradeV2 = async (
|
|
|
1625
1692
|
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
1626
1693
|
tradingPool: offer.publicKey,
|
|
1627
1694
|
positionAccount,
|
|
1628
|
-
trader:
|
|
1695
|
+
trader: program.provider.publicKey!,
|
|
1629
1696
|
systemProgram: SystemProgram.programId,
|
|
1630
1697
|
clock: SYSVAR_CLOCK_PUBKEY,
|
|
1631
1698
|
randomAccountAsId: randomSeed.publicKey.toBase58(),
|
|
@@ -1637,7 +1704,7 @@ export const openTradeV2 = async (
|
|
|
1637
1704
|
),
|
|
1638
1705
|
toTokenAccount: getAssociatedTokenAddressSync(
|
|
1639
1706
|
quoteToken,
|
|
1640
|
-
|
|
1707
|
+
program.provider.publicKey!,
|
|
1641
1708
|
true,
|
|
1642
1709
|
quoteTokenProgram
|
|
1643
1710
|
),
|
|
@@ -1677,11 +1744,11 @@ export const openTradeV2 = async (
|
|
|
1677
1744
|
|
|
1678
1745
|
const quoteOracleQuote = getOracleQuoteForCollateralType(USDC_MINT);
|
|
1679
1746
|
|
|
1680
|
-
const openAddCollateralInstruction = await
|
|
1747
|
+
const openAddCollateralInstruction = await program.methods
|
|
1681
1748
|
.tradingOpenAddCollateral(offer.account.interestRate < 255 ? offer.account.interestRate + 1 : 255)
|
|
1682
1749
|
.accountsStrict({
|
|
1683
1750
|
tradingPool: offer.publicKey,
|
|
1684
|
-
trader:
|
|
1751
|
+
trader: program.provider.publicKey!,
|
|
1685
1752
|
mint: offer.account.collateralType,
|
|
1686
1753
|
toTokenAccount: toTokenAccount.account!.address,
|
|
1687
1754
|
systemProgram: SystemProgram.programId,
|
|
@@ -1724,7 +1791,7 @@ export const openTradeV2 = async (
|
|
|
1724
1791
|
].filter(Boolean) as TransactionInstruction[];
|
|
1725
1792
|
|
|
1726
1793
|
const messageV01 = new TransactionMessage({
|
|
1727
|
-
payerKey:
|
|
1794
|
+
payerKey: program.provider.publicKey!,
|
|
1728
1795
|
recentBlockhash: blockhash,
|
|
1729
1796
|
instructions: setUpInstructions,
|
|
1730
1797
|
}).compileToV0Message(addressLookupTableAccounts);
|
|
@@ -1732,7 +1799,7 @@ export const openTradeV2 = async (
|
|
|
1732
1799
|
const tx2 = new VersionedTransaction(messageV01);
|
|
1733
1800
|
|
|
1734
1801
|
const messageV0 = new TransactionMessage({
|
|
1735
|
-
payerKey:
|
|
1802
|
+
payerKey: program.provider.publicKey!,
|
|
1736
1803
|
recentBlockhash: blockhash,
|
|
1737
1804
|
instructions: allInstructions,
|
|
1738
1805
|
}).compileToV0Message(addressLookupTableAccounts);
|
|
@@ -1754,7 +1821,7 @@ export const openTradeV2 = async (
|
|
|
1754
1821
|
].filter(Boolean) as TransactionInstruction[];
|
|
1755
1822
|
|
|
1756
1823
|
const messageV0 = new TransactionMessage({
|
|
1757
|
-
payerKey:
|
|
1824
|
+
payerKey: program.provider.publicKey!,
|
|
1758
1825
|
recentBlockhash: blockhash,
|
|
1759
1826
|
instructions: allInstructions,
|
|
1760
1827
|
}).compileToV0Message(addressLookupTableAccounts);
|