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.
Related
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.
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.
For each token in a list of tokens, I want to (programatically) get a list of all historical trades of that token on Uniswap or Pancakeswap, with accompanying information such as block number, trade quantity, gas price, token reserves, etc... What is the best way to do this?
I checked out etherscan.io, but its web interface only allows users to export the most recent DEX trades, not all of them, and its API doesn't seem to be able to solve my problem.
Everybody!
I'd like to get the BNB price from Binance API in solidity.
https://api.binance.com/api/v3/klines?symbol=BNBBUSD&interval=1m&limit=1
I can get candlestick data from the above link.
Is available same data in solidity?
Please help me.
Solidity can't query off-chain resources on its own. But you can use an oracle that will deliver the data to your contract.
A widely-used oracle service is Chainlink. Your contract queries their contract, pays with LINK for the query, and one of the Chanlink providers call your (predefined) callback function with the desired data.
Or you can build a simple oracle service on your own. Your off-chain app listens to specific transactions (from&to a predefined address in a predefined format). When this specific transaction is send, the off-chain app retrieves the off-chain data and sends a transaction (from a connected account) to your contract, passing the data.
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.