Retrieve mapping data from one contract to another contract in solidity - blockchain

Here, I'm trying to get tempMap data from MainContract in Temp contract but I'm not able to get value form tempMap, as well as I'm using external visibility in MainContract but still didn’t get any response.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract MainContract {
mapping(uint => string) public tempMap;
function addData(uint number,string memory name ) public {
tempMap[number] = name;
}
function get(uint number) external view returns(string memory){
return tempMap[number];
}
}
contract Temp{
MainContract main = new MainContract();
function getData(uint number) public view returns(string memory){
return main.get(number);
}
}

You have to pass the address of the deployed contract, so it knows where to make the call.
Your temp contract will look something like this.
contract Temp{
MainContract main;
constructor(MainContract _main) {
main = _main;
}
function getData(uint number) public view returns(string memory){
return main.get(number);
}
}

Related

how to get the address of a winner in lottery decentarlized lottery application

I created a lottery application in solidity. All things are working fine but I want to add this feature that when the winner is selected it should return me the address of winner as well.
What can i do for this? Any help will be appreciated, thanks. Here is my solidity code.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Lottery_Application {
address public manager;
address payable[] public participants;
constructor() {
manager=msg.sender;
}
receive() external payable {
require(msg.value>=1 ether);
participants.push(payable(msg.sender));
}
function getbalance() public view returns(uint) {
require(msg.sender==manager);
return address(this).balance;
}
function random() public view returns(uint) {
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,participants.length)));
}
function selectwinner() public returns(address) {
require(msg.sender==manager);
require(participants.length>=3);
uint r=random();
address payable winner;
uint index=r% participants.length;
winner=participants[index];
winner.transfer(getbalance());
winner=address(msg.sender);
return winner;
}
}
State changing functions are unable to return anything. You could do this two ways.
Create and use events.
event WinnerSelected(address winner)
At the end of your winnerSelect function,
emit WinnerSelected(winner).
Create a global variable to store recent winner.
address public recentWinner
At the end of your winnerSelect function,
recentWinner = winner.

solidity: how can i call a payable function from another contract and enter the amount i want to pay? the other contract is not on the editor

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
interface IBank {
function addTax(uint256 moneyToAdd) external payable;
}
contract Counter {
uint public count;
address bankAddress;
function setbankAddress(address _bankAddress) public payable {
bankAddress = _bankAddress;
}
function addMon(uint moneyToAdd) public payable {
require(moneyToAdd <= msg.value, "Ether value sent is not
correct");
}
function pay(uint _Amount) public payable {
IBank(bankAddress).addTax{value:_Amount}(_Amount);
}
}
when I deploy it I first add the contract address that I want to interact with, it's working with all functions that do not require value

Figuring out a solidity Interface issue

I got an example of a solidity Interface.
1/ Any clue if this method is accurate as it's implementing the Interface within the inherited from Contract and not within the extension Contract.
2/ I tried implementing it, contractA functions run correctly, however contractB getCount function is not running correctly.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface InterfaceA {
function count() external view returns (uint256);
function increment() external;
}
contract contractA {
uint256 number = 0;
function count() external view returns (uint256) {
return number;
}
function increment() external {
number++ ;
}
}
// SPDX-License-Identifier: MIT
import './contractA.sol' ;
pragma solidity ^0.8.0;
contract contractB {
address addressA;
function setPointer(address _addressA) external {
addressA = _addressA;
}
function getCount() external view returns (uint256) {
InterfaceA b = InterfaceA(addressA);
b.count();
}
function addToIncrement() external {
InterfaceA b = InterfaceA(addressA);
b.increment();
}
}
I think the only issue is you are not returning anything from getCount. You added the return signature, you should had a warning when you compiled it.
function getCount() external view returns (uint256) {
InterfaceA b = InterfaceA(addressA);
return b.count();
}

Create a global Addressbook for deployed Smart Contracts

I want to create a smart contract (called Deal) which, after it gets deployed, It calls another smart contract (called AddressBook) to register his address on the global AddressBook smart contract. The code I am using works, but unfortunately my newly deployed smart contract is not using the address from the AddressBook smart contract, but instead it uses his own address to call the addAddress function. The red marking shows the output if I am calling addressbook.getAddresses(addresses) inside the Deal contract. The blue marking shows the output if I am calling the getAddresses function within the AddressBook contract. So what I want is the blue output within the Deal contract
Image of the Output
Can anybody tell me how I can add the addesses of my deployed contract to a global AddressBook contract?
This his how my AddressBook smart contract looks like. I would like to add all deployed smart contracts at this contact.
contract AddressBook {
mapping(address => address[]) private _addresses;
mapping(address => mapping(address => string)) private _aliases;
function getAddresses() public view returns (address[]) {
return _addresses[msg.sender];
}
function addAddress(address addr, string _alias) public payable {
_addresses[msg.sender].push(addr);
_aliases[msg.sender][addr] = _alias;
}
function removeAddress(address addr) public payable {
uint length = _addresses[msg.sender].length;
for(uint i = 0; i < length; i++) {
if (addr == _addresses[msg.sender][i]) {
if (1 < _addresses[msg.sender].length && i < length-1) {
_addresses[msg.sender][i] = _addresses[msg.sender][length-1];
}
delete _addresses[msg.sender][length-1];
_addresses[msg.sender].length--;
delete _aliases[msg.sender][addr];
break;
}
}
}
function getAlias(address addr) public view returns (string) {
return _aliases[msg.sender][addr];
}
}
This is the smart contract I want to deploy and register at the global
AddressBook smart contract:
contract Deal {
AddressBook addressBook;
address public owner;
address public buyerAddr;
address private addr;
string private metaData;
uint private duration;
uint private price;
string private typeOfData;
string private gateKeeper;
event SafepaySent(address buyer, uint value, uint now);
/// The smart contract's constructor
function Deal(address _addressBook) public payable {
/// The seller is the contract's owner
owner = msg.sender;
AddressBook a = AddressBook(_addressBook);
a.addAddress(owner,"test");
}
}
function getAddresses(address _addressBook) public view returns (address[]) {
return AddressBook(_addressBook).getAddresses();
}
}
contract AddressBook {
function getAddresses() public view returns (address[]);
function addAddress(address addr, string _alias) public payable ;
function removeAddress(address addr) public payable;
function getAlias(address addr) public view returns (string);
}
Now I want to call the AddressBook.getAddresses() function within the AddressBook smart contract and I want to receive the addresses of all deployed Deal contracts. However, I am not receiving any address within the AddressBook smart contract. If I am calling the function Deal.getAddresses("address of the global Addressbook") I get the address of the newly deployed smart contract. But this is not what I want.
First if you want to use AddressBook as a global smart contract to track and load all the Deal contracts. You should already deploy the AddressBook contract and save it's address. Then you can load the AddressBook contract inside the constructor of the Deal contract and add the address of current Deal contract which is going to deploy, inside the already deployed global AddressBook contract. I did some working on the given contracts. I am using an array of addresses inside the AddressBook contract to track the address of all the Deal contracts. The following example code can give you good idea.
//Address Book Contract
pragma solidity ^0.5.1;
contract AddressBook {
//mapping(address => address[]) private _addresses;
address[] private _addresses;
mapping(address => mapping(address => string)) private _aliases;
function getAddresses() public view returns (address [] memory) {
return _addresses;
}
function addAddress(address addr, string memory _alias) public payable {
_addresses.push(addr);
_aliases[msg.sender][addr] = _alias;
}
}
I just focused on _address here. Following is the deal contract:
pragma solidity ^0.5.1;
import "./AddressBook.sol";
contract Deal {
AddressBook addressBook;
address public owner;
address public buyerAddr;
address private addr;
string private metaData;
uint private duration;
uint private price;
string private typeOfData;
string private gateKeeper;
event SafepaySent(address buyer, uint value, uint now);
/// The smart contract's constructor
constructor(address _addressBook) public payable {
/// The seller is the contract's owner
owner = msg.sender;
addressBook = AddressBook(_addressBook);
addressBook.addAddress(address(this),"test");
}
function getAddresses() public view returns (address[] memory) {
return addressBook.getAddresses();
}
}
You can also test and run this in remix. Hope it works.

How to Call contract inside another contarct in solidity version 0.5.2?

I'm using solidity version 0.5.2
pragma solidity ^0.5.2;
contract CampaignFactory{
address[] public deployedCampaigns;
function createCampaign(uint minimum) public{
address newCampaign = new Campaign(minimum,msg.sender); //Error
//here!!!
deployedCampaigns.push(newCampaign);
}
function getDeployedCampaigns() public view returns(address[] memory){
return deployedCampaigns;
}
}
I'm getting the error while assigning calling the Campaign contract inside CampaignFactory contract
TypeError: Type contract Campaign is not implicitly convertible to expected
type address.
address newCampaign = new Campaign(minimum,msg.sender);
I have another contract called Campaign which i want to access inside CampaignFactory.
contract Campaign{
//some variable declarations and some codes here......
and I have the constructor as below
constructor (uint minimum,address creator) public{
manager=creator;
minimumContribution=minimum;
}
You can just cast it:
address newCampaign = address(new Campaign(minimum,msg.sender));
Or better yet, stop using address and use the more specific type Campaign:
pragma solidity ^0.5.2;
contract CampaignFactory{
Campaign[] public deployedCampaigns;
function createCampaign(uint minimum) public {
Campaign newCampaign = new Campaign(minimum, msg.sender);
deployedCampaigns.push(newCampaign);
}
function getDeployedCampaigns() public view returns(Campaign[] memory) {
return deployedCampaigns;
}
}
To call an existing contract from another contract ,pass the contract address inside cast
pragma solidity ^0.5.1;
contract D {
uint x;
constructor (uint a) public {
x = a;
}
function getX() public view returns(uint a)
{
return x;
}
}
contract C {
//DAddress : is the exsiting contract instance address after deployment
function getValue(address DAddress) public view returns(uint a){
D d =D(DAddress);
a=d.getX();
}
}