Web3 transaction from server
Unanswered
endre posted this in #help-forum
endreOP
Hello all.
I am working on a Web3-based application in Next.js and need some help on how to best solve a problem I have.
The users of the app can buy tickets for a off-chain lottery using on-chain tokens.
Today I have a next.js app that is more just react as everything happens on the client.
The user can connect with their wallet (using wallet-connect) and then I call a contact to see how many tokens they have and let the user buys tickets. This will first trigger a smart contract transaction to "charge" the tokens and when the transaction returns, an api call that then inserts everything in the database is triggered.
This is a ratter long transaction (in time) and there is a possibility that the uses does something that interrupts the api call.
So, my question is: Is there any way to do this from the server? Or how would you solve this?
I am working on a Web3-based application in Next.js and need some help on how to best solve a problem I have.
The users of the app can buy tickets for a off-chain lottery using on-chain tokens.
Today I have a next.js app that is more just react as everything happens on the client.
The user can connect with their wallet (using wallet-connect) and then I call a contact to see how many tokens they have and let the user buys tickets. This will first trigger a smart contract transaction to "charge" the tokens and when the transaction returns, an api call that then inserts everything in the database is triggered.
This is a ratter long transaction (in time) and there is a possibility that the uses does something that interrupts the api call.
So, my question is: Is there any way to do this from the server? Or how would you solve this?
16 Replies
Silver Marten
Just a few questions to help on the topic, how will the user see that he has the lottery ticket
What chain are you on?
what type of wallet connection library are you using?
endreOP
This is on ethereum. I an using wallet-connect and ethers for all things web3, but as mentioned, it is all on the client noe.
The user will be able to see how many tickets the connected wallet has bought on the website, but this will be retrieved from the off-chain database.
The user will be able to see how many tickets the connected wallet has bought on the website, but this will be retrieved from the off-chain database.
Silver Marten
honestly I don’t know how much you’ve built out but I would definitely recommend using rainbow kit
endreOP
Hmm, I will check it out. we are always looking at the best way to do stuff.
Silver Marten
you can run everything client side and using wagmi react hooks for each “purchaseâ€, allow a user to select an amount of tickets and multiple items by the price and use the useSendTransaction, once it resolves as successful, post it to the db
so you aren’t necessarily triggering any smart contract it’s just a hook on client side to send a transaction
unless your final goal is to do it via a smart contract, then sure you can do it like that
endreOP
for this one the tickets are stored of chain and not in a smart contract.
this useSendTrascaction is part of the rainbowkit? I will check it out but it might be a lot to change for now. We have lots of different staking etc functions in the same project. But I will take a look
this useSendTrascaction is part of the rainbowkit? I will check it out but it might be a lot to change for now. We have lots of different staking etc functions in the same project. But I will take a look
@endre for this one the tickets are stored of chain and not in a smart contract.
this useSendTrascaction is part of the rainbowkit? I will check it out but it might be a lot to change for now. We have lots of different staking etc functions in the same project. But I will take a look
Silver Marten
it’s part of a library called WAGMI, it’s a react hooks library that helps interact with ethereum blockchain etc
they work on server side too
@endre Hello all.
I am working on a Web3-based application in Next.js and need some help on how to best solve a problem I have.
The users of the app can buy tickets for a off-chain lottery using on-chain tokens.
Today I have a next.js app that is more just react as everything happens on the client.
The user can connect with their wallet (using wallet-connect) and then I call a contact to see how many tokens they have and let the user buys tickets. This will first trigger a smart contract transaction to "charge" the tokens and when the transaction returns, an api call that then inserts everything in the database is triggered.
This is a ratter long transaction (in time) and there is a possibility that the uses does something that interrupts the api call.
So, my question is: Is there any way to do this from the server? Or how would you solve this?
Asiatic Lion
Maybe you can do an "optimistic" function, don't wait your response to charge the tokens, charge it and buy a ticket, but wait only the response of the buy ticket function to throw an error if necessary
You can't do it by the server faster if you need to check a smart contract
You can't do it by the server faster if you need to check a smart contract
endreOP
Thanks for your replies. I will check out wagmi and rainbowkit.
I have also considered the optimistic approche and will use that for the next version.
I have also considered the optimistic approche and will use that for the next version.
@endre Thanks for your replies. I will check out wagmi and rainbowkit.
I have also considered the optimistic approche and will use that for the next version.
Havana
I 2nd wagmi and rainbow kit, both have better integration on client side