@project-serum/anchor
Advanced tools
Comparing version 0.6.0-beta.2 to 0.6.0-beta.3
@@ -27,2 +27,3 @@ "use strict"; | ||
const camelcase_1 = __importDefault(require("camelcase")); | ||
const base64 = __importStar(require("base64-js")); | ||
const snake_case_1 = require("snake-case"); | ||
@@ -183,13 +184,21 @@ const sha256 = __importStar(require("js-sha256")); | ||
this.layouts = new Map(layouts); | ||
this.discriminators = new Map(idl.events === undefined | ||
? [] | ||
: idl.events.map((e) => [ | ||
base64.fromByteArray(eventDiscriminator(e.name)), | ||
e.name, | ||
])); | ||
} | ||
encode(eventName, account) { | ||
const buffer = Buffer.alloc(1000); // TODO: use a tighter buffer. | ||
decode(log) { | ||
const logArr = Buffer.from(base64.toByteArray(log)); | ||
const disc = base64.fromByteArray(logArr.slice(0, 8)); | ||
// Only deserialize if the discriminator implies a proper event. | ||
const eventName = this.discriminators.get(disc); | ||
if (eventName === undefined) { | ||
return null; | ||
} | ||
const layout = this.layouts.get(eventName); | ||
const len = layout.encode(account, buffer); | ||
return buffer.slice(0, len); | ||
const data = layout.decode(logArr.slice(8)); | ||
return { data, name: eventName }; | ||
} | ||
decode(eventName, ix) { | ||
const layout = this.layouts.get(eventName); | ||
return layout.decode(ix); | ||
} | ||
} | ||
@@ -196,0 +205,0 @@ class StateCoder { |
@@ -23,16 +23,8 @@ "use strict"; | ||
exports.EventParser = void 0; | ||
const base64 = __importStar(require("base64-js")); | ||
const assert = __importStar(require("assert")); | ||
const coder_1 = require("../coder"); | ||
const LOG_START_INDEX = "Program log: ".length; | ||
class EventParser { | ||
constructor(coder, programId, idl) { | ||
constructor(coder, programId) { | ||
this.coder = coder; | ||
this.programId = programId; | ||
this.discriminators = new Map(idl.events === undefined | ||
? [] | ||
: idl.events.map((e) => [ | ||
base64.fromByteArray(coder_1.eventDiscriminator(e.name)), | ||
e.name, | ||
])); | ||
} | ||
@@ -89,13 +81,3 @@ // Each log given, represents an array of messages emitted by | ||
const logStr = log.slice(LOG_START_INDEX); | ||
const logArr = Buffer.from(base64.toByteArray(logStr)); | ||
const disc = base64.fromByteArray(logArr.slice(0, 8)); | ||
// Only deserialize if the discriminator implies a proper event. | ||
let event = null; | ||
let eventName = this.discriminators.get(disc); | ||
if (eventName !== undefined) { | ||
event = { | ||
name: eventName, | ||
data: this.coder.events.decode(eventName, logArr.slice(8)), | ||
}; | ||
} | ||
const event = this.coder.events.decode(logStr); | ||
return [event, null, false]; | ||
@@ -102,0 +84,0 @@ } |
@@ -123,3 +123,3 @@ "use strict"; | ||
addEventListener(eventName, callback) { | ||
const eventParser = new event_1.EventParser(this._coder, this._programId, this._idl); | ||
const eventParser = new event_1.EventParser(this._coder, this._programId); | ||
return this._provider.connection.onLogs(this._programId, (logs, ctx) => { | ||
@@ -126,0 +126,0 @@ if (logs.err) { |
@@ -11,3 +11,3 @@ "use strict"; | ||
const tx = txFn(...args); | ||
const [_, ctx] = context_1.splitArgsAndCtx(idlIx, [...args]); | ||
const [, ctx] = context_1.splitArgsAndCtx(idlIx, [...args]); | ||
let resp = undefined; | ||
@@ -37,3 +37,3 @@ try { | ||
if (idl.events) { | ||
let parser = new event_1.EventParser(coder, programId, idl); | ||
let parser = new event_1.EventParser(coder, programId); | ||
parser.parseLogs(logs, (event) => { | ||
@@ -40,0 +40,0 @@ events.push(event); |
/// <reference types="node" /> | ||
import { Idl, IdlTypeDef } from "./idl"; | ||
import { Event } from "./program/event"; | ||
/** | ||
@@ -92,5 +93,8 @@ * Number of bytes of the account discriminator. | ||
private layouts; | ||
/** | ||
* Maps base64 encoded event discriminator to event name. | ||
*/ | ||
private discriminators; | ||
constructor(idl: Idl); | ||
encode<T = any>(eventName: string, account: T): Buffer; | ||
decode<T = any>(eventName: string, ix: Buffer): T; | ||
decode(log: string): Event | null; | ||
} | ||
@@ -97,0 +101,0 @@ declare class StateCoder { |
import camelCase from "camelcase"; | ||
import * as base64 from "base64-js"; | ||
import { snakeCase } from "snake-case"; | ||
@@ -156,13 +157,21 @@ import * as sha256 from "js-sha256"; | ||
this.layouts = new Map(layouts); | ||
this.discriminators = new Map(idl.events === undefined | ||
? [] | ||
: idl.events.map((e) => [ | ||
base64.fromByteArray(eventDiscriminator(e.name)), | ||
e.name, | ||
])); | ||
} | ||
encode(eventName, account) { | ||
const buffer = Buffer.alloc(1000); // TODO: use a tighter buffer. | ||
decode(log) { | ||
const logArr = Buffer.from(base64.toByteArray(log)); | ||
const disc = base64.fromByteArray(logArr.slice(0, 8)); | ||
// Only deserialize if the discriminator implies a proper event. | ||
const eventName = this.discriminators.get(disc); | ||
if (eventName === undefined) { | ||
return null; | ||
} | ||
const layout = this.layouts.get(eventName); | ||
const len = layout.encode(account, buffer); | ||
return buffer.slice(0, len); | ||
const data = layout.decode(logArr.slice(8)); | ||
return { data, name: eventName }; | ||
} | ||
decode(eventName, ix) { | ||
const layout = this.layouts.get(eventName); | ||
return layout.decode(ix); | ||
} | ||
} | ||
@@ -169,0 +178,0 @@ class StateCoder { |
@@ -1,15 +0,7 @@ | ||
import * as base64 from "base64-js"; | ||
import * as assert from "assert"; | ||
import { eventDiscriminator } from "../coder"; | ||
const LOG_START_INDEX = "Program log: ".length; | ||
export class EventParser { | ||
constructor(coder, programId, idl) { | ||
constructor(coder, programId) { | ||
this.coder = coder; | ||
this.programId = programId; | ||
this.discriminators = new Map(idl.events === undefined | ||
? [] | ||
: idl.events.map((e) => [ | ||
base64.fromByteArray(eventDiscriminator(e.name)), | ||
e.name, | ||
])); | ||
} | ||
@@ -66,13 +58,3 @@ // Each log given, represents an array of messages emitted by | ||
const logStr = log.slice(LOG_START_INDEX); | ||
const logArr = Buffer.from(base64.toByteArray(logStr)); | ||
const disc = base64.fromByteArray(logArr.slice(0, 8)); | ||
// Only deserialize if the discriminator implies a proper event. | ||
let event = null; | ||
let eventName = this.discriminators.get(disc); | ||
if (eventName !== undefined) { | ||
event = { | ||
name: eventName, | ||
data: this.coder.events.decode(eventName, logArr.slice(8)), | ||
}; | ||
} | ||
const event = this.coder.events.decode(logStr); | ||
return [event, null, false]; | ||
@@ -79,0 +61,0 @@ } |
@@ -117,3 +117,3 @@ import { inflate } from "pako"; | ||
addEventListener(eventName, callback) { | ||
const eventParser = new EventParser(this._coder, this._programId, this._idl); | ||
const eventParser = new EventParser(this._coder, this._programId); | ||
return this._provider.connection.onLogs(this._programId, (logs, ctx) => { | ||
@@ -120,0 +120,0 @@ if (logs.err) { |
@@ -9,3 +9,3 @@ import { translateError } from "../common"; | ||
const tx = txFn(...args); | ||
const [_, ctx] = splitArgsAndCtx(idlIx, [...args]); | ||
const [, ctx] = splitArgsAndCtx(idlIx, [...args]); | ||
let resp = undefined; | ||
@@ -35,3 +35,3 @@ try { | ||
if (idl.events) { | ||
let parser = new EventParser(coder, programId, idl); | ||
let parser = new EventParser(coder, programId); | ||
parser.parseLogs(logs, (event) => { | ||
@@ -38,0 +38,0 @@ events.push(event); |
import { PublicKey } from "@solana/web3.js"; | ||
import Coder from "../coder"; | ||
import { Idl } from "../idl"; | ||
export declare type Event = { | ||
@@ -11,4 +10,3 @@ name: string; | ||
private programId; | ||
private discriminators; | ||
constructor(coder: Coder, programId: PublicKey, idl: Idl); | ||
constructor(coder: Coder, programId: PublicKey); | ||
parseLogs(logs: string[], callback: (log: Event) => void): void; | ||
@@ -15,0 +13,0 @@ private handleLog; |
{ | ||
"name": "@project-serum/anchor", | ||
"version": "0.6.0-beta.2", | ||
"version": "0.6.0-beta.3", | ||
"description": "Anchor client", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
import camelCase from "camelcase"; | ||
import * as base64 from "base64-js"; | ||
import { snakeCase } from "snake-case"; | ||
@@ -15,2 +16,3 @@ import { Layout } from "buffer-layout"; | ||
import { IdlError } from "./error"; | ||
import { Event } from "./program/event"; | ||
@@ -217,2 +219,7 @@ /** | ||
/** | ||
* Maps base64 encoded event discriminator to event name. | ||
*/ | ||
private discriminators: Map<string, string>; | ||
public constructor(idl: Idl) { | ||
@@ -237,14 +244,26 @@ if (idl.events === undefined) { | ||
this.layouts = new Map(layouts); | ||
} | ||
public encode<T = any>(eventName: string, account: T): Buffer { | ||
const buffer = Buffer.alloc(1000); // TODO: use a tighter buffer. | ||
const layout = this.layouts.get(eventName); | ||
const len = layout.encode(account, buffer); | ||
return buffer.slice(0, len); | ||
this.discriminators = new Map<string, string>( | ||
idl.events === undefined | ||
? [] | ||
: idl.events.map((e) => [ | ||
base64.fromByteArray(eventDiscriminator(e.name)), | ||
e.name, | ||
]) | ||
); | ||
} | ||
public decode<T = any>(eventName: string, ix: Buffer): T { | ||
public decode(log: string): Event | null { | ||
const logArr = Buffer.from(base64.toByteArray(log)); | ||
const disc = base64.fromByteArray(logArr.slice(0, 8)); | ||
// Only deserialize if the discriminator implies a proper event. | ||
const eventName = this.discriminators.get(disc); | ||
if (eventName === undefined) { | ||
return null; | ||
} | ||
const layout = this.layouts.get(eventName); | ||
return layout.decode(ix); | ||
const data = layout.decode(logArr.slice(8)); | ||
return { data, name: eventName }; | ||
} | ||
@@ -251,0 +270,0 @@ } |
import { PublicKey } from "@solana/web3.js"; | ||
import * as base64 from "base64-js"; | ||
import * as assert from "assert"; | ||
import Coder, { eventDiscriminator } from "../coder"; | ||
import { Idl } from "../idl"; | ||
import Coder from "../coder"; | ||
@@ -18,16 +16,6 @@ const LOG_START_INDEX = "Program log: ".length; | ||
private programId: PublicKey; | ||
// Maps base64 encoded event discriminator to event name. | ||
private discriminators: Map<string, string>; | ||
constructor(coder: Coder, programId: PublicKey, idl: Idl) { | ||
constructor(coder: Coder, programId: PublicKey) { | ||
this.coder = coder; | ||
this.programId = programId; | ||
this.discriminators = new Map<string, string>( | ||
idl.events === undefined | ||
? [] | ||
: idl.events.map((e) => [ | ||
base64.fromByteArray(eventDiscriminator(e.name)), | ||
e.name, | ||
]) | ||
); | ||
} | ||
@@ -92,13 +80,3 @@ | ||
const logStr = log.slice(LOG_START_INDEX); | ||
const logArr = Buffer.from(base64.toByteArray(logStr)); | ||
const disc = base64.fromByteArray(logArr.slice(0, 8)); | ||
// Only deserialize if the discriminator implies a proper event. | ||
let event = null; | ||
let eventName = this.discriminators.get(disc); | ||
if (eventName !== undefined) { | ||
event = { | ||
name: eventName, | ||
data: this.coder.events.decode(eventName, logArr.slice(8)), | ||
}; | ||
} | ||
const event = this.coder.events.decode(logStr); | ||
return [event, null, false]; | ||
@@ -105,0 +83,0 @@ } |
@@ -304,7 +304,3 @@ import { inflate } from "pako"; | ||
): number { | ||
const eventParser = new EventParser( | ||
this._coder, | ||
this._programId, | ||
this._idl | ||
); | ||
const eventParser = new EventParser(this._coder, this._programId); | ||
return this._provider.connection.onLogs(this._programId, (logs, ctx) => { | ||
@@ -311,0 +307,0 @@ if (logs.err) { |
@@ -41,3 +41,3 @@ import { PublicKey } from "@solana/web3.js"; | ||
const tx = txFn(...args); | ||
const [_, ctx] = splitArgsAndCtx(idlIx, [...args]); | ||
const [, ctx] = splitArgsAndCtx(idlIx, [...args]); | ||
let resp = undefined; | ||
@@ -67,3 +67,3 @@ try { | ||
if (idl.events) { | ||
let parser = new EventParser(coder, programId, idl); | ||
let parser = new EventParser(coder, programId); | ||
parser.parseLogs(logs, (event) => { | ||
@@ -70,0 +70,0 @@ events.push(event); |
import { | ||
Connection, | ||
Keypair, | ||
Signer, | ||
Signer, | ||
PublicKey, | ||
@@ -143,5 +143,3 @@ Transaction, | ||
const signerKps = signers.filter( | ||
(s) => s !== undefined | ||
) as Array<Signer>; | ||
const signerKps = signers.filter((s) => s !== undefined) as Array<Signer>; | ||
const signerPubkeys = [this.wallet.publicKey].concat( | ||
@@ -148,0 +146,0 @@ signerKps.map((s) => s.publicKey) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
564199
133
6682