Ethereum constructor confusion - solidity

when I deploy using Truffle, the contract constructor gets called right? and in there I can set
owner = msg.sender
Now on the client side, using web3 in javascript when I do something like:
window.ethereum.enable().then((accounts)=>{
contractInstance = new web3.eth.Contract(abi, "0x2c2D9E87eCFbCb9758df8cf063C71d3C9DBE5304", {from : accounts[1]});
console.log("contractInstance", contractInstance);
});
does this also call the constructor ? or is this a different situation

No, it does not. The constructor is called when you deploy (migrate) your contract to the blockchain.

Related

How to interact with interfaces through HardHat?

how can I interact with interfaces through hardhat.
For example in brownie you can just call function from an interface like this:
def main():
weth = interface.IWeth("weth_address")
tx = weth.deposit({"from": account, "value": "value"})
and that's it.
Is there a way to do the same in hardhat? I've been siting on this problem for couple of hours and for the life of me I can't figure out how to do this.
If it's not possible how do I get weth through solidity.
Yes, you can do it with ethers.getContractAt.
import { ethers } from "hardhat";
...
// Assume `ISetup` is an interface of the contract.
const setup = await ethers.getContractAt("ISetup", contractAddress);
In hardhat, you can achieve it using the attachment.
Go to hardhat console: npx hardhat console
Get the contract factory: factory = await ethers.getContractFactory('YourContractName')
Attach to address weth = await factory.attach('weth_address')
Do your function call e.g:
signer = await ethers.getSigner();
await signer.sendTransaction({ to: tokenAddress, value: ethers.utils.parseEther("5.0") });

How to get balanceOf of tron smart contract

I am using tronlink chrome extension and trying to call balanceOf method of a smart contract. I am very new to smart contract. Unable to find any solution. Please check my code:
let contractDetail = await window.tronWeb.trx.getContract('TG7DLMkJPYeG4QTZ8Qfgk9Mu7ePM5SQpbN');
let contract = await window.tronWeb.contract(contractDetail.abi.entrys, 'TG7DLMkJPYeG4QTZ8Qfgk9Mu7ePM5SQpbN');
balance = contract.balanceOf.call('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');
console.log(balance)
//Error: Uncaught TypeError: Cannot read property 'call' of undefined
balanceOf requires one argument.
'balanceOf(address)'
Instead of
contract.balanceOf.call('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');
You should pass in the address into balanceOf
contract.balanceOf('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS').call();
Use tronWeb.trx.getBalance(CONTRACT_ADDRESS);
Your code should look like
balance = await tronWeb.trx.getBalance('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');

How to inject the .NET Core [IServiceProvider] itself into services?

How to inject the [IServiceProvider] interface into custom services? I mean after the [Startup] class finishes construction of [IServiceProvider] from [IServiceCollection] set of bindings. How do I then subscribe to the newly created [IServiceProvider] built after method [ConfigureServices] invoked?
If you want to call your service inside startup you should create a scope
// initial database
using (var scope = app.ApplicationServices.CreateScope())
{
var initDatabase = new YourClass(scope.ServiceProvider.GetService<YourServiceInterface>());
}
But Accessing to IServiceProvider in other services don't make sense. If you describe more You could get the right answer.

Unit test service call

I'm trying to unit test code surrounding a service call - i want to test that
1. the client.retrieveDetails call is made,
2. the client.retrieveDetails call is made with the correct request and
3. the result contains an error code and an exception is thrown if it matches a specific one.
I have the following method in a class, it contains other operations as well. I need to instantiate the proxy in each method call (MyClient), so i cannot have one instance of it at class level. How can I unit test this method?
public virtual List<Detail> GetDetails(long code)
{
detail_Type[] details;
var client = new MyClient();
var context = ContextV2();
var result = client.retrieveDetails(
ref context,
IdentifierV2(code),
out details);
_exceptionGenerator.ThrowExceptionIfCodeIncorrect("" + result.resultCode, result.resultMessage);
}
List<Detail> response = Mapper.Map<detail_Type[], List<Detail>>(details);
return response;
}
the client.retrieveDetails call is made
the client.retrieveDetails call is made with the correct request and
You can create a wrapper around your service and use a mock. If you use a mock framework like moq, it is pretty easy to check for a method call.
the result contains an error code and an exception is thrown if it matches a specific one.
For this, I would create separate tests, which only tests the behaviour of the service implementation.

paypal PPAPIService new SDK

Concerning the new Paypal SDK, there is almost no useful example code, and the Internet is littered with examples of the old SDK. My question concerns making an API request for a third party paypal account for which i have obtained the token and secretToken through the permissions API.
In attempting to construct a PPAPIService object, where is the list of possible sevice names?
ie: $this->serviceName = $serviceName; (in the constructor) What is the string syntax for these?
In regards to the makeRequest method, how do I define the $apiMethod variable, and what is the format of the $params variable? What are the different parameters?
A simple example of how to just obtain the account balance of the authorized third party account would be extremely helpful.
I am using PHP.
from the PPAPIService.php file:
class PPAPIService
{
public $endpoint;
public $serviceName;
private $logger;
public function __construct($serviceName = "")
{
$this->serviceName = $serviceName; //is there ANY documentation about the syntax and types of service names?
$config = PPConfigManager::getInstance();
$this->endpoint = $config->get('service.EndPoint');
$this->logger = new PPLoggingManager(__CLASS__);
}
public function setServiceName($serviceName)
{
$this->serviceName = $serviceName;
}
public function makeRequest($apiMethod, $params, $apiUsername = null, $accessToken = null, $tokenSecret = null)
{
//what are the apiMethod types? syntax? and params? type.. options...??
}
}
Well, you do not have to create a PPAPIService object directly. Let's say you want to use the Invoicing SDK, here's what you would do
$invoiceService = new InvoiceService();
$invoiceService->setAccessToken($accessToken);
$invoiceService->setTokenSecret($tokenSecret);
$createInvoiceResponse = $invoiceService->CreateInvoice($createInvoiceRequest);
There's one service class (such as InvoiceService in this code snippet) per API that you would instantiate as per your needs. The API username/password are picked from the configuration file and the access token/token secret are set via code since they typically tend to be different for different invocations.
Which API are you trying to call, by the way?
To answer on your other concern, the new PayPal SDK's have all the required samples bundled within the SDK itself. The SDK + Samples canbe downloaded from
https://www.x.com/developers/paypal/documentation-tools/paypal-sdk-index