How permit function works - blockchain

I'm trying to figure out what's purpose of permit function from the Uniswap core
I know that ecrecover function ensures, that messages was signed by some address, but that's not clear for me -- why we can use permit function?

The owner can sign a structured message which produces a signature (separated into variables v, r, and s).
Meaning of the message could be described like "I, the owner, am approving the spender to operate value of my LP tokens of this specific Uniswap pair. This approval needs to be recorded onchain before deadline."
The owner then passes the signature and the values offchain to anyone else (e.g. to the spender), who executes the permit() function. By executing the permit() function, the approval becomes valid since now it's recorded onchain.
This is useful in cases when the owner has LP tokens - but doesn't have sufficient native balance to pay for gas fees for their transfer or approval.
Or when the owner is a user of your app and you have some kind of an agreement that your app will pay gas fees for the user. E.g. because they compensate you for the gas fees using another way - stablecoins, monthly fee, etc.

In Erc20, transferFrom allows decentralized exchange (DEX) to transfer funds from your account. But before calling this function, you have to call approve function, to allow the DEX how much fund it can use. So you are calling 2 functions and each will cost you a gas fee. Those gas fees are high and If you are trading often on DEX, you will be paying a lot of gas fees.
With permit function, you do not need to call the approve function. You are approving the transaction by signing the transaction. This transaction is signed off-chain so you are not paying any gas fees. Fron-end developers handle this part and then they derive the v,r,s of the signature. If you look at the args of the permit function, it expects v,r,s arguments.
permit() function allows anyone to authorize and swap a token in one transaction instead of two transactions. But this does not mean that you are saving half the gas fees. For example, if you were paying 10 wei gas fees for two transactions now it is not going to be 5 wei because permit function has more logic to implement. So the total gas fee will be between 5-10 wei. permit is not about just saving the gas fee but delegating the transaction to another wallet so that the wallet will pay the transaction. that is called gasless transaction.

Related

BEP-20 tokenized investment fund smart contracts

I’m working on a security token for a tokenized hedge fund and I need help in how to structure the project.
My background is in algorithmic trading and I’ve developed and tested an algorithm for trading cryptos. My goal is to turn this into a token to democratize the kind of investment usually only restricted to accredited investors. And of course, I would charge a small management fee.
My idea was to create a BEP-20 token, sell a number of tokens in an STO, burn whatever is not sold initially, and start trading with the funds from the STO. My management fee would be charged by minting new tokens periodically (daily or weekly).
A couple of questions:
I want to do the STO on a DEX (for regulatory reasons). I was thinking Pancakeswap, pairing my token with BUSD or USDT, but how can I control the price with their AMM algo? Is there a way to automatically adjust liquidity with a smart contract?
The value of the token will be linked to the underlying assets in the funds (10 different cryptos + 1 or more stablecoins depending on the size of the fund). Assuming you have the addresses where the 10 cryptos (plus the stablecoin) are held, how do you know how much is held in each and therefore are able to determine the total value of the fund?
And once you have determined the value of the token (total value of the fund divided by number of tokens in circulation), can you control the price on Pancakeswap with a smart contract? Or is this done separately?
In the end, the goal is to create a security token which value is guaranteed and linked to the underlying assets in the fund.
Am I approaching the problem the wrong way? If so, how would you do it?
You could theoretically "adjust" smart contract liquidity by using the router in Uniswap which would give you methods to add liquidity to a ERC-20 token.
Having a token linked to underlying assets could be achieved by having the token go through a defi protocol like avve, your token would have a value and by making your asset the collateral you could achieve purchasing power for other currencies.
The first price is given by the rate you make in the liquidity pool, if you were to create the token with a LP of 50 ETH then your token starting price would be 1/2 ETH. It's up to the market to determine the price after that.

How to calculate total transaction fees paid by users?

I want to calculate total transaction fees paid by users by using a substrate-api-sidecar on any substrate-based chain.
I tried reading two docs on calculating fees but none could help me out in deducing what to look for while querying a block. Click here for document 1, here for document 2.
Would that differ from chain to chain implementation?
I checked Moonriver documents on using substrate-api-sidecar and it seems possible to calculate fees. But I could not find out how to do so against a Polkadot parachain(relay chain)?
For setting the base premise, Token Terminal states that in a blockchain, total revenue generated means total transaction fees paid by the users.

Is there a way to get token price on transfer function call?

is it possible to know for which price user is buying/selling token on transfer function call?
I want to store information on the contract for with user bought/sold tokens on the transfer function.
No, this is not possible, at least in a token contract, because the token contract only "knows" that the user send or approved tokens and that's all that can know, it could be possible in a dex or cex contract
Yes it is possible. this is where oracles comes into play chainlink oracle provide off-chain data like token price feeds in contract in a decentralised way. Here is how you can get token price feed in smart contract in EVM-compatible blockchains
To keep record of price on each transfer extend transfer event to emit the price as well that you fetch from oracle.
Your Flow for modified transfer function will be something like this.
All Checks to ensure (valid transfer + valid oracle call)
Get oracle price of token from oracle and store it in local variable
Transfer
Emit modified event emit Transfer(sender, recipient, amount, price); where price is current price that you got from oracle.

How do I generate bitcoin addresses with computer code?

I have a website, and I need each user of the site to have their own unique bitcoin address. Any funds sent to these addresses I need to be automatically transferred to a master wallet for me.
Any ideas on how I can set this up easily? The first stage is to generate bitcoin wallet addresses with code. I have no idea how to do this. The second question is then how do I access those funds and withdraw the money.
You can use the Bitcoin Core Api getaccountaddress with the account parameter as the user Unique ID from your Database. It will generate addresses that belongs to specific user but with a single Private Key that you own.
You can spend the funds with just normal transaction on Bitcoin Core Wallet.

Partial Captures on Order payment in Spree

I am using Spree and I have modified it as per client requirement. I am stuck at one modification, I need an expert opinion.
Case that Spree provides by default
Spree provides default payments of auto-capture on/off on order and full payment is captured
Modifications that I want
with auto-capture mode = off
Verify that user has a certain amount in its account that can be less than total amount of order.
Make a partial payment capture of that and store remaining amount somewhere.
When order gets completed after shipment etc or some other action was done, capture the remaining amount.
My Questions
How should I modify Spree to achieve these tasks?
If I make a transaction of $5 with auto-capture = off, Is it possible to capture $10 when transacting that $5 authorization? If it is not possible, then do I have to update this $5 transaction's amount with $10 using OAuth Token. If yes, then how?
Extra Info
I want gateway independent solution by using Spree Payments classes so that I don't have to configure this for each Gateway I integrate. Right now I have 7-8 gateways including Stripe and Paypal.
Case creation for my problem, just an example
Total Order Payment = $50.
First of all I want to verify that user has $10 in his/her account?
If user has $10 then make a transaction with auto capture mode = off and in someway keep remaining amount i.e $50 - $10 in record somewhere.
Now I capture $10 and after 2 days shipment gets completed. Now I want to capture that remaining $40 and order gets completed.
Your question must be answered in the context of each individual Payment Gateway as each one operates with different rules.
You have asked too many questions to provide a specific answer but generally speaking CNP (Card-Not-Present) transactions do not let you capture above the authorized amount, but it depends on your industry and the deal you have with your merchant and payment processor.
2- If I make a transaction of $5 with auto-capture = off, Is it possible to capture $10 when transacting that $5 authorization? If it is not possible, then do I have to update this $5 transaction's amount with $10 using OAuth Token. If yes, then how?
Generally you do not "update" a prior transaction to a different amount (although as I said above, in other industries it is sometimes possible to capture at an amount higher than the authorized amount). You should be authorizing the total amount you may want to capture, an then make multiple capture calls to your payment gateway as the goods ship or services are delivered (by CC industry rules, you are generally not allowed to capture until you have delivered the service.)
I don't know of any Payment gateways that use "Oauth tokens" for communication, that is generally an authentication-based, not API based, technology.
-> I want gateway independent solution by using Spree Payments classes so that I don't have to configure this for each Gateway I integrate. Right now I have 7-8 gateways including Stripe and Paypal.
Sounds like a huge disaster. Get rid of them, the time debugging each gateway alone makes this an absolutely insane idea.
Total Order Payment = $50.
1) First of all I want to verify that user has $10 in his/her account?
Generally you can't "peak" into someone's private bank account and see how much money they have-- this is a feature you invented. What you can do is authorize the total amount you might want to charge in the future.
2) If user has $10 then make a transaction with auto capture mode = off and in someway keep remaining amount i.e $50 - $10 in record somewhere.
3) Now I capture $10 and after 2 days shipment gets completed. Now I want to capture that remaining $40 and order gets completed.
What you need is one authorization with multiple captures. Call your Payment processor representative and have a conversation about this with them and see what rules govern your merchant and processor accounts.