From 5b4b4914fd868ab7f3e8a32928d957bd2d6ba1d0 Mon Sep 17 00:00:00 2001 From: bayge Date: Sun, 26 Jan 2025 01:03:04 +1030 Subject: [PATCH] Use 1.81 and use custom sqrt --- Cargo.toml | 2 +- Makefile | 13 ++++++++++++- deploy-mainnet.sh | 8 ++++++++ deploy.sh | 27 +++++++++++++++++++++++++-- lib/forge-std | 1 - lib/openzeppelin-contracts | 1 - rust-toolchain.toml | 3 +++ src/maths.rs | 31 ++++++++++++++++++++++++++++--- 8 files changed, 77 insertions(+), 9 deletions(-) create mode 100755 deploy-mainnet.sh delete mode 160000 lib/forge-std delete mode 160000 lib/openzeppelin-contracts create mode 100644 rust-toolchain.toml diff --git a/Cargo.toml b/Cargo.toml index 3a60a12..508bcb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "shahmeersgame" version = "0.1.0" -edition = "2024" +edition = "2021" [lib] name = "libshahmeersgame" diff --git a/Makefile b/Makefile index a07a8a4..8118541 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/deploy-mainnet.sh b/deploy-mainnet.sh new file mode 100755 index 0000000..a2c2682 --- /dev/null +++ b/deploy-mainnet.sh @@ -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 diff --git a/deploy.sh b/deploy.sh index cd8bf75..ffba8be 100755 --- a/deploy.sh +++ b/deploy.sh @@ -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 diff --git a/lib/forge-std b/lib/forge-std deleted file mode 160000 index b93cf4b..0000000 --- a/lib/forge-std +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b93cf4bc34ff214c099dc970b153f85ade8c9f66 diff --git a/lib/openzeppelin-contracts b/lib/openzeppelin-contracts deleted file mode 160000 index acd4ff7..0000000 --- a/lib/openzeppelin-contracts +++ /dev/null @@ -1 +0,0 @@ -Subproject commit acd4ff74de833399287ed6b31b4debf6b2b35527 diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..c914dc4 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "1.81" +components = [ "rust-src" ] diff --git a/src/maths.rs b/src/maths.rs index f69eb2b..f5fc75f 100644 --- a/src/maths.rs +++ b/src/maths.rs @@ -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)); + } + } }