Skip to content

Commit

Permalink
Merge pull request #5 from GuiGou12358/main
Browse files Browse the repository at this point in the history
Create the verified contract for the dAppStaking
  • Loading branch information
GuiGou12358 authored Aug 6, 2024
2 parents 2769ce7 + 9a651f7 commit 6c5fc99
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ members = [
"logics",
"contracts/lotto",
"integration_tests",
"contracts/dapp_staking",
]
28 changes: 28 additions & 0 deletions ink/contracts/dapp_staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "lotto_dapp_staking"
version = "1.0.0"
authors = ["guigou"]
edition = "2021"

[dependencies]
ink = { version = "5.0.0", default-features = false}
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []

[profile.release]
overflow-checks = false

[profile.dev]
overflow-checks = false
35 changes: 35 additions & 0 deletions ink/contracts/dapp_staking/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Convert wasm contract to standard_input_json
# Example
# python convert.py --manifest xx.toml

import argparse
import os
import json

parser = argparse.ArgumentParser(description='Print all files in the specified directory')

parser.add_argument('--manifest', type=str, default="Cargo.toml", help='manifest path')
parser.add_argument('--exclude', type=str, nargs='+', default=[], help='exclude folder')

args = parser.parse_args()
extension = ["rs", "toml"]
exclude = [".idea", ".git", "target"] + args.exclude
includeFiles = ["Cargo.lock"]

inputJson = {
"manifest-path": args.manifest,
"contracts": {}
}

for root, dirs, files in os.walk("."):
dirs[:] = [d for d in dirs if d not in exclude]
for file in files:
if file.endswith(tuple(extension)) or file in includeFiles:
filePath = os.path.join(root, file)
with open(filePath, 'r') as f:
contents = f.read()
if contents == "":
continue
inputJson["contracts"][filePath.lstrip("./")] = contents

print(json.dumps(inputJson, sort_keys=True, indent=4, separators=(',', ':')))
171 changes: 171 additions & 0 deletions ink/contracts/dapp_staking/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[ink::contract]
pub mod lotto_dapp_staking {

type AccountId20 = [u8; 20];

/// Event emitted when ownership is transferred
#[ink(event)]
pub struct OwnershipTransferred {
#[ink(topic)]
contract: AccountId,
previous: Option<AccountId>,
new: Option<AccountId>,
}

#[ink(event)]
pub struct ILoveAstar {
#[ink(topic)]
sender: AccountId,
}

#[ink(event)]
pub struct ILoveLucky {
#[ink(topic)]
sender: AccountId,
}

/// Errors occurred in the contract
#[derive(Debug, Eq, PartialEq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ContractError {
CallerIsNotOwner,
NewOwnerIsNotSet,
}

/// Contract storage
#[ink(storage)]
#[derive(Default)]
pub struct Contract {
pub owner: Option<AccountId>,
pub substrate_address: Option<AccountId>,
pub zk_evm_address: Option<AccountId20>,
}

impl Contract {
#[ink(constructor)]
pub fn new() -> Self {
let mut instance = Self::default();
let caller = instance.env().caller();
// set the owner of this contract
instance.inner_set_ownership(Some(caller));
instance
}

#[ink(message)]
pub fn owner(&self) -> Option<AccountId> {
self.owner
}

#[ink(message)]
pub fn renounce_ownership(&mut self) -> Result<(), ContractError> {
// check caller is the owner
self.ensure_owner()?;
// remove owner
self.inner_set_ownership(None);
Ok(())
}

#[ink(message)]
pub fn transfer_ownership(
&mut self,
new_owner: Option<AccountId>,
) -> Result<(), ContractError> {
// check caller is the owner
self.ensure_owner()?;
// check the new owner is set
if new_owner.is_none() {
return Err(ContractError::NewOwnerIsNotSet);
}
// set the new owner
self.inner_set_ownership(new_owner);
Ok(())
}

fn ensure_owner(&self) -> Result<(), ContractError> {
if self.owner != Some(self.env().caller()) {
return Err(ContractError::CallerIsNotOwner);
}
Ok(())
}

fn inner_set_ownership(&mut self, new_owner: Option<AccountId>) {
let old_owner = self.owner;
self.owner = new_owner;
// emit an event
self.env().emit_event(OwnershipTransferred {
contract: self.env().account_id(),
previous: old_owner,
new: new_owner,
});
}

#[ink(message)]
pub fn set_code(&mut self, code_hash: Hash) -> Result<(), ContractError> {
// check caller is the owner
self.ensure_owner()?;

self.env().set_code_hash(&code_hash).unwrap_or_else(|err| {
panic!(
"Failed to `set_code_hash` to {:?} due to {:?}",
code_hash, err
)
});

Ok(())
}

#[ink(message)]
pub fn set_substrate_address(&mut self, address: AccountId) -> Result<(), ContractError> {
// check caller is the owner
self.ensure_owner()?;
// set the address
self.substrate_address = Some(address);
Ok(())
}

#[ink(message)]
pub fn get_substrate_address(&mut self) -> Option<AccountId> {
self.substrate_address
}

#[ink(message)]
pub fn lotto_is_deployed_on_astar_substrate(&mut self) -> bool {
self.substrate_address.is_some()
}

#[ink(message)]
pub fn set_zk_evm_address(&mut self, address: AccountId20) -> Result<(), ContractError> {
// check caller is the owner
self.ensure_owner()?;
// set the address
self.zk_evm_address = Some(address);
Ok(())
}

#[ink(message)]
pub fn get_zk_evm_address(&mut self) -> Option<AccountId20> {
self.zk_evm_address
}

#[ink(message)]
pub fn lotto_is_deployed_on_astar_zk_evm(&mut self) -> bool {
self.zk_evm_address.is_some()
}

#[ink(message)]
pub fn send_love_to_astar(&mut self) {
self.env().emit_event(ILoveAstar {
sender: self.env().caller(),
});
}

#[ink(message)]
pub fn send_love_to_lucky(&mut self) {
self.env().emit_event(ILoveLucky {
sender: self.env().caller(),
});
}
}
}
11 changes: 11 additions & 0 deletions ink/contracts/dapp_staking/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[toolchain]
channel = "1.77.2"
components = [
"rustc",
"cargo",
"rustfmt",
"rust-src",
"clippy",
]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"

0 comments on commit 6c5fc99

Please sign in to comment.