Comments (2)
and my code:
console.log('provider');
//console.log(provider);
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
console.log('wallet');
//console.log(wallet);
const account = wallet.connect(provider);
console.log('account');
//console.log(account);
const factory = new ethers.Contract(
addresses.factory,
['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'],
account
);
console.log('factory');
//console.log(factory);
const router = new ethers.Contract(
addresses.router,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
],
account
);
console.log('router');
//console.log(router);
const wbnb = new ethers.Contract(
addresses.WBNB,
[
'function approve(address spender, uint amount) public returns(bool)',
],
account
);
console.log('wbnb');
//console.log(wbnb);
const init = async () => {
/*const tx = await wbnb.approve(
router.address,
'replace by amount covering several trades'
);
const receipt = await tx.wait(); */
console.log('Transaction receipt11');
//console.log(receipt);
}
console.log('PairCreated');
factory.on('error', function(err){
console.log('onerror');
console.log(err);
});
factory.on('PairCreated', async (token0, token1, pairAddress) => {
console.log(`
New pair detected
token0: ${token0}
token1: ${token1}
pairAddress: ${pairAddress}
`);
//The quote currency needs to be WBNB (we will pay with WBNB)
let tokenIn, tokenOut;
if(token0 === addresses.WBNB) {
tokenIn = token0;
tokenOut = token1;
}
if(token1 == addresses.WBNB) {
tokenIn = token1;
tokenOut = token0;
}
//The quote currency is not WBNB
if(typeof tokenIn === 'undefined') {
return;
}
//We buy for 0.1 BNB of the new token
//ethers was originally created for Ethereum, both also work for BSC
//'ether' === 'bnb' on BSC
const amountIn = ethers.utils.parseUnits('0.01', 'ether');
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
//Our execution price will be a bit different, we need some flexbility
const amountOutMin = amounts[1].sub(amounts[1].div(10));
console.log(Buying new token ================= tokenIn: ${amountIn.toString()} ${tokenIn} (WBNB) tokenOut: ${amounOutMin.toString()} ${tokenOut}
);
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Date.now() + 1000 * 60 * 10 //10 minutes
);
const receipt = await tx.wait();
console.log('Transaction receipt');
console.log(receipt);
});
init();
from eattheblocks.
Maybe try putting the provider starting with "wss" instead of "https:" - that worked for me. Make sure to put it in quotes too.
from eattheblocks.
Related Issues (20)
- Audit found that the transferFrom function never deducts the allowance value HOT 1
- Error recived Lesson 12 flash loans
- How can i deploy the pancakeswap sniper bot
- LESSON 28: DEPLOY FLASHLOAN SMART CONTRACT
- How to automate a function in a smart contract? HOT 2
- Money Legos- Source not found?
- 330-uniswap-trading How works on real chain?
- Error: Transaction has been reverted by the EVM - 298-arbitrage-uniswap-sushiswap HOT 1
- error when tried to connect to infura blockchain LESSON 12 HOT 1
- [day22-ICO] withdraw() never possible, because `released` never set to true.
- Truflle migrate stuck
- Warning when trying to compile Equalizer Finance loan using Remix
- Bug
- Adding a license, contributions and a code of conduct for the repo
- Create issue templates for the different types of issues
- TypeError: Cannot read property 'address' of undefined at init
- ArgumentsRequired: binance amount of BTC/USDT must be greater than minimum amount precision of 5
- 第12课。连接不到infura。报错1006
- ETH Amount in Wallet
- New Developers - List Your Blockchain dApps for Sale on Pinky Finance HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eattheblocks.