// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract t1{
mapping(address => uint256[]) AllSpecialNFT;
function addNewVal( uint _tokenId) public {
AllSpecialNFT[msg.sender].push(_tokenId);
}
function findSize() public view returns(uint){
return AllSpecialNFT[msg.sender].length;
}
}
pragma solidity >=0.4.22 <0.9.0;
import './t1.sol';
contract t2 {
t1 _t1;
constructor(t1 t1_){
_t1 = t1_;
}
function callandAdd(uint _tokenId) public{
_t1.addNewVal(_tokenId);
}
}
This code runs successfully, and is able to add the data in the mapping. But, this does not change the size in the T1 contract. Is there any way i can update add new elements in the mapping and update the size of the contract?
I was expecting that the size of array in mapping to be increased after calling the function callandVal().
So, i was able to update the mapping from another contract by simply adding a new parameter in the function.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract t1{
mapping(address => uint256[]) AllSpecialNFT;
function addNewVal(address _off, uint _tokenId) public {
AllSpecialNFT[_off].push(_tokenId);
}
function findSize(address _off) public view returns(uint){
return AllSpecialNFT[_off].length;
}
}
pragma solidity >=0.4.22 <0.9.0;
import './t1.sol';
contract t2 {
t1 _t1;
constructor(t1 t1_){
_t1 = t1_;
}
function callandAdd(uint _tokenId) public{
_t1.addNewVal(msg.sender,_tokenId);
}
}
Related
How can I create an erc20 token that pays it's holders a dividends monthly
Ive created a normal erc20 token but i can't seem to get this type of token
Contract can not initiate transactions by itself. But you can do something like this. In consctructor i defined 2,3 and 4 account that i have in remix, for testing, you can comment or delete it and mannually add your addresses in addParticipant();
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract DividentToken is ERC20 ("DividentToken", "DVD"), Ownable{
uint dividentAmount;
address[] participants;
constructor() {
_mint(msg.sender, 1000000000000 *10 ** 18);
addParticipant(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);
addParticipant(0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db);
addParticipant(0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB);
}
function mint(address to, uint amount) external onlyOwner {
_mint(to, amount);
}
//return list of addresses of participants
function viewParticipants() public view returns(address[] memory) {
return participants;
}
function setDividentAmount(uint _dividentAmount) public onlyOwner {
dividentAmount = _dividentAmount;
}
function addParticipant(address _participant) public onlyOwner {
participants.push(_participant);
}
function payDividents() public onlyOwner {
for(uint i = 0; i < participants.length; i++) {
transferFrom(owner(), participants[i], dividentAmount); //in
this case first you got to increase allowance for owner()
// _mint(participants[i], dividentAmount); also you can use mint
instead of transfer
}
}
}
γ»I want to create the lottery contract that user can buy some lottery numbers.
And, if the lottery was finished, I want to initialize it to create a new lottery.
That's why we implemented it this way.
lotChances = new LotChance[](0);
But, I faced this is error ...π
UnimplementedFeatureError: Copying of type struct Lottery.LotChance memory[] memory to storage not yet supported.
Minimal example:
contract Lottery {
// Lot Structs
struct LotChance {
address payable userAddress;
uint256 ids;
}
LotChance[] public lotChances;
function getResult() public onlyOwner {
luckyPerson.transfer(address(this).balance);
lotteryId++;
lotChances = new LotChance[](0);
}
}
Please advise meπ
For reset an array and set his values to default you can use delete keyword in Solidity. In your case, you must to change your getResult() function in this way:
function getResult() public onlyOwner {
luckyPerson.transfer(address(this).balance);
lotteryId++;
delete lotChances;
}
You can see an example of smart contract code, here following:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Lottery {
address owner;
constructor() {
owner = msg.sender;
}
// Lot Structs
struct LotChance {
address payable userAddress;
uint256 ids;
}
modifier onlyOwner() {
require(msg.sender == owner, "You aren't smart contract owner!");
_;
}
LotChance[] public lotChances;
function getResult(address _luckyPerson) public onlyOwner {
uint lotteryId = 0;
payable(_luckyPerson).transfer(address(this).balance);
lotteryId++;
// I reset array length about to '0'
delete lotChances;
}
function partecipateToLottery(uint _id) public {
lotChances.push(LotChance(payable(msg.sender), _id));
}
function getLengthArray() external view returns(uint) {
return lotChances.length;
}
}
pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT
contract FeeCollector {//hidden keys
address public owner;
uint256 public balance;
constructor () {
owner = msg.sender;
}
receive () payable external {
balance += msg.value; }
function withdraw (uint amount, address payable destAddr) { public
require(msg.sender ==owner, "only owner can withrdaw");
destAddr.transfer(amount);
balance -= amount;
}
Well just add } in the end. The rest of the code is working.
I keep getting this compiler error:
CompileError: openzeppelin-solidity/contracts/token/ERC20/ERC20.sol:61:41: ParserError: Expected '{' but got reserved keyword 'override'
function name() public view virtual override returns (string memory) {
^------^
Here is my contract code:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "openzeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol";
import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
contract TestToken is Standard, DetailedERC20 {
uint256 public totalSupply;
constructor(string _name, string _symbol, uint8 _decimals)
DetailedERC20(_name, _symbol, _decimals)
public
{
}
}
Here is the ERC20 contract code:
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* #dev Sets the values for {name} and {symbol}.
*
* The defaut value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor (string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* #dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* #dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
I got to work, it was making sure that the solidity compiler was up to date! My solidity compiler is version 0.8.6 and the compiler I had listed in my truffle-configuration file was 0.5.16
first, web3 version: 1.0.0-beta.36
the contract code is:
pragma solidity ^0.4.25;
contract Main {
struct Model {
uint256 key;
uint64 createTime;
}
Model[] public models;
mapping(uint256 => address) public modelOwner;
function total() view public returns (uint256) {
return models.length;
}
function getData(uint256 _tokenId) view returns ( uint256, uint64){
Model _model = models[_tokenId];
return (_model.key, _model.createTime);
}
function createData(uint256 _key, address _owner) returns (uint){
Model memory _model = Model({key : _key, createTime : uint64(now)});
uint256 newModelId = models.push(_model) - 1;
// modelOwner[newModelId] = _owner;
return newModelId;
}
}
I send transaction by remix and web3.js:
myContract.methods.createData(
key,
addressA
).send({
from: addressB
})
the problem is :
when I remove the code modelOwner[newModelId] = _owner; in function createData, web3 and remix both work (List models increase);
when I add modelOwner[newModelId] = _owner;, remix work, but web3 failed, because the result of the method getData returned is not correct (List models not increase);