Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use sol_log_data for logging events #1608

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ incremented for features.
* ts: Mark `transaction`, `instruction`, `simulate` and `rpc` program namespaces as deprecated in favor of `methods` ([#1539](https://github.com/project-serum/anchor/pull/1539)).
* ts: No longer allow manual setting of globally resolvable program public keys in `methods#accounts()`. ([#1548][https://github.com/project-serum/anchor/pull/1548])
* lang: Remove space calculation using [`#[derive(Default)]`] (https://github.com/project-serum/anchor/pull/1519).
* lang/ts: Events are now emitted using the `sol_log_data` syscall ([#1608](https://github.com/project-serum/anchor/pull/1608)).

## [0.22.1] - 2022-02-28

Expand Down
13 changes: 11 additions & 2 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub use solana_sdk;

mod cluster;

const PROGRAM_LOG: &str = "Program log: ";
const PROGRAM_DATA: &str = "Program data: ";

/// EventHandle unsubscribes from a program event stream on drop.
pub type EventHandle = PubsubClientSubscription<RpcResponse<RpcLogsResponse>>;

Expand Down Expand Up @@ -279,8 +282,14 @@ fn handle_program_log<T: anchor_lang::Event + anchor_lang::AnchorDeserialize>(
l: &str,
) -> Result<(Option<T>, Option<String>, bool), ClientError> {
// Log emitted from the current program.
if l.starts_with("Program log:") {
let log = l.to_string().split_off("Program log: ".len());
if l.starts_with(PROGRAM_LOG) || l.starts_with(PROGRAM_DATA) {
let log = {
if l.starts_with(PROGRAM_LOG) {
l.to_string().split_off(PROGRAM_LOG.len())
} else {
l.to_string().split_off(PROGRAM_DATA.len())
}
};
paul-schaaf marked this conversation as resolved.
Show resolved Hide resolved
let borsh_bytes = match anchor_lang::__private::base64::decode(&log) {
Ok(borsh_bytes) => borsh_bytes,
_ => {
Expand Down
4 changes: 1 addition & 3 deletions lang/attribute/event/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let data: proc_macro2::TokenStream = input.into();
proc_macro::TokenStream::from(quote! {
{
let data = anchor_lang::Event::data(&#data);
let msg_str = &anchor_lang::__private::base64::encode(data);
anchor_lang::solana_program::msg!(msg_str);
anchor_lang::solana_program::log::sol_log_data(&[&anchor_lang::Event::data(&#data)]);
}
})
}
Expand Down
13 changes: 9 additions & 4 deletions ts/src/program/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Coder } from "../coder/index.js";
import { DecodeType } from "./namespace/types.js";
import Provider from "../provider.js";

const LOG_START_INDEX = "Program log: ".length;
const PROGRAM_LOG = "Program log: ";
const PROGRAM_DATA = "Program data: ";
const PROGRAM_LOG_START_INDEX = PROGRAM_LOG.length;
const PROGRAM_DATA_START_INDEX = PROGRAM_DATA.length;

// Deserialized event.
export type Event<
Expand Down Expand Up @@ -213,9 +216,11 @@ export class EventParser {
private handleProgramLog(
log: string
): [Event | null, string | null, boolean] {
// This is a `msg!` log.
if (log.startsWith("Program log:")) {
const logStr = log.slice(LOG_START_INDEX);
// This is a `msg!` log or a `sol_log_data` log.
if (log.startsWith(PROGRAM_LOG) || log.startsWith(PROGRAM_DATA)) {
const logStr = log.startsWith(PROGRAM_LOG)
? log.slice(PROGRAM_LOG_START_INDEX)
: log.slice(PROGRAM_DATA_START_INDEX);
const event = this.coder.events.decode(logStr);
return [event, null, false];
}
Expand Down