use sol_log_data for logging events (#1608)

This commit is contained in:
Paul 2022-03-16 14:28:19 -04:00 committed by GitHub
parent b2b1a16b77
commit 3b0006b3ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 9 deletions

View File

@ -33,6 +33,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/ts: Events are now emitted using the `sol_log_data` syscall ([#1608](https://github.com/project-serum/anchor/pull/1608)).
* lang: Remove space calculation using `#[derive(Default)]` ([#1519](https://github.com/project-serum/anchor/pull/1519)).
* lang: Add support for logging expected and actual values and pubkeys. Add `require_eq` and `require_keys_eq` macros. Add default error code to `require` macro ([#1572](https://github.com/project-serum/anchor/pull/1572)).

View File

@ -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>>;
@ -279,8 +282,10 @@ 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 let Some(log) = l
.strip_prefix(PROGRAM_LOG)
.or_else(|| l.strip_prefix(PROGRAM_DATA))
{
let borsh_bytes = match anchor_lang::__private::base64::decode(&log) {
Ok(borsh_bytes) => borsh_bytes,
_ => {

View File

@ -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)]);
}
})
}

View File

@ -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<
@ -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];
}