I am using the below code in solidity to transfer ether from one account to another.
I am calling this from the owner's account.
But my ether gets deducted from owners and goes to contract address instead of the receiver/payee account.
function PayCredit(address payable payee, uint money, uint invoiceNum) public payable{
require(msg.sender==owner, "only owner can invoke it");
payee.transfer(address(this).balance);
claims[payee][invoiceNum].isPayed = true;
}
You are sending your ether's to contract address , change address(this) to address .
I would suggest you good practice of sending ether's to other account. Solidity transaction support value as argument and this is good place for sending ether(WEI) to other account . below code snippet will send 12 WEI to other account.
pragma solidity >=0.4.22 <0.6.0;
contract AB {
uint256 num1;
address owner;
constructor() public{
owner = msg.sender;
}
function sendBal(address payable receiver) payable external onlyOwner {
uint256 amount = msg.value;
receiver.transfer(amount);
}
Related
I am trying to mint new token. This is part of code. On the fifth line is definition of list of four adresses,
First is contract of code which will be reward, second is router (every DEX has unique one), third is marketing wallet, fourth is dividendTracker.
Where can I have dividendTracker adress?
Thank you
constructor(
string memory name_,
string memory symbol_,
uint256 totalSupply_,
address[4] memory addrs, // reward, router, marketing wallet, dividendTracker
uint256[3] memory feeSettings, // rewards, liquidity, marketing
uint256 minimumTokenBalanceForDividends_,
address serviceFeeReceiver_,
uint256 serviceFee_
I'm trying to call claimItem function on my ERC721 token. This function basically calls _safeMint() function from OpenZeppelin ERC721 contract. Here is the question. As a first step I managed deploy a contract on polygon mumbai testnet and I can see my contract on polygon scan. But when I tried to call claimItem function, inside a Transaciton Transaction(from:EthereumAddress.fromHex(session!.accounts[0]) ,to:EthereumAddress.fromHex("CONTRACT_ADDRESS"), data: bytes); like this. Transaction completes but NFT is not minted.
Here Are Some Scree Shots
This one is from a successful one (This is deployed and minted with truffle & web3)
This one is from unsuccessful one (This is signed by MetaMask)
I don't know what I'm doing wrong and I'm really new into blockchain.
EDIT Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
contract ArtItemNew is ERC721, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
using Strings for uint256;
// Optional mapping for token URIs
mapping(uint256 => string) private _tokenURIs;
constructor(string memory _tokenName, string memory _tokenSymbol) ERC721(_tokenName, _tokenSymbol) public {
}
function _setTokenURI(uint256 tokenId, string memory _tokenURI)
internal
virtual
{
require(
_exists(tokenId),
"ERC721Metadata: URI set of nonexistent token"
);
_tokenURIs[tokenId] = _tokenURI;
}
function claimItem(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}
I'm fresh on solidity, when I use remix to test my contract, I want to transfer some eth from my account to the smart contract. I have tried this code, but it seems to transfer the eth from the contract but not my account.
function addStaker (uint _stakeAmount) public membership(master, msg.sender) returns(bool) {
if(!members[msg.sender].alreadyExist) {
Member memory newMember = Member(msg.sender, true);
members[msg.sender] = newMember;
bool sent = payable(address(this)).send(_stakeAmount);
require(sent, "invalid balance");
return true;
} else {
return false;
}
}
How could I transfer eth from my account to the smart contract?
A smart contract is not able to pull a contract-specified amount of ETH from an address. The amount needs to always be specified and signed by the sender.
The contract can only validate the sent amount:
function addStaker() public payable {
require(msg.value == 1 ether);
}
How to chose the amount depends on the wallet software you're using. In case of Remix, there's the "Value" input in the "Deploy & Run Transactions" tab, where you specify the amount of ETH sent with the transaction.
If you want to implement a defi contract (since your function name is addStaker) that accepts staked coins from ERC20 tokens, the implementation is different. But if you just want to send money to contract from your metamask account, you have to mark the function payable.
function pay() public payable {
// msg.value is the amount of wei sent with the message to the contract.
// with this you are setting a minimum amount
require(msg.value > .01 ether);
// add your logic
}
contract Bank {
address public admin;
constructor() {
admin = msg.sender;
}
mapping (address => uint) balance;
mapping (address => bool) AccountActive;
function closeAccount() public payable{
AccountActive[msg.sender] = false;
//trasfer all the money from an account closed to the admin
payable(admin).transfer(balance[msg.sender]);
}
function viewbalance() public view returns(uint) {
return balance[msg.sender];
}}
when inserting a value before deployment, I get this error, and if I don't do it in this way the balance is 0, why? (sorry for this noob question)
This error is because you can not use an address ether balance from a smart contract. What I mean is that a smart contract cannot transfer ether from one address to another, because it would be really dangerous.
What you can do is to transfer the msg.value, which is the ether sent as the value of the transaction.
By the way, you should check for correct indentation and symbols placement. Those can lead to errors too.
The issue is here:
payable(admin).transfer(balance[msg.sender]);
You want to transfer money from the admin but the admin has no balance. So you need to send money. For this write this function:
function depositMoneyToAdmin() payable public {
// since it is payable, the money that you send would be stored in msg.value
(bool success,) = admin.call{value: msg.value}("");
// then add the owner's balance to the mapping so when u call viewBalance, you get the balance of owner
balance[admin]+=msg.value;
require(success,"Transfer failed!");
}
Avoid using transfer.
https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/
I am playing with Ethereum smart contracts and noticed some strange behavior of some transactions.
Transaction is visible and it states that tokens are sent to an address but when I check balance on Metamask it's not here (Token contract is added to MetaMask). What could be the reason of such contract behavior?
This is example transaction: https://ropsten.etherscan.io/tx/0xf3316c497fd47ee79be57be7b91cb96cc0414003a155d8ff8bc83b3a090594c9
200,000 tokens is sent to https://ropsten.etherscan.io/address/0x9DEF8C013eb590aFf09c8c76F9A74A3055fAA177#tokentxns and it's displayed on ropsten network explorer, but I can't see them or manipulate on MetaMask.
This is part of my smart contract which does transfer, I marked line with comment "invisible" which does not work for me:
function transfer(address to, uint256 value) public returns (bool) {
require(value <= _balances[msg.sender]);
require(to != address(0));
uint256 tokensToBurn = findOnePercent(value);
uint256 tokensToStack = findOnePercent(value);
uint256 tokenstoDeduct = tokensToBurn.add(tokensToStack);
uint256 tokensToTransfer = value.sub(tokenstoDeduct);
_balances[msg.sender] = _balances[msg.sender].sub(value);
_balances[to] = _balances[to].add(tokensToTransfer);
_totalSupply = _totalSupply.sub(tokenstoDeduct);
emit Transfer(msg.sender, to, tokensToTransfer); // visible on metaMask
emit Transfer(msg.sender, address(0), tokensToBurn); // burned
emit Transfer(msg.sender, address(VAULT_ADDRESS), tokensToStack); // invisible on metaMask
return true;
}
This is screenshot of address wallet: 0x9DEF8C013eb590aFf09c8c76F9A74A3055fAA177
Please advise what I could do wrong here.
[meanwhile I submitted ticket to MetaMask, maybe it's a defect on wallet extension]
You need to add the contract address to your Metamask instance.
In the account detail, click on "Add token"
Paste the conctract address to the address field in the "Custom token" tab
Review the data, click "Next", and you you can see the token balance in your wallet.