Godwoken layer 2 is not fixed block time, so I need a way to get Nervos Network Layer1 blockNumber to support the DeFi Project . Is there a solution now?
The layer2 block info on GwScan explorer contains layer1 block number.
see: https://v1.testnet.gwscan.com/block/0xd387a364a24f7d091f2dec81b1fb6b9eaf133e2eddb22dbf34dca5531940eb44
These data can be fetched from GwScan Public Service
query ($hash: String!) {
block(input: { hash: $hash }) {
layer1_tx_hash
layer1_block_number
}
blocks {
layer1_tx_hash
layer1_block_number
}
}
variables {
"hash": "0xd6e95eab94eb485925d9bc4e4aa034a8bec0c46a1164fd2ff54025564037dd01"
}
An endpoint for public usage is active at https://api.v1-betanet.gwscan.com/graphql though, a self-hosted service is strongly recommended for your own dapps.
In the Defi protocol, there needs to be a way to obtain the on-chain time in the contract to calculate the loan interest or the income of liquidity mining. Obtaining the time in the block explorer should not meet the demand. What can Godwoken use as a stable and reliable source of time? I remember Arbitrum has a special system call.
Is there another way /function to get layer1 block number , uses solidity code on layer2 (just like block.number, block.timestamp on smart contract)?
see also: Yokai TimelessStakingRewards contract function lastTimeRewardApplicable()
yokai-swap-farm/contracts/TimelessStakingRewards.sol at main · YokaiSwap/yokai-swap-farm · GitHub
You can use layer2 block timestamp to calculate the interests. The layer1 script verifies the l2 block timestamp to ensure it is reliable.
Noted, so I have to use block.timestamp to instead of block.number , this may not be a small change.