Skip to content

Instantly share code, notes, and snippets.

@Josiassejod1
Created December 30, 2021 06:51
Show Gist options
  • Select an option

  • Save Josiassejod1/54f7969d4bf80b3fd8732bbd2f5ef7a4 to your computer and use it in GitHub Desktop.

Select an option

Save Josiassejod1/54f7969d4bf80b3fd8732bbd2f5ef7a4 to your computer and use it in GitHub Desktop.
Saving Address to Smart Contract
pragma solidity 0.6.6;
pragma experimental ABIEncoderV2;
// All thought this is highly convoluted, this was intented to save the names of hereos and their attributes
// from my marvel vs capcom 2 api https://rapidapi.com/Josiassejod1-WNU9Fi2D8/api/marvel-vs-capcom-2/details
contract CharacterStorage {
mapping(address => Character[]) characters;
struct Character {
string name;
string headshot;
}
Character character;
function setCharacterData(string memory _name, string memory _headshot) public{
characters[msg.sender].push(Character({
name: _name
headshot: _headshot
}));
}
function getCharacterData(string memory _name) public returns(Character memory) {
address _address = msg.sender;
uint count = characters[_address].length;
Character[] memory userCharacters = characters[_address];
for (uint i = 0; i < count; i++) {
if (keccak256(abi.encodePacked(userCharacters[i].name)) == keccak256(abi.encodePacked(_name))){
character = userCharacters[i];
break;
}
}
return character;
}
function getAllCharacters() public view returns(Character[] memory) {
address _address = msg.sender;
Character[] memory userCharacters = characters[_address];
return userCharacters;
}
function toBytes(address a) public pure returns (bytes memory) {
return abi.encodePacked(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment