Lesson 6 - Address Validation
Introduction
Prerequisites
Objectives and Outcomes
Exercise
Vulnerable Smart contract
#![cfg_attr(not(feature = "std"), no_std)]
#[ink::contract]
mod s0token {
use ink::{storage::Mapping};
#[ink(storage)]
pub struct S0token {
total_supply: u32,
balances: Mapping<AccountId, u32>
}
impl S0token {
/// Creates a token contract with the given initial supply belonging to the contract creator
#[ink(constructor)]
pub fn new_token(supply: u32) -> Self {
let mut balances = Mapping::default();
let caller = Self::env().caller();
balances.insert(&caller,&supply);
Self {
total_supply: supply,
balances
}
}
/// Total Supply of s0token
#[ink(message)]
pub fn total_supply(&self) -> u32 {
self.total_supply
}
/// Current balance of the chosen account.
#[ink(message)]
pub fn balance_of(&self, account: AccountId) -> u32 {
match self.balances.get(&account) {
Some(value) => value,
None => 0,
}
}
/// Transfers an amount of tokens to the chosen recipient.
#[ink(message)]
pub fn transfer(&mut self, recipient: AccountId, amount: u32) {
let sender = self.env().caller();
let sender_balance = self.balance_of(sender);
if sender_balance < amount {
return;
}
self.balances.insert(sender, &(sender_balance - amount));
let recipient_balance = self.balance_of(recipient);
self.balances.insert(recipient, &(recipient_balance + amount));
}
}
}
Simulated Attack
Setup
Build and Deploy to Testnet
Secure Solution
Verifying Secure Solution
Setup
Build and Deploy to Testnet
Using Cargo Contract Command line
Question
Last updated
Was this helpful?
