Browse Source

use sol_log_data for logging events (#1608)

Paul 3 years ago
parent
commit
3b0006b3ce
4 changed files with 18 additions and 9 deletions
  1. 1 0
      CHANGELOG.md
  2. 7 2
      client/src/lib.rs
  3. 1 3
      lang/attribute/event/src/lib.rs
  4. 9 4
      ts/src/program/event.ts

+ 1 - 0
CHANGELOG.md

@@ -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)).
 

+ 7 - 2
client/src/lib.rs

@@ -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,
             _ => {

+ 1 - 3
lang/attribute/event/src/lib.rs

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

+ 9 - 4
ts/src/program/event.ts

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