How to get clock in a program
Getting a clock (ie, the current time) can be done in two ways:
- Passing
SYSVAR_CLOCK_PUBKEY
into an instruction - Accessing Clock directly inside an instruction.
It is nice to know both the methods, because some legacy programs still expect
the SYSVAR_CLOCK_PUBKEY
as an account.
Passing Clock as an account inside an instruction
Let's create an instruction which receives an account for initializing and the sysvar pubkey
Now we pass the clock's sysvar public address via the client
Accessing Clock directly inside an instruction
Let's create the same instruction, but without expecting the
SYSVAR_CLOCK_PUBKEY
from the client side.
The client side instruction, now only needs to pass the state and payer accounts.