Skip to content

Commit 528e7d9

Browse files
authored
Merge pull request #2571 from pyth-network/cprussin/more-staking-sdk-fixes
fix(staking-sdk): more staking-sdk improvements
2 parents ea118ba + a845328 commit 528e7d9

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed
+1-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1 @@
1-
import { base } from "@cprussin/eslint-config";
2-
3-
export default [
4-
...base,
5-
{
6-
rules: {
7-
"n/no-unpublished-import": "off",
8-
"unicorn/no-null": "off",
9-
"unicorn/prefer-node-protocol": "off",
10-
},
11-
},
12-
];
1+
export { base as default } from "@cprussin/eslint-config";

governance/pyth_staking_sdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/staking-sdk",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Pyth staking SDK",
55
"type": "module",
66
"exports": {

governance/pyth_staking_sdk/src/pyth-staking-client.ts

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as crypto from "crypto";
1+
import crypto from "crypto"; // eslint-disable-line unicorn/prefer-node-protocol
22

33
import { AnchorProvider, BN, Program } from "@coral-xyz/anchor";
44
import {
@@ -752,7 +752,7 @@ export class PythStakingClient {
752752
publisherStakeAccountPositions: stakeAccount,
753753
publisherStakeAccountCustody: stakeAccount
754754
? getStakeAccountCustodyAddress(stakeAccount)
755-
: null,
755+
: null, // eslint-disable-line unicorn/no-null
756756
stakeAccountPositions,
757757
stakeAccountCustody: getStakeAccountCustodyAddress(
758758
stakeAccountPositions,
@@ -839,6 +839,7 @@ export class PythStakingClient {
839839
.setPublisherStakeAccount()
840840
.accounts({
841841
currentStakeAccountPositionsOption: stakeAccountPositions,
842+
// eslint-disable-next-line unicorn/no-null
842843
newStakeAccountPositionsOption: newStakeAccountPositions ?? null,
843844
publisher,
844845
})
@@ -1088,22 +1089,25 @@ export class PythStakingClient {
10881089
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
10891090
while (true) {
10901091
const res = await reader.read();
1091-
if (res.done) break;
1092-
if (typeof res.value === "string") {
1092+
if (res.done) {
1093+
break;
1094+
} else if (
1095+
typeof res.value === "string" ||
1096+
res.value instanceof Uint8Array
1097+
) {
10931098
jsonparser.write(res.value);
10941099
}
10951100
}
10961101
};
10971102

1098-
parse().catch((error: unknown) => {
1099-
reject(
1100-
error instanceof Error
1101-
? error
1102-
: new Error(
1103-
typeof error === "string" ? error : "Unknown Error",
1104-
),
1105-
);
1106-
});
1103+
parse().then(
1104+
() => {
1105+
reject(new EndOfStreamError());
1106+
},
1107+
(error: unknown) => {
1108+
reject(intoError(error));
1109+
},
1110+
);
11071111
});
11081112

11091113
return accountSchema
@@ -1140,6 +1144,16 @@ const accountSchema = z.array(
11401144
}),
11411145
);
11421146

1147+
const intoError = (error: unknown): Error => {
1148+
if (error instanceof Error) {
1149+
return error;
1150+
} else if (typeof error === "string") {
1151+
return new Error(error);
1152+
} else {
1153+
return new UnknownError();
1154+
}
1155+
};
1156+
11431157
class NotOKError extends Error {
11441158
constructor(result: Response) {
11451159
super(`Received a ${result.status.toString()} response for ${result.url}`);
@@ -1154,3 +1168,17 @@ class NoBodyError extends Error {
11541168
this.name = "NoBodyError";
11551169
}
11561170
}
1171+
1172+
class EndOfStreamError extends Error {
1173+
constructor() {
1174+
super("Reached end of stream without finding accounts");
1175+
this.name = "EndOfStreamError";
1176+
}
1177+
}
1178+
1179+
class UnknownError extends Error {
1180+
constructor() {
1181+
super("Unknown error");
1182+
this.name = "UnknownError";
1183+
}
1184+
}

governance/pyth_staking_sdk/src/utils/pool.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const extractPublisherData = (
1717
stakeAccount:
1818
poolData.publisherStakeAccounts[index] === undefined ||
1919
poolData.publisherStakeAccounts[index].equals(PublicKey.default)
20-
? null
20+
? null // eslint-disable-line unicorn/no-null
2121
: poolData.publisherStakeAccounts[index],
2222
totalDelegation:
2323
(poolData.delState[index]?.totalDelegation ?? 0n) +

0 commit comments

Comments
 (0)