What does hardhat "deployed()" do? - javascript

I have the below code
const SimpleStorageFactory = await ethers.getContractFactory(
"SimpleStorage"
)
const simpleStorage = await SimpleStorageFactory.deploy()
await simpleStorage.deployed()
await simpleStorage.deployTransaction.wait(6)
I understand that hardhat's getContractFactory is automatically supplied the abi, binary and specified private key for signing transactions. After this is done I am assuming that SimpleStorageFactory.deploy() is the same as in ethers and deploys the contract to the blockchain, and then I am waiting 6 network confirmations. However, I am confused why hardhat has await simpleStorage.deployed() and what this does?
I have tried reading the documentation on hardhat but have not found an answer to this.

The await simpleStorage.deployed() line of code is waiting for the SimpleStorage contract to be deployed to the Ethereum network. When a contract is deployed, it is uploaded to the network and made available for interaction.
The deployed() method is a Promise that returns a ContractReceipt object, which contains information about the deployment transaction, such as the transaction hash, block number, and contract address.
This method is useful because it allows you to ensure that the contract has been deployed and is available for interaction before you try to call any of its methods or access its state. Without this line, the rest of the code might execute before the contract is fully deployed, which could result in errors.
The await simpleStorage.deployTransaction.wait(6) line of code is waiting for the deployment transaction to be included in six blocks on the Ethereum blockchain. This is done to ensure that the transaction has sufficient confirmations, which helps to ensure the transaction will not be reversed.

This hardhat plugin adds a mechanism to deploy contracts to any network, keeping track of them and replicating the same environment for testing.

Related

Web3 contract.methods.approve(contractAddress, value), how to prompt user to approve using test account address?

I am able to call Smart contract methods from client machine in development env:
example:
contract.methods.allowance
contract.methods.approve
and I can see the transactions in Ganache UI returned from contract.methods.approve
Which is fine. However, what I am expecting to see in the browser is a prompt in the connected wallet to approve the transaction, similar to the prompt I get when calling:
window.ethereum.request({ method: 'eth_requestAccounts' });
or
window.ethereum.request({ method: 'eth_sendTransaction', params});
The account I am using is one I used provided by Ganache UI where I imported the private key into metamask.
I must be misunderstanding something because there is no point in an "approve" feature conceptually if there is no "approving" taking place by the owner of the address apart from the client machine calling "approve". Does this have something to do with the fact the the dev env already knows what the private key is?
When switching metamask account to an account outside of the development network (my actual account), I get "sender account not recognized" error. But why isn't default behaviour to ask the unrecognised account to approve the transaction?
example, I can call: window.ethereum.request({ method: 'eth_sendTransaction', params}); with the "unrecognised" account and approve the transaction but I can't make a SC call and approve it with this account.
To answer my own question the answer is:
Use metamask "window.ethereum" as a provider allowing it to select the network instead of injecting the RPC directly.
I also switched to ethersjs and stopped using web3js. So the following:
const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
Instead of injecting an http provider since the the network doesn't have you private key.

Web3js event listen / subscribe to transactions to or from a wallet address

So I am connecting to Binance Smart Chain RPC websocket from Moralis.io. And trying to listen to wallet amount changes of a certain wallet address.
What I want to do is, I want to trigger a event whenever there is a transfer from or to this wallet address. I understand how to do it with BEP20 tokens but I require a solution to monitor the wallet address for BNB transfers.
The code that I put together:
const web3 = new Web3(new Web3.providers.WebsocketProvider('URI'))
let options = {
address: '0xe....'
}
const subscribe = web3.eth.subscribe('logs', options, (err, res) => {})
subscribe.on('data', (txLog) => console.log(txLog))
This doesn't work when I send a certain amount of BNB to this account.
I went through the documentation of web3js but couldn't figure out.
I found a longer way around for this problem where someone suggests to listen to all pending transactions and try to get the transaction data of each of these transactions and evaluate if any of these are from or to the wallet address. But I think it is an extremely ineffective and inefficient way to do it as it requires someone to run their own node to do it in a meaningful way.
listen to all pending transactions and try to get the transaction data of each of these transactions
I agree that this is an ineffective way. But with the current state of the JSON-RPC API, it's still the most effective way available. Apart from using a 3rd party service that does all this work in the background and provides the data over their custom API.
Note: The link goes to Ethereum documentation, but Binance Smart Chain implements the same JSON-RPC API.
There's simply no method to subscribe to or poll incoming native transactions to a specified address.
And since web3, ethers.js, and many other libraries are wrappers of this API, they can only support methods that the API supports.

Can I send transactions async using polkadot-js

I have walked through the official document and found a page about how to transfer using polkadot-js https://polkadot.js.org/docs/api/examples/promise/make-transfer
const transfer = api.tx.balances.transfer(BOB, 12345);
const hash = await transfer.signAndSend(alice);
I want to know if I can split the signAndSend method into two and execute at different machines. like in a client machine, in the browser compute the signature.
const transfer = api.tx.balances.transfer(BOB, 12345);
const signature = await transfer.signAsync(alice);
and then in the server side send the transfer transaction.
const mockSigner = createMockSigner(signature); // signature is computed from the client side and send to server over HTTP
const transfer = api.tx.balances.transfer(BOB, 12345);
const res = transfer.send({signer: mockSigner});
The above example doesn't work, I just want to express if I can do sign and send in different machines.
Signing a transaction on one computer and sending it from a second computer is definitely possible.
PolkadotJS Tools contains a method for building and signing a transaction offline. You can find the source here. Please note that building the transaction in the browser will still require access to a polkadot node (the endpoint in the code).
The signer sendOffline command has the exact same API, but will not
broadcast the transaction. submit and sendOffline must be connected to
a node to fetch the current metadata and construct a valid
transaction. Their API has the format:
Therefore, you'd need to run a light client in the browser in order to have access to current block information or attach to some other node endpoint outwith the browser.
The offline sign version:
https://gist.github.com/xcaptain/4d190232411dcf27441d9fadd7ff6988
The online sign version:
const transfer = api.tx.balances.transfer(BOB, 12345);
const signedExtrinsic = await transfer.signAsync(alice).toJSON();
await api.rpc.author.submitExtrinsic(signedExtrinsic);
Don't know what's the difference, but they both work.

How to create/add an account to truffle test environment

I'm writing test for my solidity project. Using Truffle #5.3.5. So when running truffle test It generates 10 accounts with 100 ETH (I believe it runs with ganache in the background).
The problem lies with I need an account that starts with 0 ETH. I have looked through the docs and haven't seen a solution to this.
I have tried using the ethers library to generate a new wallet. but it the test node doesn't recognize address (rather it doesn't know the private key for that address).
Is there a way to do this within the truffle testing environment.
contract("TreasuryContract", ([ treasurer, managerRewardAddress, employeeRewardAddress ]) =>
{...})
The test itself isn't really relative to the question but the above is the method I'm using to get the accounts. (this all works fine).
I was just hoping for an easy way to have an account that the test recognizes that start with 0 ETH
In Ganache configuration, accounts & keys, you can change de account default balance and put it in 0.
Bests

RabbitMQ Exchange not found in vhost "/'

I am trying to set up a retry system in my Rabbitmq setup. In order to achieve this, I have to declare some exchanges, queues and create the necessary bindings between them. So I have something of this nature
await subscriber.createExchange('TTL-PAYMENTS', 'direct')
await subscriber.createExchange('DLX-PAYMENTS', 'fanout')
await subscriber.createQueues('payments-retry-1-30s', 'DLX-PAYMENTS', 30000)
await subscriber.createQueues('payments-retry-2-50s', 'DLX-PAYMENTS', 50000)
await subscriber.bindExchanges('payments', 'DLX-PAYMETS')
await subscriber.bindExchanges('payments-retry-1-30s', 'TTL-PAYMETS', 'retry-1')
await subscriber.bindExchanges('payments-retry-2-50s', 'TTL-PAYMETS', 'retry-2')
But when I start up the server, I get this error
Error: Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no exchange 'DLX-PAYMETS' in vhost '/'"
When I check my rabbitmq management dashboard, I can see that the exchanges and necessary queues are created already. From my understanding, an exchange Is idempotent, meaning that if it doesn't exist it will be created, and if it does exist as long as nothing changes in exchange attributes, the exchange should remain the same. So I am quite confused as to why this is happening. What could I be doing wrong? And how can I rectify this issue? Thank you very much!
Looks like there's a typo with DLX-PAYMETS, missing the N. You have created an exchange named DLX-PAYMENTS.

Categories

Resources