Solidity: transfer function uses which from address, contract address or caller's address? - solidity

contract MarketplaceEscrow {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner {
require(owner == msg.sender, "Only the owner of the marketplace can perform this action");
_;
}
modifier onlyArbiter {
require(arbiter == msg.sender, "Only the designated marker can perform this action");
_;
}
function setProductDeliveredAndReleaseFunds(uint _productId) public onlyArbiter {
Product memory product = products[_productId];
productPurchaseHistoryForBuyer[buyer][_productId].status = eStatus.product_delivered;
Seller memory seller = product.seller;
uint priceether = product.priceEther;
uint price = priceether*10**18;
payable(seller.sellerAddress).transfer(price);
}
I tested the above function from the truffle console like this:
mp = await MarketplaceEscrow.deployed();
mp.setProductDeliveredAndReleaseFunds(24, {from: arbiter_address});
But I get an error. My question is whether the arbiter's address is being used by the code for sending funds to seller, or whether the contract address is being used. Thank you for the help.

Related

I'm trying to create a token store contract

When creating this contract, line "_buyer.transfer(buyers[_buyer]);" generates an error " "send" and "transfer" are only available for objects of type "address payable", not "address". ".
Here is the contract code.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CoinStore {
address owner;
mapping(address => uint256) buyers;
mapping(address => uint256) sellers;
uint256 coinPrice;
bool tradePaused;
constructor() {
owner = msg.sender;
}
function setCoinPrice(uint256 _price) public {
require(msg.sender == owner, "Only the owner can set the coin price.");
coinPrice = _price;
}
function registerBuyer(address _buyer, uint256 _amount) public {
require(!tradePaused, "Trading is currently paused.");
buyers[_buyer] = _amount;
}
function registerSeller(address _seller, uint256 _amount) public {
require(!tradePaused, "Trading is currently paused.");
sellers[_seller] = _amount;
}
function pauseTrade() public {
require(msg.sender == owner, "Only the owner can pause trading.");
tradePaused = true;
}
function startTrade() public {
require(msg.sender == owner, "Only the owner can start trading.");
tradePaused = false;
}
function completeSale(address _buyer, address _seller) public payable {
require(_buyer.balance >= buyers[_buyer], "Buyer does not have enough funds.");
require(_seller.balance >= sellers[_seller], "Seller does not have enough funds.");
require(msg.value == coinPrice, "Incorrect payment amount.");
_buyer.transfer(buyers[_buyer]);
_seller.transfer(sellers[_seller]);
if (msg.value > buyers[_buyer]) {
_buyer.transfer(msg.value - buyers[_buyer]);
}
}
function withdrawFunds() public {
require(msg.sender == owner, "Only the owner can withdraw funds.");
msg.sender.transfer(address(this).balance);
}
}
I am a recent learner of Solidity and am not an expert in smart contract development. Everything I could I tried. Help me please. Thank you.
The address that you are trying to send to has to be payable aswell.
Your withdraw should look like this:
function withdrawFunds() public {
require(msg.sender == owner, "Only the owner can withdraw funds.");
payable(msg.sender).transfer(address(this).balance);
}

solidity delegatecall to call issue?

My delegatecall to call flow isn't completing the transaction and has no errors to show. is it a gas issue? (local harhdat)
I have the following:
contract TestRouter {
using SafeERC20 for IERC20;
constructor() {}
function deposit2(address asset, uint256 amount, address pool_, address caller) public {
(bool success, bytes memory result) = pool_.call(abi.encodeWithSignature("deposit(address,uint256,address,uint16)",asset,amount,caller,0));
console.log("deposit2", success);
}
contract TestRoutee {
using SafeERC20 for IERC20;
address public testRouter;
constructor(address _testRouter) {
testRouter = _testRouter;
}
function deposit(address asset, uint256 _amount, address pool_) public {
IERC20(asset).safeTransferFrom(msg.sender, address(this), _amount);
IERC20(asset).approve(pool_, _amount);
(bool success, bytes memory result) = testRouter.delegatecall(abi.encodeWithSignature("deposit2(address,uint256,address,address)",asset,_amount,pool_,address(this)));
}
}
The flow is user -> TestRoutee.deposit(...) -> TestRouter.deposit2(...) -> LendingPool.sol (AAVE V2)
This ends up going to another protocol called AAVE V2
The function calling in that protocol is:
function deposit(
address asset,
uint256 amount,
address onBehalfOf,
uint16 referralCode
) external override whenNotPaused {
DataTypes.ReserveData storage reserve = _reserves[asset];
console.log("deposit asset", asset);
console.log("deposit amount", amount);
console.log("deposit onBehalfOf", onBehalfOf);
console.log("deposit referralCode", referralCode);
console.log("deposit msg.sender", msg.sender);
ValidationLogic.validateDeposit(reserve, amount);
address aToken = reserve.aTokenAddress;
reserve.updateState();
reserve.updateInterestRates(asset, aToken, amount, 0);
IERC20(asset).safeTransferFrom(msg.sender, aToken, amount);
bool isFirstDeposit = IAToken(aToken).mint(onBehalfOf, amount, reserve.liquidityIndex);
if (isFirstDeposit) {
_usersConfig[onBehalfOf].setUsingAsCollateral(reserve.id, true);
emit ReserveUsedAsCollateralEnabled(asset, onBehalfOf);
}
emit Deposit(asset, msg.sender, onBehalfOf, amount, referralCode);
}
In that protocol, I deployed it locally and have it console.log'ing msg.sender and onBehalfOf correctly, that is it is showing the address of TestRoutee, which is what I want.
Although, it only gets up to console.log("deposit after safeTransferFrom");... Which is odd.
In there, the function is gets caught up on is the _mint function:
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), 'ERC20: mint to the zero address');
console.log("IncentivizedERC20 after require");
_beforeTokenTransfer(address(0), account, amount);
uint256 oldTotalSupply = _totalSupply;
_totalSupply = oldTotalSupply.add(amount);
uint256 oldAccountBalance = _balances[account];
console.log("IncentivizedERC20 _mint oldAccountBalance", oldAccountBalance);
_balances[account] = oldAccountBalance.add(amount);
console.log("IncentivizedERC20 _mint oldAccountBalance", oldAccountBalance);
if (address(_getIncentivesController()) != address(0)) {
_getIncentivesController().handleAction(account, oldTotalSupply, oldAccountBalance);
}
}
It console.log's up until console.log("IncentivizedERC20 _mint oldAccountBalance", oldAccountBalance);. Is it running out of gas or is there anohter issue here?
There shouldn't be any storage issues with the delegatecall, which is a usual concern but i pass all of the parameters to the TestRouter contract and it calls the LendingPool successfully.

Need some help resolving smart contract warnings

New solidity programmer here.
I'm trying to create a smart contract where a user can create a Bounty. The creator sets the bounty on the smart contract in the constructor. They can subsequently choose the recipient of the funds after evaluation of some criteria. They can cancel, increase the bounty.
I tested the code out and it appears to work, but I'm getting some warnings in remix IDE that I don't know how to fix.
Could some one show me how its supposed to be done?
contract Bounty {
address payable public owner;
address payable public provider;
uint256 private bounty;
bool isActive;
event IncreaseBounty (uint256 oldBounty, uint256 newBounty);
event Paid(address owner, address payee, uint256 amount);
event Cancel(address owner, uint256 amount);
constructor() payable {
owner = payable(msg.sender);
bounty = msg.value;
isActive = true;
}
function cancel() public {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner can cancel the bounty");
uint256 bountyTemp = bounty;
bounty = 0;
owner.transfer(bountyTemp);
isActive = false;
emit Cancel(msg.sender, bountyTemp);
}
function setAndTransferToProvider(address addy) public {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner release the funds");
provider = payable(addy);
provider.transfer(bounty);
uint256 bountyUsed = bounty;
bounty = 0;
isActive = false;
emit Paid(owner, provider, bountyUsed);
}
function increaseBounty() payable external returns (uint256) {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner can increase the bounty");
uint oldBounty = bounty;
bounty += uint(msg.value);
emit IncreaseBounty(oldBounty, bounty);
return bounty;
}
function getBounty() public view returns (uint256) {
require(isActive, "contract must be active");
return bounty;
}
}
Try to put this line
provider.transfer(bounty);
right before the emit of the events on every function.
You can check this article for more understanding of reentrancy attacks.

Type is not callable _isExcludedFromFee[(owner)] = true;

My token contract with solidity 0.8.4 is catching:
TypeError: Type is not callable
_isExcludedFromFee[(owner)] = true;
^^^^^^^^
From:
constructor () {
_rOwned[_msgSender()] = _rTotal;
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F);
// Create a uniswap pair for this new token
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
.createPair(address(this), _uniswapV2Router.WETH());
// set the rest of the contract variables
uniswapV2Router = _uniswapV2Router;
_isExcludedFromFee[owner()] = true;
_isExcludedFromFee[address(this)] = true;
emit Transfer(address(0), _msgSender(), _tTotal);
initialize();
pause();
}
I believe it is "owner" that is causing the error, and will attach the Ownable and token contract below. There is an owner function that is typically included SafeMoon's Ownable that returns _owner but that just breaks everything more since I have newly declared address public owner.
Was able to correct by making these changes:
contract Ownable is Context {
// OWNER DATA
address public _owner;
address public _proposedOwner;
address private _oldOwner;
uint256 private _lockTime;
// OWNABLE EVENTS
event OwnershipTransferProposed(
address indexed currentOwner,
address indexed proposedOwner
);
event OwnershipTransferDisregarded(
address indexed oldProposedOwner
);
event OwnershipTransferred(
address indexed oldOwner,
address indexed newOwner
);
/**
* #dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* #dev Returns the address of the current owner.
*/
function owner() public view returns (address) {
return _owner;
}
/**
* #dev Returns the address of the current owner.
*/
/**
* #dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* #dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* #dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
function getUnlockTime() public view returns (uint256) {
return _lockTime;
}
//Locks the contract for owner for the amount of time provided
function lock(uint256 time) public virtual onlyOwner {
_oldOwner = _owner;
_owner = address(0);
_lockTime = block.timestamp + time;
emit OwnershipTransferred(_owner, address(0));
}
//Unlocks the contract for owner when _lockTime is exceeds
function unlock() public virtual {
require(_oldOwner == msg.sender, "You don't have permission to unlock");
require(block.timestamp > _lockTime , "Contract is locked until 7 days");
emit OwnershipTransferred(_owner, _oldOwner);
_owner = _oldOwner;
}
}

Openzeppelin ERC20 make revert

my intent is making ERC721 token can transfer by my ERC20 token only
the transfer flow is
Buyer approve ERC20 to Seller.
Seller transfer ERC721 to Buyer.
My ERC721 Token's transfer function transfer ERC20 to Seller from Buyer first, and transfer ERC721 to Buyer from Seller.
revert error occur to ERC20 transfer step.
i try to every single line delete to find revert point.
and i found that.
this is my test code
const token20 = artifacts.require("MyToken20");
const token721 = artifacts.require("MyToken721");
contract("Test", async()=>{
//...
// Buyer token20 approve to Seller
it("Token20 approve", async()=>{
var value = web3.toWei(token721Price, "ether");
await contract20.approve(seller, value, {from:buyer});
var allowed = await contract20.allowance(buyer, seller);
allowed = web3.fromWei(allowed, "ether");
assert.equal(allowed, token721Price);
});
// Seller transfer token721 to Buyer
// token20 transfer to Seller inside of function transferMy721
it("Token721 transfer", async()=>{
var allowed = await contract20.allowance(buyer, seller);
allowed = web3.fromWei(allowed, "ether");
assert.equal(allowed, token721Price);
await contract721.transferMy721(buyer, token721Id, {from:seller}); // <--- revert here
var newOwner = await contract721.ownerOf(token721Id);
assert.equal(newOwner, buyer);
});
});
and revert point in my contract is here
contract MyToken721 is ERC721Token{
string public name = "My ERC721 Token Product";
string public symbol = "MTP";
mapping(uint256 => uint256) my721TokenPrice;
MyToken20 token;
constructor(MyToken20 _token) public ERC721Token(name, symbol){
require(_token != address(0));
token = _token;
}
function mint(address _to, uint256 _tokenId, uint256 _price) public {
_mint(_to, _tokenId);
my721TokenPrice[_tokenId] = _price;
}
function transferMy721(address _to, uint256 _tokenId) public returns(bool){
require(msg.sender == ownerOf(_tokenId));
uint256 tokenPrice = my721TokenPrice[_tokenId];
if( token.transferFrom(_to, msg.sender, tokenPrice) == false ) // <--- revert here
return false;
super.approve(_to, _tokenId);
super.transferFrom(msg.sender, _to, _tokenId);
return true;
}
//...
}
and revert point in ERC20 StandardToken contract is here
contract StandardToken is ERC20, BasicToken {
mapping (address => mapping (address => uint256)) internal allowed;
function transferFrom(address _from, address _to, uint256 _value)
public returns (bool)
{
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]); // <--- revert here
require(_to != address(0));
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
emit Transfer(_from, _to, _value);
return true;
}
//...
}
as you can see, in my test code, i double check
allowed[_from][msg.sender]
please check my full code here
the one calling transferFrom is my erc721 contract.
so, i change test code
await contract20.approve(seller, value, {from:buyer});
change to
await contract20.approve(contract721.address, value, {from:buyer});
thank you for SylTi