JSSDK 0.5.7: Event Log supports & enhanced contract transaction tracking
2023-12-06
The JS-SDK 0.5.7 introduces a significant enhancement for PinkLoggerContractPromise
, which allows you to fetch logs for your Phat Contract.
The updates consist of several parts.
Obtain a ready-to-use logger instance more easily
Before version 0.5.7, you needed to remember the number of arguments required to create a logger instance. However, in version 0.5.7, we introduced a new helper function called getLogger
that allows you to obtain a logger instance without having to worry about tedious and order-sensitive arguments.
In on-chain mode, you only need to provide a named argument transport
which is the same as getClient. This will open an additional WebSocket connection to the RPC endpoint.
import { getLogger } from '@phala/sdk'
const logger = await getLogger({ transport: 'wss://poc6.phala.network/ws' })
To use off-chain mode, which only sends requests on demand, you need to provide the endpoint URL and contractId
to the PRuntime. Here is a snippet that works for PoC6 testnet:
import { getLogger } from '@phala/sdk'
const logger = await getLogger({
pruntimeURL: 'https://poc6.phala.network/pruntime/0x923462b4',
contractId: '0x1c825d94cdacab15de009d169e0f4893b5fd33743fba010fee277a9e529431ed',
})
You may be wondering about the difference between on-chain mode and off-chain mode. In short, off-chain mode is always faster and uses fewer resources than on-chain mode. However, in order to make it work, you need to specify the key arguments (PRuntime endpoint URL and logger contractId). Additionally, in off-chain mode, you will bypass the step of getting information from the on-chain registry. This mode is suitable for long-run daemon scenarios.
Event Chain Decode supports
Phat Contract, like Ink! contracts, also supports contract events. To emit a contract event, use the following syntax:
self.env().emit_event(OwnerChanged {
address,
});
Unlike Ink! contracts, Phat Contract runs off-chain. This means that the events data is only available within the "event chain" inside the cluster. The logger is the only way to access the event data.
Before version 0.5.7, event logs could be retrieved using PinkLoggerContractPromise
, but decoding was not supported. However, in version 0.5.7, the missing piece was added. To decode events automatically, simply pass the Abi JSON string or Abi
instance to the corresponding functions:
const abi = fs.readFileSync('path/to/your/contract.json', 'utf8')
const logger = await getLogger({ transport: 'wss://poc6.phala.network/ws' })
const { records } = await logger.tail(100, { abi }) // Or logger.head(100, { abi })
Limit the filtering conditions for log retrieval
Starting from version 0.5.7, we have added support for filter conditions when retrieving logs using head
or tail
commands.
type
: Only the specified types of logs will be returned, which can includeLog
,Event
,MessageOutput
,QueryIn
, andTooLarge
.
topic
: To filter events by topic, make sure to set the type filter to "Event". You can use a readable topic name (prefixed with the contract name in Pascal case) instead of providing the hashed topic.
const { records } = await logger.tail(100, { type: 'Event', topic: 'PhatContract::OwnerChanged' })
nonce
: The nonce is a filter for theMessageOutput
type, representing the unique ID of the contract transaction. It can be accessed through theSubmittableResult
.
const result = await contractPromise.send.newBadge(
{ pair, address, cert },
'New Badge 01'
)
const nonce = result.nonce
await result.waitFinalized()
const { records } = await logger.tail(100, { type: 'MessageOutput', nonce })
Others
- Type hints for different logger methods.
- Fixed incorrect type hint for
strategy
argument.
These updates also include updates to the tail.js
and tail-offchain.js
scripts in the cookbook. You can check out those updates as well.