I need to create a smart contract where each token of mine will be exchanged for 1 usdt, that is, a person sends 1 usdt to the function and he adds 1 token to his balance sheet.
How can I do it? with eth it is simple but usdt is being a challenge for me.
Does anyone have an example?
How can I test on the ropsten network?
[Ethereum] swap token for usdt, create function send 1 usdt and receive 1 token
erc-20etherremixsolidityusdt
Related Solutions
There are 2 ways using https://etherscan.io/ :
- Navigate to https://etherscan.io/ . Enter the account in the search box on the top right, the click Go. If there are tokens associated with the account, you will see the Token Tracker dropdow. Clicking this will show you the number of tokens owned by the account. For example: https://etherscan.io/address/0xc49cee55a099349bc5a67a6a454dbad3833e7b14 :
- If you know the token, navigate to https://etherscan.io/ . Select the menu Token -> View Tokens. Then select the token in the list. In this example, we will look at Golem. Enter the address of interest in the Filter By Address field and click Apply. The following screen shows the details for the same address in the example above:
Update Responding To The More Specific Question On Getting The Balance Using Solidity
Here is an example using GNT, where I got the ABI from the GNT contract details at 0xa74476443119A942dE498590Fe1f2454d7D4aC0d:
> var gntABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"golemFactory","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_master","type":"address"}],"name":"setMigrationMaster","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"migrate","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finalize","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"refund","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"migrationMaster","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenCreationCap","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_agent","type":"address"}],"name":"setMigrationAgent","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"migrationAgent","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"fundingEndBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalMigrated","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenCreationMin","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"funding","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenCreationRate","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"fundingStartBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"create","outputs":[],"payable":true,"type":"function"},{"inputs":[{"name":"_golemFactory","type":"address"},{"name":"_migrationMaster","type":"address"},{"name":"_fundingStartBlock","type":"uint256"},{"name":"_fundingEndBlock","type":"uint256"}],"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Migrate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Refund","type":"event"}];
undefined
> var gntAddress = "0xa74476443119A942dE498590Fe1f2454d7D4aC0d";
undefined
> var address = "0xc49cee55a099349bc5a67a6a454dbad3833e7b14"; // Address in examples above
undefined
> var gntContract = eth.contract(gntABI).at(gntAddress);
undefined
> var balance = gntContract.balanceOf(address).div(1e18);
undefined
> balance
23771.112437704491608715
Update With Script To Find GNT Balances For Multiple Accounts
The getGNTBalances
script from https://github.com/bokkypoobah/TokenTrader/tree/master/scripts will produce the following type of output:
# Account GNT ETH
------- ------------------------------------------ ---------------------------- --------------------------
0 0x4319c142f7b6cd722fc3a49289b8a22a7a51ca1e 180000000.000000000000000000 0.000000000000000000
1 0x168ae36b4386cea14475faa41498a0ea63c67dd7 100000999.999999991611392000 11717.218877544008288507
2 0xd39379d7887c6a9ebd01007e2b96efc774652047 57459993.000000000000000000 106160.984854218882216845
------- ------------------------------------------ ---------------------------- --------------------------
3 Total 337460992.999999991611392000 117878.203731762890505352
Just change the address in the script to any ERC20 compliant token to view the balances for that token.
This sort of transfer can be done in two stages.
The person who aspires to be good needs to first call vbToken.approve(<your contract's address>, 5)
(or rather 5 * 10**decimals
). This authorizes your contract to take 5 vbcoins from them.
They then call sendVBCoins(5)
(or, again, more likely 5 * 10**decimals
). Inside sendVBCoins
, you will call vbToken.transferFrom(msg.sender, _howmuch)
. If that succeeds, then you've received those vbcoins from them, and you can consider them to be a "good person."
Best Answer
For ERC20 the usual approach is to require the user to approve your contract.
You have to do something like this from the front end.
Smart contract