I'm working on solidity smart contract for Ethereum.
I need to run some cryptographic verification algorithm before making transaction. This algorithm is implemented in C++.
How I can call this code from inside smart contract?
Let's say I have fallowing C++ code(but in practice it is much more complex)
int foo(int a, int b, int n){
if(pow(a,b)%n == 1)
return true;
else
return false;
}
And if this code return's true I need to make transaction
msg.reciever.addr.send(amount);
Is there any solution I can use in order to combine Ethereum's smart contacts and complex self-implemented cryptography which should be done inside of blockchain? How to make a bridge between blockchain and outside library.
Generally, you cannot call any external code from an Ethereum contract. To run cryptographic verification you should use ecrecover solidity function
http://solidity.readthedocs.io/en/develop/units-and-global-variables.html#mathematical-and-cryptographic-functions
https://ethereum.stackexchange.com/questions/1777/workflow-on-signing-a-string-with-private-key-followed-by-signature-verificatio
Related
As IVotes is a interface and cannot be deployed.
IVotes public immutable token;
constructor(IVotes tokenAddress) {
token = tokenAddress;
}
....
Error: *** Deployment Failed ***
"IVotes" is an abstract contract or an interface and cannot be deployed.
Import abstractions into the '.sol' file that uses them instead of deploying them separately.
Contracts that inherit an abstraction must implement all its method signatures exactly.
A contract that only implements part of an inherited abstraction is also considered abstract.
I want to use IVotes address passing in GovernorVotes constructor.
I tried bunch of methods but none works
The Solidity snippet, that you shared, expects a contract on the tokenAddress to implement the IVotes interface.
But it needs to be a "full" contract with all function bodies - not just the function declarations.
I'm studying Uniswapv2 codes and I got stucked with ABI. https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol
Why use ABI with call method, even if we can call transfer function from interface directly?
bytes4 private constant SELECTOR = bytes4(keccak256(bytes('transfer(address,uint256)')));
.
.
.
function _safeTransfer(address token, address to, uint value) private {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'UniswapV2: TRANSFER_FAILED');
}
I've read a lot about ABI, and I'm confused about some says that it is JSON format and some says it is byte form. What is right?
You can use short form, like your example, without full ABI.
ABI for web3js is JSON form. Its used for simply call all existed functions and parameters. If you use many function and parameters -- JSON ABI much better for use.
You used short hacked version with selector - this is not good readable style of code.
1-
I believe token.call(abi.encodeWithSelector(SELECTOR, to, value)); token is another contract instance and called inside a contract. call is used to execute code of another contract
2-
The ABI encodes information about smart contracts' functions and events.
It acts as an interface between EVM-level bytecode and high-level smart
contract program code. To interact with a smart contract deployed on the
Ethereum blockchain, external programs require an ABI and the address
of the smart contract. The ABI consists of the following:
• All function names
• Input and output types of functions
• All event names and their parameters
Contract has hex representation and binary representation:
Contract.abi : This contains the ABI of the smart contract in JSON format.
Contract.bin : This contains the hex representation of binary of the smart contract code.
Assalamualaikum,
I am new to blockchain. So I was thinking of deploying a smart contract as rest api, and use it in my another smart contract.
Is it possible?
I know oracle helps to fetch data but can it help interacting two deployed contracts?
Thank in advance.
You can define an interface of the target contract in the source contract. Example:
TargetContract, deployed on the 0x123 address:
pragma solidity ^0.8;
contract TargetContract {
function foo() external pure returns (bool) {
return true;
}
}
SourceContract, pointing to the 0x123 TargetContract
pragma solidity ^0.8;
interface ITargetContract {
function foo() external returns (bool);
}
contract SourceContract {
function baz() external {
ITargetContract targetContract = ITargetContract(address(0x123));
bool returnedValue = targetContract.foo();
}
}
I have contract in solidity MasterChef.sol which can be seen at the link below
https://github.com/pancakeswap/pancake-farm/tree/master/contracts
I want to deploy it using truffle on the Binance smart chain and the question is that the constructor of the MasterChef.sol takes in 5 args and two of them are contracts I want to know that how to pass the other two contracts i.e CakeToken.sol and SyrupBar.sol as the first two args in the deploy_contracts.js file.
You need deploy other contracts beforehand, write down their addresses and pass addresses when MasterChef is deployed.
I have three smart contracts say a.sol, b.sol and c.sol... Out of these three, first two are independent smart contracts whereas c.sol uses the functions of a.sol and b.sol and thus c.sol requires to "import" the first two smart contracts. "Import" works locally but how to deploy all of them via remix/truffle on testnet such that c.sol can still access the functions of a.sol and b.sol?
Does your contract a and b supposed to be standalone contracts that will be used regardless of contract c? ie: user store data in contract a, which will be used by contract c
If so, then you can have contract a and b as variables of contract c like this
a.sol
contract A {
function doSomething() {
...
}
}
c.sol
contract C {
A a;
function setA(address addressOfContractA) {
a = A(address);
}
function makeADoSomething() {
a.doSomething();
}
}
credit: https://zupzup.org/smart-contract-interaction/
If your project was created with Truffle, you can set up c.sol in the following way:
import "./a.sol";
import "./b.sol";
contract c is a, b {
...
}
If this is the structure of your code, you will be able to deploy your Truffle project using truffle migrate (provided your migrations are set up correctly).