I tried to create lp token from token created in bsctestnet and BNB
I used this testnet router address 0xD99D1c33F9fC3444f8101754aBC46c52416550D1 where there are two functions. Which of them should i use:
addLiquidityETH or addLiquidity .
When i used the first function i get contract error..reverted.
You need to call addLiquidityETH but you first need to approve the needed amount (or the max), e.i.
IERC20(uniswapV2Pair).approve(address(uniswapV2Router),type(uint).max);
uniswapV2Router.addLiquidityEth{value:ethAmount}(...);
Related
I have the following scenario:
I have a contract to generate a limited amount of erc20 tokens. User A buys X amount of this token. Then user A wants to sell some of his tokens, for X amount of ether, then user B wants to buy tokens of user A, how can I automatically transfer tokens from user A to user B when the transfer of ether from user B to user A has been successful be transfer?
Do I need a second contract to handle this kind of operation?
You may have two options,
Play around with Escrow SmartContracts (Like Openzeppelin) and modify it as per your need.
Use ERC20 Approve(spender,amount) on User B, to get allowance for your contract to spend on behalf of B. (spender : Your contract address)
Run a web3 script to listen on events to catch the corresponding transaction of sending ether from B->A ,
once the ether is transferred, you can place a function call to smart contract to spend the token from B to A
In your ERC20 Contract implementation, you can have a mapping of address -> eth, where the eth represents the selling price each user has set up.
Now, when a user "sells" their token, they can allow the same contract to transfer X amount of tokens from their balance which they want to sell.
And to sell, you can create a payable function that receives ether, and via parameter a seller address. This function will dynamically ( using the address received on the mapping we created at the beginning ) transfer X tokens to the buyer, using that seller's token price and the amount of ether received.
For example: If 1TKs = 0.5ETH, and the buyer sends 2eth, the contract will transfer 4TKS.
Recomendation.
Usually, the token contract and the seller/buyer functions are done in different contracts for security reasons, for example having the actual token, and the token shop separated on different contracts.
Disclaimer.
This is the simplest implementation I could think of, but it will accomplish your requirements.
I'm new to solidity and I wanted to develop a subscription contract where a user can subscribe to a merchants plan and pay. But I'm unable to subscribe function and transfer the token to merchant.
I'm using open zeppelin ERC20 standard token to transfer.
IERC20 token = IERC20(plans[planId].token);
token.transferFrom(
payable(msg.sender),
payable(plan.merchant),
plan.amount
);
I don't know why but this keeps giving me allowance error even though I have increased the allowance of the user.
Since you're invoking the token.transferFrom() from the subscription contract, the token owner needs to increaseAllowance() for the subscription contract to manipulate their tokens.
Example:
Your subscription contract is deployed on address 0x123.
Token address is 0x456, and it has 18 decimals.
The user needs to execute increaseAllowance(0x123, 500 * 1e18) on the token contract in order to increase the allowance by 500 tokens.
Mind the subscription contract address as the first param, and the decimals as the second param (so the actual value passed to the second param is 500000000000000000000 because it includes the decimals as units as well).
I'm working on BSCSCAN API. My goal is to get full balance for a given address as you can see on this page:
https://bscscan.com/tokenholdings?a=0x1c82bdb6a93f50a564155ddbcb9cf1b1a244d169
I checked the documentation on BscScan Website, and it seems I just could get:
-BNB BALANCE (not the full token balance) for a given address
-BALANCE of an address by giving the contract address of a single token
While I want to get the full balance for a given address. What is the right endpoints I should use? Thank you in advice.
This API returns the Value of BNB and not the Value of Tokens in BNB
https://api.bscscan.com/api?module=account&action=balance&address=0x0Bb3A1517624Ff45186Df879E9292f67750F8EB0&apikey=YOURAPIKEYBSCSCAN
This API returns the token Balance of and address
https://api.bscscan.com/api?module=account&action=tokenbalance&contractaddress=YOURCONTRACTADDRESS&address=YOURADDRESS&tag=latest&apikey=YOURAPIKEY
I am looking at this staking contract (pancake swap):
0x73feaa1ee314f8c655e354234017be2193c9e24e
and it's token (cake):
0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82
you can find a summary here:
https://bscscan.com/token/0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82?a=0x73feaa1ee314f8c655e354234017be2193c9e24e
On bscscan, I can get the following information:
price of token $12.54
market cap $2.9b
supply 231m
holders 137k
balance 88m (cake)
With nethereum, when I call:
web3.Eth.GetBalance.SendRequestAsync
I get:
with staking contract address: 479430015101300880
with token address: 3459819507903896496
and I'm not sure what the units are, nor how to map this to the information from bscscan.
I'm trying to get both the token value, but also what is the balance in the contract and I can't seem to find a way to do this.
so I looked at the nethereum playground and found an example dealing with ERC20 tokens, but when I put my addresses in it, the output is 0 and absolutely no error message, etc.
I want to test my truffle contract with multiple msg.sender addresses. Like "the first user sell token, the second user buys this token". For one address I simply write something like contract.buy.value(10 wei)();. But where I could get another address and how to send money from him?
I write my tests on solidity, not on javascript.
as you can see in the Truffle docs, you can specify two different accounts to interact with your deployed smart contract like below (Metacoin example):
var account_one = "0x1234..."; // an address
var account_two = "0xabcd..."; // another address
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.sendCoin(account_two, 10, {from: account_one});
}).then(function(result) {
// If this callback is called, the transaction was successfully processed.
alert("Transaction successful!")
}).catch(function(e) {
// There was an error! Handle it.
})
This is about how you can do with your own created token.
If you want to transfer Ether between accounts, you can specify accounts in your truffle execution file (a javascript file). And these accounts may come from your configured local blockchain (Ganache, if you are using Truffle Suite to test your smart contract, it will provide you with several accounts and you can configure these by yourself).
Moreover, you may need javascript API to specify the sender and receiver: web3.eth.sendTransaction.
First time to answer a question, hope this will help.