Created
September 8, 2022 00:50
-
-
Save BlockmanCodes/397aad133efb8b41369df9b88737d6b6 to your computer and use it in GitHub Desktop.
Crowdsale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react'; | |
import { ethers } from 'ethers'; | |
import './App.css'; | |
import { Tsunami } from 'react-bootstrap-icons' | |
import artifact from './artifacts/contracts/Crowdsale.sol/Crowdsale.json' | |
const CONTRACT_ADDRESS = '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512' | |
function App() { | |
const [provider, setProvider] = useState(undefined); | |
const [signer, setSigner] = useState(undefined); | |
const [contract, setContract] = useState(undefined); | |
const [signerAddress, setSignerAddress] = useState(undefined); | |
const [amount, setAmount] = useState(0) | |
useEffect(() => { | |
const onLoad = async () => { | |
const provider = await new ethers.providers.Web3Provider(window.ethereum) | |
setProvider(provider) | |
const contract = await new ethers.Contract( | |
CONTRACT_ADDRESS, | |
artifact.abi, | |
provider | |
) | |
setContract(contract) | |
} | |
onLoad() | |
}, []) | |
const isConnected = () => (signer !== undefined) | |
const connect = () => { | |
getSigner(provider) | |
.then(signer => { | |
setSigner(signer) | |
}) | |
} | |
const getSigner = async provider => { | |
const signer = provider.getSigner(); | |
signer.getAddress() | |
.then((address) => { | |
setSignerAddress(address) | |
}) | |
return signer; | |
} | |
const toWei = ether => ethers.utils.parseEther(ether) | |
const buyTokens = async () => { | |
const wei = toWei(amount) | |
await contract.connect(signer).buyTokens(signerAddress, {value: wei}) | |
} | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
{isConnected() ? ( | |
<div> | |
<p> | |
Welcome {signerAddress?.substring(0,10)}... | |
</p> | |
<div className="list-group"> | |
<div className="list-group-item"> | |
<div className="row py-3"> | |
<div className="col-md-2"> | |
<Tsunami className="rounded-circle" width="36" height="36" /> | |
</div> | |
<div className="col-md-5"> | |
<input | |
className="inputField" | |
placeholder="0.0" | |
onChange={e => setAmount(e.target.value)} | |
/> | |
</div> | |
<div className="d-flex gap-4 col-md-3"> | |
RXC | |
</div> | |
<div className="d-flex gap-4 col-md-2"> | |
<button | |
class="btn btn-success" | |
onClick={() => buyTokens()}> | |
Buy | |
</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
) : ( | |
<div> | |
<p> | |
You are not connected | |
</p> | |
<button onClick={connect} className="btn btn-primary">Connect Metamask</button> | |
</div> | |
)} | |
</header> | |
</div> | |
); | |
} | |
export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { expect } = require("chai"); | |
describe('Staking', () => { | |
beforeEach(async () => { | |
[owner, signer2, signer3] = await ethers.getSigners(); | |
RexCoin = await ethers.getContractFactory('RexCoin', owner); | |
rexCoin = await RexCoin.deploy(); | |
Crowdsale = await ethers.getContractFactory('Crowdsale', owner); | |
crowdSale = await Crowdsale.deploy(2, owner.address, rexCoin.address); | |
}); | |
describe('buyTokens', () => { | |
it('adds a token symbol', async () => { | |
let totalSupply; | |
let signer2Balance; | |
let signer3Balance; | |
totalSupply = await rexCoin.totalSupply() | |
signer2Balance = await rexCoin.balanceOf(signer2.address) | |
signer3Balance = await rexCoin.balanceOf(signer3.address) | |
expect(totalSupply).to.be.equal(0) | |
expect(signer2Balance).to.be.equal(0) | |
expect(signer3Balance).to.be.equal(0) | |
await rexCoin.connect(owner).mint( | |
crowdSale.address, | |
ethers.utils.parseEther('10000') | |
) | |
const ownerEtherBalanceOld = await owner.getBalance() | |
await crowdSale.connect(signer2).buyTokens(signer2.address, {value: ethers.utils.parseEther('10')}) | |
await crowdSale.connect(signer3).buyTokens(signer3.address, {value: ethers.utils.parseEther('20')}) | |
totalSupply = await rexCoin.totalSupply() | |
signer2Balance = await rexCoin.connect(owner).balanceOf(signer2.address) | |
signer3Balance = await rexCoin.connect(owner).balanceOf(signer3.address) | |
const ownerEtherBalanceNew = await owner.getBalance() | |
expect(totalSupply).to.be.equal(ethers.utils.parseEther('10000')) | |
expect(signer2Balance).to.be.equal(ethers.utils.parseEther('20')) | |
expect(signer3Balance).to.be.equal(ethers.utils.parseEther('40')) | |
expect(ownerEtherBalanceNew).to.be.above(ownerEtherBalanceOld) | |
}) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function main() { | |
[owner, signer2, signer3] = await ethers.getSigners(); | |
RexCoin = await ethers.getContractFactory('RexCoin', owner); | |
rexCoin = await RexCoin.deploy(); | |
Crowdsale = await ethers.getContractFactory('Crowdsale', owner); | |
crowdSale = await Crowdsale.deploy(2, owner.address, rexCoin.address); | |
await rexCoin.connect(owner).mint( | |
crowdSale.address, | |
ethers.utils.parseEther('10000') | |
) | |
console.log("Crowdsale:", crowdSale.address); | |
console.log("RexCoin:", rexCoin.address); | |
console.log("signer2:", signer2.address); | |
} | |
// npx hardhat run --network localhost scripts/deploy.js | |
main() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom/client'; | |
import './index.css'; | |
import App from './App'; | |
import 'bootstrap/dist/css/bootstrap.css'; | |
const root = ReactDOM.createRoot(document.getElementById('root')); | |
root.render( | |
<React.StrictMode> | |
<App /> | |
</React.StrictMode> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, can you share your contact details?