payments ( pre set value) in solidity - solidity

i am developing a Movie renting smartcontract. Where the owner can add new movies, clients can search movies and pay for the movies they select.Adding and searching is working to my liking.
problem: i want to develop the pay function as such- where it takes one argument( the title of the movie) and clients has to pay the exact amount set by the owner, he can not pay less then the price.
for example: owner add a movie: title-titanic,price-10 eth. when client use the pay function he put the title titanic and pay 10 eth. if he tries to pay less or more the transaction will not be successful.
here is the code
pragma solidity 0.8.7;
// SPDX-License-Identifier: MIT
contract Movie{
address public owner;
struct Move{
uint year;
uint price;
}
mapping (string => Move ) public movieInfo;
mapping(uint => Move) amount;
constructor() payable{
owner= msg.sender;
}
function addMovie(string memory _title, uint _year, uint _price) public {
require( msg.sender==owner);
movieInfo[_title]= Move(_year, _price);
}
function pay(string memory _title) public payable{
}
function totalFunds() public view returns(uint){
return address(this).balance;
}
}

Don't think that this kind of stuff is good to store into the blockchain ATM, due to the highly cost of storing.
In case you would like to have it:
mapping(string -> address[]) paidUsers;
function pay(string memory _title) public payable {
require(msg.value == movieInfo[_title].price, "Invalid price for the film");
paidUsers[_title].push(msg.sender);
}

Related

attempting to understand gas limits, and why I'm getting infinite gas and extremely high gas fees for the constructor function

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract Coin {
address public minter;
mapping(address => uint) public balances;
event Sent(address from, address to, uint amount);
constructor() {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
error InsufficientBalance(uint requested, uint available);
function send(address receiver, uint amount) public {
if (amount > balances[msg.sender])
revert InsufficientBalance({
requested: amount,
available: balances[msg.sender]
});
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
While scrolling and attempting to understand ALL aspects of solidity, I can't understand gas fees. Why they say infinite and why it says the "constructor" function is so high.
Remix IDE contains an estimator that is still in active development (February 2023), and it's not perfect yet.
In some cases it's simply not able to estimate the amount of gas that the function is going to consume, and then it shows Infinity or undefined.
For example, it can currently estimate simple assigning to the value, but it's not yet able to estimate increasing the value:
But it doesn't mean that the actual consumption is going to be infinite.

Struct within another struct Solidity

I just started learning solidity and I am working on a bidding contract that allows bidders to bid on a campaign.
I have a struct for campaigns. Bidders have details (address, name), I want to store bidders with their information inside of the campaign. There can be more than one bidder for a campaign
This is my Campaign and Bidder struct
struct Campaign {
uint256 campaignID;
uint256 budget;
uint256 bidCount;
}
struct Bidder {
bool bided;
uint256 bid;
string name;
address bidderAddress;
}
mapping(address => Bidder) public bidders;
Campaign[] public campaigns;
I wrote down a bid function here that takes the index of campaign and bid then populate bidCount.
function bid(uint256 _bidIndex, uint256 _twitterID) public {
require(!bidders[msg.sender].bided);
bidders[msg.sender].bid = _bidIndex;
campaigns[_bidIndex].bidCount += 1;
totalBids += 1;
}
So the Campaign can look something like this (if this is possible)
0: campaignID 1
1: budget 2ETH
2: bidCount 3
3: Bidder {0: name Bidder1, 1: address 0xahaaahha}
{0: name Bidder2, 2: address 0x2334jddd}
Any help will be greatly appreciated. Thanks
According to me, in this case you can use nested mapping into Campaign struct for 'connect' different bids to a single campaign. I created a smart contract ad hoc for your case, you can see it in the following lines:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Auction {
address owner;
constructor() {
owner = msg.sender;
}
struct Campaign {
uint256 campaignID;
uint256 budget;
uint256 bidCount;
mapping(uint => Bidder[]) bidders;
}
struct Bidder {
bool bided;
uint256 bid;
string name;
address bidderAddress;
}
Campaign[] public campaigns;
uint totalCampaign = 0;
modifier onlyOwner {
require(msg.sender == owner, "Error! You're not the smart contract owner!");
_;
}
// Create campaigns
function createCampaing(uint _budgetCampaign) public onlyOwner {
Campaign storage _firstCampaigns = campaigns.push();
_firstCampaigns.campaignID = totalCampaign;
_firstCampaigns.budget = _budgetCampaign;
totalCampaign++;
}
// Bid
function bid(uint _indexCampaign, string memory _nameBidder) public {
Campaign storage _bidCampaign = campaigns[_indexCampaign];
_bidCampaign.bidCount += 1;
uint _bidIndex = _bidCampaign.bidCount;
_bidCampaign.bidders[_indexCampaign].push(Bidder(true, _bidIndex, _nameBidder, msg.sender));
}
// Getter Bids
function getBids(uint _indexCampaign) onlyOwner external view returns(Bidder[] memory) {
return campaigns[_indexCampaign].bidders[_indexCampaign];
}
}
With getBids() function, you retrieve all bids for a specific campaign that it identify with its index. I put a onlyOwner modifier that it allows to specific function that they'll call only from the smart contract owner (rather who deployed for first time smart contract into blockchain).

Solidity Function That Restricts The Public To Mint Just One ERC721 Token (NFT) With Mint Price of Zero?

I would like to to request some help in developing a mint function that restricts a public user to mint only ONE NFT with a mint price of 0 (excluding gas fees) - in the case of a promotional giveaway. Is this even possible? I would welcome any suggestions, even a supplementary centralised solution..
Here is my function so far. At the moment, the only way I can restrict the number of free minting NFT is if the owner of the contract executes the minting. But I would like the public user to execute this function, escpecially if the number of free NFTs is a lot and hence associated gas fee. Its based on the OpenZeppelin Contracts:
contract MyTestContract is ERC721, ERC721Enumerable, Ownable {
bool public preLaunchActive = false;
uint256 public maxGiveAway = 3;
function myPreLaunchGiveAway(uint amount, address to) public onlyOwner {
require(preLaunchActive, "preLaunchActive state is not active.");
require(amount <= maxGiveAway, "No more available.");
require(amount > 0, "Amount must be greater than 0.");
for (uint i = 0; i < amount; i++) {
uint256 tokenId = totalSupply();
if (tokenId < maxGiveAway) {
_safeMint(to, tokenId);
}
}
maxGiveAway = maxGiveAway.sub(amount);
}
}
require(balanceOf(msg.sender) <= maxGiveAway,"No more available.!");
Will let user mint allowed number of max give away!

How to make an API call in solidity?

I have a smart contract that I’m trying to make, it pays out the winners of my League of Legends tournament. However I’m running into an issue. I need to make an API call to get the winner of the match, I have a simple URL that I’ve make.
"example-winner.com/winner"
And it returns simple JSON with the address of the winner:
{"winner":"0xa7D0......."}
However, I’m not sure how to make the API call to the outside function. I know I need to use some sort of oracle technology.
Any thoughts? Below is my code:
pragma solidity ^0.4.24;
contract LeagueWinners{
address public manager;
address[] public players;
uint256 MINIMUM = 1000000000000000;
constructor() public{
manager = msg.sender;
}
function enter() public payable{
assert(msg.value > MINIMUM);
players.push(msg.sender);
}
function getWinner() public{
assert(msg.sender == manager);
// TODO
// Get the winner from the API call
result = 0; // the result of the API call
players[result].transfer(address(this).balance);
// returns an adress object
// all units of transfer are in wei
players = new address[](0);
// this empties the dynamic array
}
}
You can use Chainlink as your Oracle.
As many have mentioned, you will need an oracle to get your API call. Something that is important to note, your contract is actually asking an oracle to make your API call for you, and not making the API call itself. This is because the blockchain is deterministic. For more information see this thread.
To answer your question, you can use the decentralized oracle service Chainlink.
You'd add a function:
function getWinner()
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
req.add("get", "example-winner.com/winner");
req.add("path", "winner");
sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
}
For the purpose of the following exmaple, we are going to pretend you want to return a uint256 instead of an address. You can return a bytes32 and then convert it to an address, but for simplicity let's say the API returns the index of the winner. You'll have to find a node and jobId that can make a http.get request and return a uint256 object. You can find nodes and jobs from market.link. Each testnet (Ropsten, Mainnet, Kovan, etc) has different node addresses, so make sure you pick the right ones.
For this demo, we are going to use LinkPool's ropsten node
address ORACLE=0x83F00b902cbf06E316C95F51cbEeD9D2572a349a;
bytes32 JOB= "c179a8180e034cf5a341488406c32827";
Ideally, you'd choose a number of nodes to run your job, to make it trustless and decentralized. You can read here for more information on precoordinators and aggregating data. disclosure I am the author of that blog
Your full contract would look like:
pragma solidity ^0.6.0;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol";
contract GetData is ChainlinkClient {
uint256 indexOfWinner;
address public manager;
address payable[] public players;
uint256 MINIMUM = 1000000000000000;
// The address of an oracle
address ORACLE=0x83F00b902cbf06E316C95F51cbEeD9D2572a349a;
//bytes32 JOB= "93fedd3377a54d8dac6b4ceadd78ac34";
bytes32 JOB= "c179a8180e034cf5a341488406c32827";
uint256 ORACLE_PAYMENT = 1 * LINK;
constructor() public {
setPublicChainlinkToken();
manager = msg.sender;
}
function getWinnerAddress()
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
req.add("get", "example-winner.com/winner");
req.add("path", "winner");
sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
}
// When the URL finishes, the response is routed to this function
function fulfill(bytes32 _requestId, uint256 _index)
public
recordChainlinkFulfillment(_requestId)
{
indexOfWinner = _index;
assert(msg.sender == manager);
players[indexOfWinner].transfer(address(this).balance);
players = new address payable[](0);
}
function enter() public payable{
assert(msg.value > MINIMUM);
players.push(msg.sender);
}
modifier onlyOwner() {
require(msg.sender == manager);
_;
}
// Allows the owner to withdraw their LINK on this contract
function withdrawLink() external onlyOwner() {
LinkTokenInterface _link = LinkTokenInterface(chainlinkTokenAddress());
require(_link.transfer(msg.sender, _link.balanceOf(address(this))), "Unable to transfer");
}
}
This would do about everything you need.
If you can't adjust the API to return a uint, you can return a bytes32 and then convert it to an address or a string.
function bytes32ToStr(bytes32 _bytes32) public pure returns (string memory) {
bytes memory bytesArray = new bytes(32);
for (uint256 i; i < 32; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}
You cannot. The vm does not have any I/O outside of the blockchain itself. Instead you will need to tell your smart contract who the winner is and then the smart contract can just read the value of that variable.
This design pattern is also known as the "oracle". Google "Ethereum oracle" for more info.
Basically your web server can call your smart contract. Your smart contract cannot call your web server. If you need your smart contract to access a 3rd party service then your web server will need to make the request then forward the result to solidity by calling a function in your smart contract.
You didn't properly explain what you are trying to do. Are you having trouble with the solidity code? or rather with your server? Here is an edited version. See if it helps.
pragma solidity ^0.4.24;
contract LeagueWinners{
address public manager;
//address[] public players;
uint256 MINIMUM = 1000000000000000;
constructor() public{
manager = msg.sender;
}
struct Player {
address playerAddress;
uint score;
}
Player[] public players;
// i prefer passing arguments this way
function enter(uint value) public payable{
assert(msg.value > MINIMUM);
players.push(Player(msg.sender, value));
}
//call this to get the address of winner
function winningPlayer() public view
returns (address winner)
{
uint winningScore = 0;
for (uint p = 0; p < players.length; p++) {
if (players[p].score > winningScore) {
winningScore = players[p].score;
winner = players[p].playerAddress;
}
}
}
// call this to transfer fund
function getWinner() public{
require(msg.sender == manager, "Only a manager is allowed to perform this operation");
// TODO
address winner = winningPlayer();
// Get the winner from the API call
//uint result = 0; // the result of the API call
winner.transfer(address(this).balance);
// returns an adress object
// all units of transfer are in wei
delete players;
// this empties the dynamic array
}
}
At least that is what I understand by your question.

How to transfer funds from smart contract to account OTHER than msg.sender?

I am trying to transfer part of funds stored in a smart contract to an account other than the one calling the function (msg.sender). My function is pretty much like this:
function getFunds(address addr, uint amount) public {
require (address.this(balance)>= amount);
addr.transfer(amount);
}
What I get when I compile in Truffle is:
Member "transfer" not found or not visible after argument-dependent lookup in address.
As if it is looking for members in a struct.
Thanks for any help.
Since solidity 0.5 addresses should be payable in order to transfer eth to them
function getFunds(address payable addr, uint amount) public {
require (address.this(balance)>= amount);
addr.transfer(amount);
}
For mapping of a struct you can use like this
contract testContract {
mapping(uint256 => test) testAddressMapping;
struct test {
address payable testAddress;
}
function testFunction() external{
testAddressMapping[0].testAddress.transfer(100);
}
}