Transaction From Ethereum Smart contract [closed] - cryptography

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
When we have a contract, we transfer currency
How is the signature done ???
For example, I send 10 ethers to the contract and the contract is divided between 5 people
What is this transaction like? Where does my private key come from to sign all 5 transactions?

I send 10 ethers to the contract and the contract is divided between 5 people
So possibly something like this:
pragma solidity ^0.8;
contract MyContract {
function redistribute(address[5] memory otherPeople) external payable {
for(uint i = 0; i < 5; i++) {
payable(otherPeople[i]).transfer(msg.value / 5);
}
}
}
You send one transaction (valued 10 ETH) to the contract, executing the redistribute() function.
The contract performs 5 internal transactions, effectively transferring 2 ETH to each of the otherPeople addresses.
In this case, you only need to use the private key of the "main" transaction sender, because the other transactions are internal txs, wrapped by the main tx.

Related

Questions about ERC-20 tokens

I know what they are but I don't quite understand the raw fundamentals of how they're expressed, recorded, and sent on the blockchain.
If tokens are just smart contracts, then how do you send them exactly? How do those token transactions get recorded on the blockchain? What are the raw fundamentals of how a token is created from a smart contract?
Token balance of an address is recorded on the token contract. A token contract is considered a contract that meets all criteria required by the ERC-20 standard, such as implementing the specified interface and emitting events when required by the standard.
Balances are mostly stored in the form of a mapping where the key is the holder address, and the value is the amount of tokens they own, because it's convenient for most cases. However the standard does not specify and particular way so if it suits your needs, you can store the balances in an array, or any other way.
Token transfer is an interaction with the token contract transfer() function (standardized in ERC-20), which should perform validations (my example skips that for simplicity), update the local variables storing balances, and emit the (again standardized) Transfer event.
Offchain apps, such as Etherscan, can listen to these events and update their own database of token holders. This allows them to filter all tokens by an address in their own database and show them on the website. But again, this is not part of the blockchain data, it's an aggregated database built on top of blockchain.
Example: Address 0x123 owns 1 USDT and 2 DAI. The USDT balance is stored in the USDT contract, and the DAI balance is stored in the DAI contract. There's no global property of the 0x123 address keeping track of its tokens.
contract USDT {
// for the key `0x123`, the value is 1 (and decimals)
mapping (address => uint256) balances;
event Transfer(address indexed from, address indexed to, uint256 amount);
function transfer(address to, uint256 amount) public {
balances[msg.sender] -= amount;
balances[to] += amount;
emit Transfer(msg.sender, to, amount);
}
}
contract DAI {
// for the key `0x123`, the value is 2 (and decimals)
mapping (address => uint256) balances;
}
For a full code example, see the OpenZeppelin implementation, and their docs related to this specific implementation. Just to clarify, OpenZeppelin is an organization that publishes open source code, but you can also build your own implementation of the standard or use another one, if that fits your use case.

I'm creating a smart contract to interact with specific NFTs. Is there a function to filter a specific NFT contract address?

I wanted to create a smart contract that only interacts with a specific NFT. I know there is a "tokenID" attribute I don't think this is unique. Cronoscan shows multiple collections that have the same tokenIDs. Does anyone know if smart contracts can filter based on a contract address? I'd like to accomplish this with as little gas as possible.
Sorry if this is a basic question but I've Googled and searched this message board for the answer but was not able to find on other that someone trying to sell their service.
I Google and search Stack Overflow but could not find an answer.
Yes, each contract will have their own set of ids and therefore they are not unique between contracts only unique for each contract.
This checks if the code size for the address is > 0. This will have to be implemented on a new contract or you will have to find an existing contract with this functionality to view/execute it
function isContract(address addressValue) public view returns (bool) {
uint size;
assembly { size := extcodesize(addressValue) }
return size > 0;
}
Also notice this is a view function and for that reason wont cost any gas to execute.
In regards to someone selling it as a service, you can get it yourself by just deploying this contract on whatever main net you want (by the sounds of it Cronos).
'// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract ContractIdentifier{
function isContract(address addressValue) public view returns (bool) {
uint size;
assembly { size := extcodesize(addressValue) }
return size > 0;
}
}

Solidity Blockchain - Memory,Storage and mapping explanation brief explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Kindly someone can explain me about mapping,storage and memory breifly with examples ? I am not clear with some articles
Thanks!!
STORAGE AND MEMORY
Storage and Memory keywords in Solidity are analogous to Computer’s hard drive and Computer’s RAM. Much like RAM, Memory in Solidity is a temporary place to store data whereas Storage holds data between function calls. The Solidity Smart Contract can use any amount of memory during the execution but once the execution stops, the Memory is completely wiped off for the next execution.
Whereas Storage on the other hand is persistent, each execution of the Smart contract has access to the data previously stored on the storage area.
Every transaction on Ethereum Virtual Machine costs us some amount of Gas. The lower the Gas consumption the better is your Solidity code. The Gas consumption of Memory is not very significant as compared to the gas consumption of Storage. Therefore, it is always better to use Memory for intermediate calculations and store the final result in Storage.
State variables and Local Variables of structs, array are always
stored in storage by default.
Function arguments are in memory.
Whenever a new instance of an array is created using the keyword
‘memory’, a new copy of that variable is created.
Changing the array value of the new instance does not affect the
original array.
Example#1: In the below example, a contract is created to demonstrate the ‘storage’ keyword.
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising array numbers
int[] public numbers;
// Function to insert values
// in the array numbers
function Numbers() public
{
numbers.push(1);
numbers.push(2);
//Creating a new instance
int[] storage myArray = numbers;
// Adding value to the
// first index of the new Instance
myArray[0] = 0;
}
}
Output:
When we retrieve the value of the array numbers in the above code, Note that the output of the array is [0,2] and not [1,2].
Example#2: In the below example, a contract is created to demonstrate the keyword ‘memory’.
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising array numbers
int[] public numbers;
// Function to insert
// values in the array
// numbers
function Numbers() public
{
numbers.push(1);
numbers.push(2);
//creating a new instance
int[] memory myArray = numbers;
// Adding value to the first
// index of the array myArray
myArray[0] = 0;
}
}
Output:
When we retrieve the value of the array numbers in the above code, Note that the output of the array is [1,2]. In this case, changing the value of myArray does not affect the value in the array numbers, this because the function stopped, the the array wasn't saved
MAPPINGS
Mappings are a totally different thing.
These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types but reference types are not allowed while the value can be of any type.
Mappings are mostly (but not limited to) used to associate the unique Ethereum address with the associated value type.
The mappings are quite similar to an array
mapping(key => value) <name>;
Example#1:
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising mapping of user balance
mapping(address => uint) balance;
// Function to insert user balance
function Insert(address _user, uint _amount) public
{
//insert the amount to a specific user
balance[_user] = _amount
}
//function to view the balance
function View(address _user) public view returns(uint)
{
//see the value inside the mapping, it will return the balance of _user
return balance[_user];
}
}

What does immutability mean in the definition of the nCopies API in JAVA ?What is an use case for this API? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I came across a nCopies code somewhere and not being familiar with this funciton, I looked up the Oracle Docs and noticed that the definition says :
Returns an immutable list consisting of n copies of the specified object.
What does immutable to mean here ?I thought immutable meant that it cannot be modified. However, I notice that I am able to modify it . Also, I am able to reassign some other list to it.
This is the sample code I wrote to check the behavior .
public static void main(String[] args){
List<Integer> list = new ArrayList<>(Collections.nCopies(5,0));
List<Integer> list2 = new ArrayList<>();
list2.add(2);list2.add(3);
for(int i=0;i<list.size();i++){
int prod = i*2;
list.set(i, prod); // list is modifiable
}
print(list);
list = list2; // list is assignable
print(list);
}
What is an use case of this API ?
Try List<Integer> list = Collections.nCopies(5,0); - your version passes the immutable collection to the ArrayList(Collection<? extends E> c) constructor, which copies it. The copy is mutable.

Best pratice when choosing between objects [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
So i am creating a program that collects alot of different data from the database and creats / updates several charts for the end user to see.
Now at some level all of the data follows the same pattern:
Every data has a date attached to it this date is used to display the X Cordinate on the chart
Every data has a so called queue (which is simply a name)**
Now what i have done so far is to create a super class (abstract class). My idea was to create individual sub classes of this super class and allow them to have their own implementation and fields.
Now to my question some of these objects will be relativly small for example i have an object that only consists of three fields with getter and setter. Is best pratice to devide and conquere or have as few objects as possible?
The alternative to having small objects is that a larger object that in short are talking the same type of object but half of them has a field that the other half does not I.E why i wanted to split it into two objects to avoid having a field that will be null 50% of the times.
Here is an example of how my objects look:
Example on subclass
class Callback : ContactQueue
{
public int completedCallbacks{get; set;}
public int completed_within_timeframe{get; set;}
public int answerPercentage { get; set; }
public override String type {get; set;}
public override DateTime period { get; set; }
public Callback(String type,DateTime period)
{
this.type = type;
this.period = period;
}
public override String toString()
{
return type;
}
public double percentageCompleted {
get
{
return completed_within_timeframe / completedCallbacks * 100; // todo
}
}
}
I hope you can understand my question if not please leave a comment and i will respond as soon as possible
It really depends on your system. If you want to have a storage for your fields then you can have one object with many getters/setters.
But I would recommend splitting them by behaviour. You might want to add methods to your objects and there will be differences in behaviour you'll want to have. And at this point if you had gone with the first way, you'll have to make a lot of checks inside these methods to correctly execute it. You need to separate objects to scale easier.