Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow json strings in array #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

allow json strings in array #162

wants to merge 1 commit into from

Conversation

technophile-04
Copy link
Member

@technophile-04 technophile-04 commented Oct 25, 2024

Description

So currently if we cannot pass the json objects is strings to array. Example when contract expects and string[]

For example : ['{"ford": "gray"}']

Here is the demo how it should work :

https://www.veed.io/view/87fae0c4-9463-4f8e-b017-303f37b3fefb


In SE-2:

Past in YourContract.sol :
/**
 *Submitted for verification at Arbiscan.io on 2024-02-25
 */

/**
 *Submitted for verification at sepolia-optimism.etherscan.io on 2023-12-27
 */

// SPDX-License-Identifier: MIT
// by besta.pe
pragma solidity ^0.8.19;

import "hardhat/console.sol";

contract YourContract {
	function KeyValue(
		string[] calldata key,
		string[] calldata value,
		bool newKeysOnly
	) external {
		// log the args
		console.log("Setting new greeting '%s' from %s", key[0], value[0]);

		uint count;
		Change = false;
		Replace = true;
		if (key.length != value.length) {
			revert KeyValueNotBijective();
		}
		if (newKeysOnly) {
			Replace = false;
		}
		while (count < key.length) {
			addKeyValue(key[count], value[count]);
			count++;
		}
		if (!Change) {
			revert NoChange();
		}
	}

	// TODO:
	function JSON() public view returns (string memory) {
		if (KeyCountPlus1 == 0) {
			revert NoKey();
		}
		uint count;
		string memory json = "{";
		while (count < KeyCountPlus1) {
			string memory key = keyIndex[count].key;
			uint version = keyIndex[count].version;
			string memory value = keyValue[key][version].value;
			json = string.concat(json, '"', key, '":"', value, '"');
			if (count < KeyCountPlus1 - 1) {
				json = string.concat(json, ",");
			}
			count++;
		}
		return string.concat(json, "}");
	}

	function LatestVersion(string calldata key) public view returns (uint) {
		uint count;
		if (KeyCountPlus1 == 0) {
			revert NoKey();
		}
		while (count < KeyCountPlus1) {
			if (
				keccak256(bytes(key)) == keccak256(bytes(keyIndex[count].key))
			) {
				return keyIndex[count].version;
			}
			count++;
		}
		revert NoKey();
	}

	function TotalKeys() public view returns (uint) {
		if (KeyCountPlus1 == 0) {
			revert NoKey();
		}
		return KeyCountPlus1;
	}

	function Value(
		string calldata key,
		uint version
	) public view returns (string memory) {
		string memory value = keyValue[key][version].value;
		if (keccak256(bytes(value)) == keccak256(bytes(""))) {
			revert NoValue();
		}
		return value;
	}

	function addKey(string calldata key) internal {
		KeyCountPlus1++;
		keyIndex[KeyCountPlus1 - 1] = keyMap(key, 0);
	}

	function addKeyValue(string calldata key, string calldata value) internal {
		if (keccak256(bytes(key)) == keccak256(bytes(""))) {
			revert NoKey();
		}
		if (keccak256(bytes(value)) == keccak256(bytes(""))) {
			revert NoValue();
		}
		(uint index, uint version) = newKeyTest(key);
		if (version > 0) {
			if (
				keccak256(bytes(value)) ==
				keccak256(bytes(keyValue[key][version - 1].value))
			) {
				return;
			}
			keyIndex[index].version += 1;
		}
		keyValue[key][version] = versionMap(value);
		Change = true;
	}

	function newKeyTest(string calldata key) internal returns (uint, uint) {
		uint count;
		while (count < KeyCountPlus1) {
			if (
				keccak256(bytes(key)) == keccak256(bytes(keyIndex[count].key))
			) {
				if (!Replace) {
					revert NewKeysOnly();
				}
				return (count, keyIndex[count].version + 1);
			}
			count++;
		}
		addKey(key);
		Change = true;
		return (0, 0);
	}

	mapping(uint => keyMap) private keyIndex;
	mapping(string => mapping(uint => versionMap)) private keyValue;
	struct versionMap {
		string value;
	}
	struct keyMap {
		string key;
		uint version;
	}
	error KeyValueNotBijective();
	error NewKeysOnly();
	error NoChange();
	error NoKey();
	error NotAdmin();
	error NoValue();
	bool Change;
	uint KeyCountPlus1;
	bool Replace;

	constructor() {}
}
  • Update deploy script to remove passing of first argumen.

  • yarn deploy

And now copy the address and abi in abi.ninja

Will create a PR in SE-2 so that we could test

Copy link

vercel bot commented Oct 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
abi-ninja-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 25, 2024 2:22am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant