Truffle/Ganache: is there a way to set a deployment address? - ethereum

I'm currently developing a dapp in Solidity and want to frequently test it locally along with the updates - so I don't really want to redeploy it everytime to a test net.
However, everytime I deploy it, the address the smart contract is deployed to changes, so I have to update my front-end code to the new address.
Is there a way to "force" the smart contract to always be deployed at the same address? Or is there any other equivalent solution you might think of?
Thanks!

Faced the same problem. I don't know whether it legal but you can do:
in your migration file (migrations/1_example_migration.js)
var MyContract = artifacts.require("MyContract");
module.exports = function(deployer) {
console.log(deployer);
console.log(arguments);
let n = 5; // it can be any address from list of available
deployer.deploy(MyContract, {from: arguments[2][n]});
};
Documentation

you can get the account list you have by passing in the accounts argument like follows In the deploy_migration files :
module.exports = function(deployer, network, accounts) {
// Use the accounts within your migrations.
}

Related

Using Google Secret Manager with Firebase Functions and Stripe (top level)

Gday,
Quite new to TS and only got a little bit of experience with backend in general, so apologies if I am completely missing the idea.
So I really like the idea of using Google Secret Manager instead of custom env config for Firebase Functions.
Below is all done within a config.ts within my functions dir.
I need to initialize stripe. Traditional tutorials say to do something like the below example:
import Stripe from 'stripe';
export const stripeSecret = functions.config().stripe.secret;
const stripe = new Stripe(stripeSecret, {apiVersion: '2020-08-27'});
I think it would be ideal to change it to this:
import Stripe from 'stripe'
import {SecretManagerServiceClient} from '#google-cloud/secret-manager';
export async function getSecret(name:string){
const [version] = await secrets.accessSecretVersion({
name: `projects/example/secrets/${name}/versions/latest`,
});
return version.payload?.data?.toString();
}
export const stripeSecret = await getSecret("Stripe-API")
const stripe = new Stripe(stripeSecret, {apiVersion: '2020-08-27'});
But this causes an issue with it being async/await on the top level.
What are my options here and what is best practice?
The Google Secret manager is a useful utility and below are a few best practices when using Secret Manager.
You may try and use following to create and use a secret:
From the root of your local project directory, run the following command:
firebase functions:secrets:set SECRET_NAME
Enter a value for SECRET_NAME.
The CLI echoes a success message and warns that you must deploy functions
for the change to take effect.
Before deploying, make sure your functions code allows the function to access the secret using the runWith parameter:
exports.processPayment = functions
// Make the secret available to this function
.runWith({ secrets: ["SECRET_NAME"] })
.onCall((data, context) => {
const myBillingService = initializeBillingService(
// reference the secret value
process.env.SECRET_NAME);
// Process the payment});
Deploy Cloud Functions:
firebase deploy --only functions
You can also access it like any other environment variable. Conversely, if another function that does not specify the secret in runWith tries to access the secret, it receives an undefined value:
To see the complete best practices list, please visit the next link [1] and to know more about managing secret versions, please take a look at the next link [2].
[1] https://cloud.google.com/secret-manager/docs/best-practices
[2] https://cloud.google.com/secret-manager/docs/managing-secret-versions

how to deal with contract versions and test smart contracts for upgrade compatibility

I try to use OpenZeppelin SDK to make my contracts upgradeable using the SimpleProxy approach. I need to test my contracts for upgrade compatibility (e.g prevent mistakes about unstructured storage during upgrading the contracts to a new version).
According to OpenZeppelin docs, I can write these types of tests by something like this
const { deployProxy, upgradeProxy } = require('#openzeppelin/truffle-upgrades');
const Box = artifacts.require('Box');
const BoxV2 = artifacts.require('BoxV2');
describe('upgrades', () => {
it('works', async () => {
const box = await deployProxy(Box, [42]);
const box2 = await upgradeProxy(box.address, BoxV2);
const value = await box2.value();
assert.equal(value.toString(), '42');
});
});
But in the above code, I must import both versions and this means I need to add the version number to the end of the contract's name and create a copy whenever I want to upgrade the contract. But what if I want to only modify the old contract and use a version control system like git?
If this is not possible, how to deal with these versions and files? when I want to upgrade a contract, need to update all of the imports and parent contracts for every child contract? If I want to put the latest version of each contract in a separate directory, do I need to update all import paths whenever upgrading a contract?
Sorry for my bad English.

What is class in feathersjs and how to implement

Am new to feathersjs am try to do local auth in feathers while creating the service under users.class.js file where there. i don't know what to implement there so kindly guide me with this
const { Service } = require('feathers-mongoose');
exports.Users = class Users extends Service {
create(Users, email, username, password, role ) {
Users.created_at = new Date();
return super.create(Users, email, username, password, role );
}
update(id, Users, users) {
Users.updated_at = new Date();
return super.update(id, Users, users);
}
};
I’d look at the docs to learn more about services.
In Feathers, when you generate a service through the CLI, it creates three files.
service-name.class.js/.ts contains your service definition. Feathers operates everything in a CRUD fashion. So, you’ll see all the CRUD methods here. You can either define the service yourself, or use CLI to define it for you using an adaptor like Mongoose or Sequelize.
service-name.hooks.js/.ts contains all the hooks associated with this service. Feathers likes to keep your logic simple and clean, opting to do things like validation through reusable hooks instead of modifying the service logic directly. You said you wanted to add authentication to your service. This is where you would do it. In the before:[] hook section, add the authenticate(“jet”) hook your the methods you want to require authentication for.
service-name.service.js/.ts just handles service registration with the framework. Starting out, I wouldn’t mess around with this file much. It just performs some setup so the framework knows your service exists and attaches it to a specific route like /users.
Hope this helps.

Web3j call contract method without credentials

Already implemented the same solution in javascript(web3js) and c#(nethereum) and both of them allow to load smart contract with abi and deployed contract address. Right now I'm working in java environment(web3j) and to load smart contract I need to provide credentials.
I just want to call some static methods from contract, that doesn't require credentials. Is there any workaround to load contract without it? That way I'm not able to show contract data to user without giving credentials.
MyContract contract = MyContract.load(
"0x32b0138BD1b9527E95f141319ECF9B2765e06C00",
web3,
credentials,
new BigInteger("22000000000"),
new BigInteger("510000")
);
Since the operations you want to do don't require any Eth to execute, it doesn't matter what credentials you're using to do this. So you can just generate a new keypair, and use this.
Credentials dummyCredentials = Credentials.create(Keys.createEcKeyPair());
MyContract contract = MyContract.load(
"0x32b0138BD1b9527E95f141319ECF9B2765e06C00",
web3,
dummyCredentials,
new BigInteger("22000000000"),
new BigInteger("510000")
);

Sender account not recognized on private ethereum network

I'm currently developing a dApp in Solidity and am working on a web3 library to handle communication with it.
I struggle with the process of new account creation and transaction signing in web3. Before I continue it worth noting that I'm running my own local, private blockchain (currently with Ganache).
My code looks as follows:
try{
let a = web3.eth.accounts.create()
let dataTx = someContract.methods.someMethod().encodeABI()
let rawTx = {
to: someContract._address,
from: account.address,
data: dataTx,
gas: 10000000000
}
const transaction = web3.eth.accounts.signTransaction(rawTx, util.toBuffer(account.privateKey))
web3.eth.sendTransaction(rawTx).then(console.log)
}
catch(e){
console.log(e)
}
The problem here is that the web3.eth.sendTransaction() method raises the following exception: Error: Returned error: sender account not recognized.
My understanding is that web3.eth.accounts is used for managing local accounts and web3.eth.personal is used to communicate with a client (e.g. Geth). I wish to keep the private keys of accounts my app creates locally on the device of the web3 client, but it raises this exception.
Where am I going wrong? Should I register the newly created accounts somewhere before running transactions with it? Is there some vital information I'm missing here?
Thanks!
If you want to use an account other than Ganache provided you, you have to start Ganache providing your accounts data in the form private_key,initial_balance:
Example command: ganache-cli --account 0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56,123456789
And it's output:
Ganache CLI v6.1.8 (ganache-core: 2.2.1)
Available Accounts
================== (0) 0x44fa41e453654ccb365a358e994c764a37eea91f (~0 ETH)
Private Keys
================== (0) 0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56
Gas Price
================== 20000000000
Gas Limit
================== 6721975
Listening on 127.0.0.1:8545
I am having same issue in my project. The problem in my case is beacuse i am not using same web3 provider to create contract variable.
example code:
const providerEth= new Web3.providers.HttpProvider(
'HTTP://127.0.0.1:8545'
);
const web3Eth = new Web3(providerEth);
const contract= new web3Eth.eth.Contract(abi,address);
Here, we are not using metamask provider although both on same network Still it not recognize the account. So you should create contract like this
const web3Eth = new Web3(window.web3.currentProvider);
const contract= new web3Eth.eth.Contract(abi,address);
const accounts = await web3.eth.getAccounts();
var receipt=await contract.methods.transfer(receiver,amount).send({from:accounts[0]});
Now, you can able to call smart contract function with account address.
I had the same issue. It happened when I already opened my truffle console and after that I did a restart of my ganache because I wanted to start clean.
What fixed it for me was stopping the truffle console job and starting it again.
You are refering to functionality in web3 1.0.0 which is not yet fully released.
If you go to https://web3js.readthedocs.io/en/1.0/getting-started.html
you would see that they state the following:
This documentation is work in progress and web3.js 1.0 is not yet released! You can find the current documentation for web3 0.x.x at github.com/ethereum/wiki/wiki/JavaScript-API.
Most probably you are using a version 0.20.x or something like that so check this first. To check this open the dApp in the browser and type in the console the following:
web3.version.api
This should show you which version you are using.
I don't think there is a way to create accounts with web3js 0.20.x directly but you can try to update the web3js to the 1.0.0-beta and try to run your code again. You can find it in NPM here - https://www.npmjs.com/package/web3