This commit is contained in:
Armani Ferrante 2022-02-21 12:51:08 -05:00
parent fe252b0148
commit e9f8623337
No known key found for this signature in database
GPG Key ID: D597A80BCF8E12B7
4 changed files with 14 additions and 18 deletions

View File

@ -83,10 +83,6 @@ export class BorshEventCoder implements EventCoder {
}
}
export function eventDiscriminator(name: string): Buffer {
return this.header.discriminator(name);
}
class EventHeader {
constructor(private _idl: Idl) {}

View File

@ -7,8 +7,8 @@ import { Coder } from "../index.js";
export { BorshInstructionCoder } from "./instruction.js";
export { BorshAccountsCoder, BorshAccountHeader } from "./accounts.js";
export { BorshEventCoder, eventDiscriminator } from "./event.js";
export { BorshStateCoder, stateDiscriminator } from "./state.js";
export { BorshEventCoder } from "./event.js";
export { BorshStateCoder } from "./state.js";
/**
* BorshCoder is the default Coder for Anchor programs implementing the

View File

@ -34,13 +34,13 @@ export class BorshStateCoder {
data = data.slice(BorshAccountHeader.size());
return this.layout.decode(data);
}
}
// Calculates unique 8 byte discriminator prepended to all anchor state accounts.
export async function stateDiscriminator(name: string): Promise<Buffer> {
let ns = features.isSet("anchor-deprecated-state") ? "account" : "state";
return Buffer.from(sha256.digest(`${ns}:${name}`)).slice(
0,
this.header.discriminatorSize()
);
// Calculates unique 8 byte discriminator prepended to all anchor state accounts.
async discriminator(name: string): Promise<Buffer> {
let ns = features.isSet("anchor-deprecated-state") ? "account" : "state";
return Buffer.from(sha256.digest(`${ns}:${name}`)).slice(
0,
this.header.discriminatorSize()
);
}
}

View File

@ -8,7 +8,7 @@ import {
} from "@solana/web3.js";
import Provider, { getProvider } from "../../provider.js";
import { Idl, IdlInstruction, IdlStateMethod, IdlTypeDef } from "../../idl.js";
import { BorshCoder, Coder, stateDiscriminator } from "../../coder/index.js";
import { BorshCoder, Coder } from "../../coder/index.js";
import {
RpcNamespace,
InstructionNamespace,
@ -24,7 +24,6 @@ import InstructionNamespaceFactory from "./instruction.js";
import RpcNamespaceFactory from "./rpc.js";
import TransactionNamespaceFactory from "./transaction.js";
import { IdlTypes, TypeDef } from "./types.js";
import { BorshAccountHeader } from "../../coder/borsh/accounts.js";
export default class StateFactory {
public static build<IDL extends Idl>(
@ -85,7 +84,8 @@ export class StateClient<IDL extends Idl> {
*/
public readonly provider: Provider = getProvider(),
/**
* Returns the coder.
* Returns the coder. Note that we use BorshCoder and not `Coder` because
* the deprecated state abstraction only applies to Anchor programs.
*/
public readonly coder: BorshCoder = new BorshCoder(idl)
) {
@ -173,7 +173,7 @@ export class StateClient<IDL extends Idl> {
throw new Error("State is not specified in IDL.");
}
const expectedDiscriminator = await stateDiscriminator(state.struct.name);
const expectedDiscriminator = await this.coder.state.discriminator(state.struct.name);
const discriminator = this.coder.state.header.parseDiscriminator(
accountInfo.data
);