There is a contract where we have artists and anybody can buy an artist, then he becomes the owner of this artist. Also, I have owners
array and bulkSend
function that sends ETH to all addresses of the owners
array.
pragma solidity >=0.4.22 <0.7.0;
contract Cinema {
struct Artist {
string name;
uint price;
address owner;
}
Artist public artist;
mapping (uint => Artist) public artists;
address payable private manager;
address payable[] public owners;
uint numArtists;
constructor() public {
manager = msg.sender;
}
function bulkSend(uint256 amount) public payable {
uint256 share = amount / owners.length;
uint256 length = owners.length;
uint256 currentSum = 0;
for (uint256 i = 0; i < length; i++) {
require(share > 0);
currentSum += share;
require(currentSum <= msg.value);
owners[i].transfer(share);
}
}
function addArtist(string memory name, uint price, address payable owner) public returns(uint artistID) {
artistID = numArtists++; // artistID is return variable
artists[artistID] = Artist(name, price, owner);
owners.push(owner);
}
function contribute(uint artistID) public payable {
Artist storage c = artists[artistID];
require(msg.value == c.price, "Wrong amount");
manager.transfer(msg.value / 10); // Send 10% to the manager
bulkSend(msg.value * 9 / 10); // Send 90% to owners
c.owner = msg.sender; // Set contributor as owner of the Artist
delete owners[c.index];
owners.push(msg.sender); // Add contributor to the owners array
}
}
The question is can I delete an element in the array and somehow past a new element at this position (.push
doesn't allow to do this)? It's important to keep the order of the elements.