Skip to content

Commit

Permalink
fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed Nov 30, 2023
1 parent c1382e5 commit dd31390
Show file tree
Hide file tree
Showing 81 changed files with 553 additions and 544 deletions.
6 changes: 3 additions & 3 deletions packages/anchor-contrib/src/generateAccountParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export type AccountParsers<M> = {
* @param idl The IDL.
*/
export const generateAccountParsers = <M extends Record<string, object>>(
idl: Idl
idl: Idl,
): AccountParsers<M> => {
const coder = new BorshAccountsCoder(idl);
return generateAccountParsersFromCoder(
idl.accounts?.map((a) => a.name),
coder
coder,
);
};

Expand All @@ -35,7 +35,7 @@ export const generateAccountParsers = <M extends Record<string, object>>(
*/
export const generateAccountParsersFromCoder = <M>(
accountNames: string[] | undefined,
coder: AccountsCoder
coder: AccountsCoder,
): AccountParsers<M> => {
return (accountNames ?? []).reduce((parsers, account) => {
parsers[camelCase(account) as keyof M] = (data: Buffer) =>
Expand Down
32 changes: 16 additions & 16 deletions packages/anchor-contrib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Context<A extends IdlAccountItem[]> = Omit<AnchorContext, "accounts"> & {
type MakeInstructionsNamespace<
R extends InstructionsParsed,
Ret,
Mk extends { [M in keyof R]: unknown } = { [M in keyof R]: unknown }
Mk extends { [M in keyof R]: unknown } = { [M in keyof R]: unknown },
> = {
[M in keyof R]: ((
...args: [...R[M]["args"], Context<R[M]["accounts"]>]
Expand Down Expand Up @@ -124,18 +124,18 @@ type TypeMap = {
type DecodeType<T extends IdlType, Defined> = T extends keyof TypeMap
? TypeMap[T]
: T extends { defined: keyof Defined }
? Defined[T["defined"]]
: T extends { option: { defined: keyof Defined } }
? Defined[T["option"]["defined"]] | null
: T extends { option: keyof TypeMap }
? TypeMap[T["option"]] | null
: T extends { vec: { defined: keyof Defined } }
? Defined[T["vec"]["defined"]][]
: T extends { vec: keyof TypeMap }
? TypeMap[T["vec"]][]
: T extends { array: [idlType: keyof TypeMap, size: number] }
? TypeMap[T["array"][0]][]
: unknown;
? Defined[T["defined"]]
: T extends { option: { defined: keyof Defined } }
? Defined[T["option"]["defined"]] | null
: T extends { option: keyof TypeMap }
? TypeMap[T["option"]] | null
: T extends { vec: { defined: keyof Defined } }
? Defined[T["vec"]["defined"]][]
: T extends { vec: keyof TypeMap }
? TypeMap[T["vec"]][]
: T extends { array: [idlType: keyof TypeMap, size: number] }
? TypeMap[T["array"][0]][]
: unknown;

type MakeArgs<A extends IdlField[], Defined> = {
[K in keyof A]: A[K] extends IdlField
Expand Down Expand Up @@ -169,7 +169,7 @@ export type AnchorProgram<
Methods extends MakeInstructions<
NonNullable<IDL["state"]>["methods"],
Defined
> = MakeInstructions<NonNullable<IDL["state"]>["methods"], Defined>
> = MakeInstructions<NonNullable<IDL["state"]>["methods"], Defined>,
> = Omit<
AProgram,
"rpc" | "state" | "account" | "transaction" | "instruction"
Expand Down Expand Up @@ -202,7 +202,7 @@ type AnchorTypeDefs<T extends IdlTypeDef[], Defined> = {

export type AnchorDefined<
T extends Idl,
D = Record<string, never>
D = Record<string, never>,
> = AnchorTypeDefs<NonNullable<T["types"]>, D>;

export type AnchorAccounts<T extends Idl, Defined> = AnchorTypeDefs<
Expand All @@ -219,7 +219,7 @@ export type AnchorTypes<
T extends Idl,
AccountMap = Record<string, never>,
D = Record<string, never>,
DEF = AnchorDefined<T, D>
DEF = AnchorDefined<T, D>,
> = {
Defined: DEF;
Accounts: AnchorAccounts<T, DEF>;
Expand Down
2 changes: 1 addition & 1 deletion packages/anchor-contrib/src/utils/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type AnchorAccountMap<M> = {
export const generateAnchorAccounts = <M>(
programID: PublicKey,
accounts: IdlTypeDef[],
coder: AccountsCoder
coder: AccountsCoder,
): AnchorAccountMap<M> => {
const parsers: Partial<AnchorAccountMap<M>> = {};
accounts.forEach((account) => {
Expand Down
28 changes: 15 additions & 13 deletions packages/anchor-contrib/src/utils/coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ export class SuperCoder<T extends CoderAnchorTypes> {
/**
* Program IDL.
*/
readonly idl: T["IDL"]
readonly idl: T["IDL"],
) {
this.coder = new BorshCoder<IDLAccountName<T["IDL"]>>(idl);
this.eventParser = new EventParser(address, this.coder);
this.accounts = generateAnchorAccounts(
address,
idl.accounts ?? [],
this.coder.accounts
this.coder.accounts,
);

this.errorMap = generateErrorMap<T["IDL"]>(idl);

const accountsList = Object.values(
this.accounts
this.accounts,
) as AnchorAccount<unknown>[];
const accountTypeDefs: Partial<AccountTypeDefMap<T["IDL"]>> = {};
accountsList.forEach((account) => {
Expand All @@ -132,16 +132,18 @@ export class SuperCoder<T extends CoderAnchorTypes> {

this.accountParsers = generateAccountParsersFromCoder(
idl.accounts?.map((acc) => acc.name),
this.coder.accounts
this.coder.accounts,
);
this.accountTypeDefs = accountTypeDefs as AccountTypeDefMap<T["IDL"]>;
this.discriminators = accountsList.reduce(
(acc, el) => ({ ...acc, [el.discriminator.toString("hex")]: el.name }),
{}
{},
);
this.discriminatorsByAccount = accountsList.reduce(
(acc, el) => ({ ...acc, [el.name]: el.discriminator }),
{} as { [K in NonNullable<T["IDL"]["accounts"]>[number]["name"]]: Buffer }
{} as {
[K in NonNullable<T["IDL"]["accounts"]>[number]["name"]]: Buffer;
},
);
}

Expand Down Expand Up @@ -169,7 +171,7 @@ export class SuperCoder<T extends CoderAnchorTypes> {
* @returns
*/
parseProgramLogEvents<
E extends T["Events"][keyof T["Events"]] = T["Events"][keyof T["Events"]]
E extends T["Events"][keyof T["Events"]] = T["Events"][keyof T["Events"]],
>(logs?: string[]): readonly E[] {
if (!logs) {
return [];
Expand All @@ -191,11 +193,11 @@ export class SuperCoder<T extends CoderAnchorTypes> {
encodeIX<
K extends keyof T["Instructions"] & string = keyof T["Instructions"] &
string,
I extends T["Instructions"][K] = T["Instructions"][K]
I extends T["Instructions"][K] = T["Instructions"][K],
>(
name: K,
args: I["namedArgs"],
accounts: Accounts<I["accounts"][number]>
accounts: Accounts<I["accounts"][number]>,
): TransactionInstruction {
const idlIx = this.idl.instructions.find((ix) => ix.name === name);
if (!idlIx) {
Expand All @@ -205,7 +207,7 @@ export class SuperCoder<T extends CoderAnchorTypes> {
const keys = InstructionNamespaceFactory.accountsArray(
accounts,
idlIx.accounts,
name
name,
);
return new TransactionInstruction({
programId: this.address,
Expand Down Expand Up @@ -250,21 +252,21 @@ export class SuperCoder<T extends CoderAnchorTypes> {
export const buildCoderMap = <
P extends {
[K in keyof P]: CoderAnchorTypes;
}
},
>(
idls: {
[K in keyof P]: Idl;
},
addresses: {
[K in keyof P]: PublicKey;
}
},
): {
[K in keyof P]: SuperCoder<P[K]>;
} => {
return mapValues(
idls,
<K extends keyof P>(idl: P[K]["IDL"], k: K) =>
new SuperCoder<P[K]>(addresses[k], idl)
new SuperCoder<P[K]>(addresses[k], idl),
) as unknown as {
[K in keyof P]: SuperCoder<P[K]>;
};
Expand Down
8 changes: 4 additions & 4 deletions packages/anchor-contrib/src/utils/programs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import { makeAnchorProvider } from "./provider.js";
export const newProgram = <P>(
idl: Idl,
address: PublicKey,
provider: SaberProvider | ReadonlySaberProvider
provider: SaberProvider | ReadonlySaberProvider,
) => {
return new Program(
idl,
address.toString(),
makeAnchorProvider(provider)
makeAnchorProvider(provider),
) as unknown as P;
};

Expand All @@ -43,12 +43,12 @@ export const newProgramMap = <P>(
},
addresses: {
[K in keyof P]: PublicKey;
}
},
): {
[K in keyof P]: P[K];
} => {
return mapValues(idls, (idl, k: keyof P) =>
newProgram(idl, addresses[k], provider)
newProgram(idl, addresses[k], provider),
) as unknown as {
[K in keyof P]: P[K];
};
Expand Down
12 changes: 6 additions & 6 deletions packages/anchor-contrib/src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const AnchorProviderClass: AnchorProviderCtor &
export type AnchorProviderCtor = new (
connection: Connection,
wallet: Wallet,
opts: ConfirmOptions
opts: ConfirmOptions,
) => AnchorProvider;

/**
Expand All @@ -57,7 +57,7 @@ export type AnchorProviderCtor = new (
export const buildAnchorProvider = (
connection: Connection,
wallet: Wallet,
opts: ConfirmOptions
opts: ConfirmOptions,
) => {
return new AnchorProviderClass(connection, wallet, opts);
};
Expand All @@ -68,7 +68,7 @@ export const buildAnchorProvider = (
* @returns
*/
export const makeReadonlySaberProvider = (
anchorProvider: IAnchorProvider
anchorProvider: IAnchorProvider,
): ReadonlySaberProvider => {
return new SolanaReadonlyProvider(anchorProvider.connection);
};
Expand All @@ -79,7 +79,7 @@ export const makeReadonlySaberProvider = (
* @returns
*/
export const makeSaberProvider = (
anchorProvider: AnchorProvider
anchorProvider: AnchorProvider,
): SaberProvider => {
return SolanaProvider.init({
connection: anchorProvider.connection,
Expand All @@ -94,11 +94,11 @@ export const makeSaberProvider = (
* @returns
*/
export const makeAnchorProvider = (
saberProvider: ReadonlySaberProvider
saberProvider: ReadonlySaberProvider,
): AnchorProvider => {
return buildAnchorProvider(
saberProvider.connection,
saberProvider.wallet,
saberProvider.opts
saberProvider.opts,
);
};
18 changes: 9 additions & 9 deletions packages/chai-solana/src/expectTXTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const expectTXTable = (
} = {
verbosity: null,
formatLogs: true,
}
},
): Chai.PromisedAssertion => {
if (tx === null) {
throw new Error();
Expand All @@ -60,15 +60,15 @@ export const expectTXTable = (
const callStack = new Error().stack?.split("\n");
if (callStack) {
const expectIndex = callStack.findIndex((l) =>
l.includes(`at ${expectTXTable.name}`)
l.includes(`at ${expectTXTable.name}`),
);
if (expectIndex > 0) {
const targetLine = callStack[expectIndex + 1];
if (targetLine) {
const cwd = process.cwd();
// get the part of targetLine after cwd
const targetLineAfterCwd = targetLine.substring(
targetLine.indexOf(cwd) + cwd.length
targetLine.indexOf(cwd) + cwd.length,
);
if (targetLineAfterCwd.length > 0) {
relativePath = targetLineAfterCwd.substring(1);
Expand All @@ -85,7 +85,7 @@ export const expectTXTable = (
printTXTable(
tx,
simulation.value.logs,
`${msg ? msg + " " : ""}${relativePath ? `(${relativePath})` : ""}`
`${msg ? msg + " " : ""}${relativePath ? `(${relativePath})` : ""}`,
);
}

Expand All @@ -110,15 +110,15 @@ export const expectTXTable = (
const curLine = logs[i];
if (curLine) {
const errorCode = curLine.match(
/Program log: Custom program error: (0x[0-9a-f]*)/
/Program log: Custom program error: (0x[0-9a-f]*)/,
);
if (errorCode && errorCode[1]) {
const programIdMatch = lastLine.split(" ");

if (programIdMatch && programIdMatch[1]) {
console.log(
` Program ${programIdMatch[1]} error:`,
Number(errorCode[1])
Number(errorCode[1]),
);
}
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export const expectTXTable = (
*/
export const assertTXSuccess = (
tx: TransactionEnvelope,
msg?: string
msg?: string,
): Chai.PromisedAssertion => {
return expectTX(tx, msg).to.be.fulfilled;
};
Expand All @@ -160,7 +160,7 @@ export const assertTXSuccess = (
export const assertTXThrows = (
tx: TransactionEnvelope,
err: IdlErrorCode,
msg?: string
msg?: string,
): Chai.PromisedAssertion => {
return expectTX(tx, msg).to.be.rejectedWith(matchError(err));
};
Expand All @@ -174,7 +174,7 @@ export const assertTXThrows = (
export const assertTXThrowsCode = (
tx: TransactionEnvelope,
code: number,
msg?: string
msg?: string,
): Chai.PromisedAssertion => {
return expectTX(tx, msg).to.be.rejectedWith(matchErrorCode(code));
};

0 comments on commit dd31390

Please sign in to comment.