ERC-20 integration
Last updated
Last updated
This is experimental. Contact us @megapot_io on Twitter for more details.
Try out the testnet app at staging.megapot.io to get a feel for all of its pages.
Buying tickets, viewing history, claiming fees are the same as the BLAST lottery integration. Please see BLAST integration
For ERC-20's, it's ideal that the lottery is hosted on your own page for maximum discoverability.
If it's hosted on our site, most players will go for the BLAST lottery which has the biggest jackpot or the ETH lottery since it's a token they already have. This likely means few players will play your lottery, such that fees will be low, and LPs won't be interested.
Since the lottery is hosted on your page, you need a user to be able to add liquidity to it or want to run the lottery yourself as well. Instructions for both are below.
Prior to any of these actions, you need to ensure a user has approved spending a token with that contract. For example, here you'd go to the token address > Write Contract > approve. Set the spender as the contract here, with an amount you want.
Use lpDeposit
riskPercentage
is an integer between 1 and 100
value
is the amount to deposit, in WEI. In $ANDY's case, it's 9 decimals so make sure it's 10^9
You can fetch the minimum LP deposit amount with minLpDeposit
The max LP deposit should not matter, it's lpPoolCap
- lpPoolTotal
, but it should be initialized at 100M so I don't think it's worth showing.
We recommend letting people know the minimum needed to supply.
We recommend providing defaults for users who likely won't read through the stake.
You also use lpDeposit
. You can do any of the following:
Update risk percentage only: Set new riskPercentage
, set value
as 0
.
Add more liquidity while keeping same risk percentage: Set value
as additional amount, set riskPercentage
as the existing one
Add more liquidity and change risk percentage: Set value
as additional amount, set new riskPercentage
We recommend letting people know they can edit liquidity without adding more tokens (eg. leave at 0).
Call withdrawAllLP from your wallet at any time. You must wait until the next lottery run to be able to withdraw. Otherwise, players will be confused why the jackpot got smaller!
After the next lottery run, your riskPercentage
will be set to 0, and so your stake will also be 0. Then call withdrawAllLP
again to withdraw the full amount.
withdrawAllLP
is set up to work differently depending on if an LP has money in the jackpot:
You can call it at any time when an LP has money in the jackpot (stake >= 1%). This sets a user's "stake %" to 0%, but their money is still in the jackpot. After the lottery runs, and their position is rebalanced, they no longer have any money in the jackpot.
Call the same function again, now that LP has no money in the jackpot. This withdraws all of a LP's position.
We recommend letting users know that they can withdraw after the current lottery ends:
We then recommend showing a countdown for when they can withdraw. This is the same countdown as the lottery end time. (countdown not shown below)
Use the read function lpsInfo
with the contract address. It returns these values:
Principal in WEI
Stake in WEI
riskPercentage as an integer
if the LP is active (has any deposits)
We recommend showing the total position which is the sum of principal and stake.
For users who just deposited, let them know that this stake will take into effect starting next lottery
Our lottery is not automatically run -- we can set up a cron job to run it if you'd like.
Below are instructions to do it yourself.
With few players and LPs, the call to request a random number will run the lottery.
Fetch the lottery fee at Blastscan > Read Proxy Contract > getLotteryFee. Convert this from WEI to ETH, thus, divide by 10^18
Go to Blastscan > Write Proxy Contract > RunLottery
For runLottery - payableAmount (ether)
, enter in the lottery fee in WEI.
For userRandomNumber - bytes32
, enter in 0x55fb29339a98ca25bedb7b5aa225041f669ca1407e926a95ce4a9b080ac66907
(note: reusing this is fine, Pyth generates randomness, this seed further maximizes randomness - generate your own with Web3.utils.randomHex(32)
)
You can confirm the lottery was run by going to Contract > Events > RevealedWithCallback > Logs. Scroll down to see the LotteryRun
event.
When you have a bunch of LPs and users, running the lottery requires more gas than Pyth’s standard response will spend. Thus, you need to make multiple calls:
Kick off the lottery run, which generates a random number from Pyth
Fetch the lottery fee at Blastscan > Read Proxy Contract > getLotteryFee. Convert this from WEI to ETH, thus, divide by 10^18
Go to Blastscan > Write Proxy Contract > RunLottery
For runLottery - payableAmount (ether)
, enter in the lottery fee in WEI.
For userRandomNumber - bytes32
, enter in 0x55fb29339a98ca25bedb7b5aa225041f669ca1407e926a95ce4a9b080ac66907
Note: reusing this is fine, Pyth generates randomness, this seed further maximizes randomness
You can generate your own with Web3.utils.randomHex(32)
)
Request callback from Pyth, which runs the lottery
Go to Blastscan > Pyth's Blast contract > RevealwithCallback
For these arguments:
provider (address)
sequenceNumber (uint64)
userRandomNumber (bytes32)
Fetch the values from the RequestedWithCallback
log from above. This event is emitted with the runLottery
method on our contract is called. Make sure to pass them with the correct signatures.
For the last argument, providerRevelation (bytes32):
Go to the following endpoint: and use the sequence number: https://fortuna.dourolabs.app/v1/chains/blast/revelations/[sequenceNumber]
You will find the providerRevelation
in the data field.
You can confirm the lottery was run by going to Contract > Events > RevealedWithCallback > Logs. Scroll down to see the LotteryRun
event.