Lesson 3 - Integer Overflow
Introduction
Prerequisites
Objectives and Outcomes
Exercise
Vulnerable Smart contract
#![cfg_attr(not(feature = "std"), no_std)]
#[ink::contract]
mod bank {
use ink::storage::Mapping;
#[ink(storage)]
#[derive(Default)]
pub struct Bank {
bank: u8,
balances: Mapping<AccountId, u8>,
}
impl Bank {
/// Constructor that initializes the supply of the token bank
#[ink(constructor)]
pub fn new(supply: u8) -> Self {
let balances = Mapping::default();
Self{
balances,
bank: supply,
}
}
/// report the totaly supply of token
#[ink(message)]
pub fn bank(&self) -> u8 {
self.bank
}
/// Simply returns the current balance of token in balances
#[ink(message)]
pub fn balance_of(&self, account: AccountId) -> u8 {
match self.balances.get(&account) {
Some(value) => value,
None => 0,
}
}
#[ink(message)]
pub fn withdraw(&mut self, amount: u8) {
let sender = self.env().caller();
let sender_balance = self.balance_of(sender);
if sender_balance + amount < sender_balance {
return;
}
self.balances.insert(sender, &(sender_balance + amount));
self.bank -= amount;
}
}
}
Simulated Attack
Secure Solution
Question
Last updated
Was this helpful?
