Skip to content

Commit

Permalink
Use 1.81 and use custom sqrt
Browse files Browse the repository at this point in the history
  • Loading branch information
af-afk committed Jan 25, 2025
1 parent ce3c97d commit 5b4b491
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "shahmeersgame"
version = "0.1.0"
edition = "2024"
edition = "2021"

[lib]
name = "libshahmeersgame"
Expand Down
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@

.PHONY: clean

WASM_SRC := target/wasm32-unknown-unknown/release/shahmeersgame.wasm

shahmeersgame.wasm: $(shell find src -type f)
@forge build
@rm -f ${WASM_SRC} shahmeersgame.wasm
@cargo build \
--release \
--target wasm32-unknown-unknown
@mv target/wasm32-unknown-unknown/release/shahmeersgame.wasm .
@mv ${WASM_SRC} .

clean:
@rm -rf \
mutants.out.old \
mutants.out \
target
8 changes: 8 additions & 0 deletions deploy-mainnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh -e

export \
SPN_DILUTION_CONCEPT_AMT=100000000 \
SPN_DILUTION_SUBMITTER_AMT=10000000 \
SPN_SHAHMEERSGAME_TOKEN_ADDR=0xFDab24861F407765E6E64c282420585ef7cf68fe

./deploy.sh
27 changes: 25 additions & 2 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
#!/bin/sh
#!/bin/sh -e

impl="$(./deploy-stylus.sh shahmeersgame.wasm)"
err() {
>&2 echo $@
exit 1
}

[ -z "$SPN_ADMIN_ADDR" ] && err "SPN_ADMIN_ADDR unset"
[ -z "$SPN_DILUTION_CONCEPT_AMT" ] && err "SPN_DILUTION_CONCEPT_AMT unset"
[ -z "$SPN_DILUTION_SUBMITTER_AMT" ] && err "SPN_DILUTION_SUBMITTER_AMT unset"
[ -z "$SPN_SHAHMEERSGAME_TOKEN_ADDR" ] && err "SPN_SHAHMEERSGAME_TOKEN_ADDR unset"

if [ -z "$SPN_SHAHMEERSGAME_IMPL" ]; then
export SPN_SHAHMEERSGAME_IMPL="$(./deploy-stylus.sh shahmeersgame.wasm)"
fi

[ -z "$SPN_SHAHMEERSGAME_IMPL" ] && exit 1

echo "SPN_SHAHMEERSGAME_IMPL=$SPN_SHAHMEERSGAME_IMPL"

forge create \
--broadcast \
--rpc-url "$SPN_SUPERPOSITION_URL" \
--private-key "$SPN_SUPERPOSITION_KEY" \
foundry-libs/openzeppelin-contracts/contracts/proxy/transparent/TransparentUpgradeableProxy.sol:TransparentUpgradeableProxy \
--constructor-args "$impl" "$SPN_ADMIN_ADDR" "$SPN_SHAHMEERSGAME_IMPL" "$(cast calldata 'ctor(address,address,uint256,uint256)' "$SPN_SHAHMEERSGAME_TOKEN_ADDR" "$SPN_ADMIN_ADDR" "$SPN_DILUTION_CONCEPT_AMT" "$SPN_DILUTION_SUBMITTER_AMT")" \
| jq -r .deployedTo
1 change: 0 additions & 1 deletion lib/forge-std
Submodule forge-std deleted from b93cf4
1 change: 0 additions & 1 deletion lib/openzeppelin-contracts
Submodule openzeppelin-contracts deleted from acd4ff
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "1.81"
components = [ "rust-src" ]
31 changes: 28 additions & 3 deletions src/maths.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@

use stylus_sdk::alloy_primitives::U256;

pub const SCALING_AMT: U256 = U256::from_limbs([1000, 0, 0, 0]);

/// Compute the quadratic voting power of a STG amount.
/// Compute the quadratic voting power of a STG amount using the Babylonian method.
pub fn stg_to_quad(x: U256) -> U256 {
x.root(2)
// Since the Alloy implementation uses some floating point conversions
// the Stylus runtime doesn't have. Rounds down.
if x.is_zero() {
return U256::ZERO;
}
let mut z = (x >> 1) + U256::from(1);
let mut y = x;
while z < y {
y = z;
z = (x / z + z) >> 1;
}
if y * y > x {
y -= U256::from(1);
}
y
}

#[cfg(not(target_arch = "wasm32"))]
mod test {
use proptest::prelude::*;

proptest! {
#[test]
fn test_stg_to_quad(x in crate::storage::strat_tiny_u256()) {
assert_eq!(x.root(2), super::stg_to_quad(x));
}
}
}

0 comments on commit 5b4b491

Please sign in to comment.