Skip to content

Commit

Permalink
Merge pull request #2 from kage-collective/feat/optimize-map
Browse files Browse the repository at this point in the history
Feat/optimize map
  • Loading branch information
kagenkoda authored Apr 12, 2024
2 parents 8287e83 + 08d1740 commit 9fab2ac
Show file tree
Hide file tree
Showing 11 changed files with 2,174 additions and 50 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ artifacts/
tarpaulin-report.*

# raw json schema files
**/raw/*.json
**/raw/*.json

**/node_modules/
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = ["packages/*", "contracts/*"]

[workspace.package]
version = "0.21.0"
version = "0.22.0"
edition = "2021"
license = "MIT"
repository = "https://github.com/kage-collective/cw404-capped"
Expand Down
48 changes: 43 additions & 5 deletions DEPLOY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
```bash
INJ_ADDRESS=inj1635cku3flgssj6q92l48juyw7c5war2v52vj8q

yes 12345678 | injectived tx wasm store ./artifacts/cw404_fixed-aarch64.wasm \
yes 12345678 | injectived tx wasm store ./artifacts/cw404.wasm \
--from=$(echo $INJ_ADDRESS) \
--chain-id="injective-888" \
--yes --gas-prices=500000000inj --gas=20000000 \
--node=https://testnet.sentry.tm.injective.network:443


CODE_ID=7761
# CODE_ID=7761

CODE_ID=8417 # original cw404
# CODE_ID=8408 # sequential remints

CW404_INIT='{"name":"LIMIT BREAK","symbol":"LBK","decimals":18,"total_native_supply":"15000"}'
# CW404_INIT='{"name":"LIMIT BREAK","symbol":"LBK2","decimals":18,"total_native_supply":"15000"}'
# CW404_INIT='{"name":"LIMIT BREAK","symbol":"LBK3","decimals":18,"total_native_supply":"1000"}'
CW404_INIT='{"name":"OG404","symbol":"OG4042","decimals":18,"total_native_supply":"1000"}'

yes 12345678 | injectived tx wasm instantiate $CODE_ID $CW404_INIT --label="Init CW404" --from=$(echo $INJ_ADDRESS) --chain-id="injective-888" --yes --gas-prices=500000000inj --gas=20000000 --admin=$(echo $INJ_ADDRESS) --node=https://testnet.sentry.tm.injective.network:443 --dry-run

TEST_404=inj1h4nqsp435wnzaau4sqsuvqkxngkuad9m0dawnx
# TEST_404=inj1h4nqsp435wnzaau4sqsuvqkxngkuad9m0dawnx

# TEST_404=inj1ta0jferds2wut90stl0l7sfxpftfjw5yejj96z # LBK2
# TEST_404=inj1eu902t6hpusk0mmt9fk5xu4zwhyufgsc349ae0 # LBK3
# TEST_404=inj1j7xjm9c8v5gju3xesyeqmpgp360plfaeg70nps # OG404
# TEST_404=inj1mdq3x4er0q52cs0sfrexndxuql095mt4fhayjg # OG4042

WHITELIST_SELF='{"set_whitelist":{"target":"'$INJ_ADDRESS'","state":true}}'

Expand All @@ -29,8 +38,37 @@ injectived query wasm contract-state smart $TEST_404 "$BALANCE_404" --node=https

INJ_USER2=inj15x87psc989d5yeyxs9vpe5aa7wlaak6730v43q

TRANSFER_404='{"transfer":{"recipient":"'$INJ_USER2'","amount":"400000000000000000000"}}'
TRANSFER_404='{"transfer":{"recipient":"'$INJ_USER2'","amount":"50000000000000000000"}}'

yes 12345678 | injectived tx wasm execute $TEST_404 "$TRANSFER_404" --from=$(echo $INJ_ADDRESS) --chain-id="injective-888" --yes --gas-prices=500000000inj --gas=50000000 --node=https://testnet.sentry.tm.injective.network:443 --dry-run


TRANSFER_404='{"transfer":{"recipient":"'$INJ_USER2'","amount":"350000000000000000000"}}'

yes 12345678 | injectived tx wasm execute $TEST_404 "$TRANSFER_404" --from=$(echo $INJ_ADDRESS) --chain-id="injective-888" --yes --gas-prices=500000000inj --gas=50000000 --node=https://testnet.sentry.tm.injective.network:443 --dry-run


INJ_USER3=inj1y7p4rctk7y6tkjh3zr4hpqu5dyr334m8wfdvr7

TRANSFER_404='{"transfer":{"recipient":"'$INJ_USER2'","amount":"100000000000000000000"}}'

yes 12345678 | injectived tx wasm execute $TEST_404 "$TRANSFER_404" --from=$(echo $INJ_ADDRESS) --chain-id="injective-888" --yes --gas-prices=500000000inj --gas=50000000 --node=https://testnet.sentry.tm.injective.network:443 --dry-run


INJ_USER4=inj1eukw5lgakapal054nldataq00tv3cuqn2g5405

TRANSFER_404='{"transfer":{"recipient":"'$INJ_USER4'","amount":"100000000000000000000"}}'

yes 12345678 | injectived tx wasm execute $TEST_404 "$TRANSFER_404" --from=$(echo $INJ_ADDRESS) --chain-id="injective-888" --yes --gas-prices=500000000inj --gas=50000000 --node=https://testnet.sentry.tm.injective.network:443 --dry-run



TOKEN_POOL_QUERY='{"token_pool":{}}'
injectived query wasm contract-state smart $TEST_404 "$TOKEN_POOL_QUERY" --node=https://testnet.sentry.tm.injective.network:443 --output json



SET_CAP='{"set_token_id_cap":{"cap":"1100"}}'

yes 12345678 | injectived tx wasm execute $TEST_404 "$SET_CAP" --from=$(echo $INJ_ADDRESS) --chain-id="injective-888" --yes --gas-prices=500000000inj --gas=50000000 --node=https://testnet.sentry.tm.injective.network:443 --dry-run
```
2 changes: 1 addition & 1 deletion contracts/cw404/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use crate::msg::{ExecuteMsg, InstantiateMsg, MinterResponse, QueryMsg};
use cosmwasm_std::Empty;

// Version info for migration
pub const CONTRACT_NAME: &str = "dojoswap:cw404";
pub const CONTRACT_NAME: &str = "kagefinance:cw404-capped";
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg(not(feature = "library"))]
Expand Down
36 changes: 9 additions & 27 deletions contracts/cw404/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::str::FromStr;

use cosmwasm_std::{
to_json_binary, Binary, DepsMut, Env, MessageInfo, Order, Response, StdResult, Storage,
Uint128, WasmMsg,
to_json_binary, Binary, DepsMut, Env, MessageInfo, Response, StdResult, Storage, Uint128,
WasmMsg,
};

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg};
use crate::state::{
Cw20ReceiveMsg, ALLOWANCE, APPROVED_FOR_ALL, BALANCES, BASE_TOKEN_URI, DECIMALS, GET_APPROVED,
ID_ASSIGNED, LOCKED, MINTED, NAME, OWNED, OWNED_INDEX, OWNER, OWNER_OF, SYMBOL, TOKEN_ID_CAP,
TOTAL_SUPPLY, WHITELIST,
LOCKED, MINTED, NAME, OWNED, OWNED_INDEX, OWNER, OWNER_OF, SYMBOL, TOKEN_ID_CAP, TOTAL_SUPPLY,
UNASSIGNED_IDS, WHITELIST,
};

pub fn instantiate(
Expand Down Expand Up @@ -607,22 +607,11 @@ fn _mint(storage: &mut dyn Storage, env: Env, to: String) -> Result<WasmMsg, Con
} else {
// all tokens minted
// find first unassigned token in map (lexical order as key is in string)
next_id = ID_ASSIGNED
.range(storage, None, None, Order::Ascending)
.find(|item| {
if let Ok((_, assigned)) = item {
!assigned
} else {
false
}
})
next_id = UNASSIGNED_IDS
.first(storage)?
.map_or_else(
|| Err(ContractError::NoAvailableId {}),
|f| {
// if result is found, parse the key to Uint128
let key = f.unwrap().0;
Ok(Uint128::from_str(&key).unwrap())
},
|(key, _)| Ok(Uint128::from_str(&key).unwrap()),
)
.unwrap();
}
Expand All @@ -635,7 +624,7 @@ fn _mint(storage: &mut dyn Storage, env: Env, to: String) -> Result<WasmMsg, Con
return Err(ContractError::AlreadyExists {});
}

ID_ASSIGNED.save(storage, next_id.to_string(), &true)?;
UNASSIGNED_IDS.remove(storage, next_id.to_string());

OWNER_OF.save(storage, next_id.to_string(), &to)?;

Expand Down Expand Up @@ -667,14 +656,7 @@ fn _burn(storage: &mut dyn Storage, env: Env, from: String) -> Result<WasmMsg, C
let mut owned = OWNED.may_load(storage, from.clone())?.unwrap_or(vec![]);
let id = owned[owned.len() - 1];

// set token_id as unassigned
let is_assigned = ID_ASSIGNED.load(storage, id.to_string())?;

if !is_assigned {
return Err(ContractError::IdNotAssigned {});
}

ID_ASSIGNED.save(storage, id.to_string(), &false)?;
UNASSIGNED_IDS.save(storage, id.to_string(), &true)?;

owned.pop();
OWNED.save(storage, from.clone(), &owned)?;
Expand Down
15 changes: 4 additions & 11 deletions contracts/cw404/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::msg::{
ExtendedInfoResponse, MinterResponse, QueryMsg, TokenPoolResponse, UserInfoResponse,
};
use crate::state::{
ALLOWANCE, BALANCES, BASE_TOKEN_URI, DECIMALS, GET_APPROVED, ID_ASSIGNED, LOCKED, MINTED, NAME,
OWNED, OWNED_INDEX, OWNER_OF, SYMBOL, TOKEN_ID_CAP, TOTAL_SUPPLY,
ALLOWANCE, BALANCES, BASE_TOKEN_URI, DECIMALS, GET_APPROVED, LOCKED, MINTED, NAME, OWNED,
OWNED_INDEX, OWNER_OF, SYMBOL, TOKEN_ID_CAP, TOTAL_SUPPLY, UNASSIGNED_IDS,
};

const DEFAULT_LIMIT: u32 = 10;
Expand Down Expand Up @@ -279,15 +279,8 @@ pub fn minter(deps: Deps) -> StdResult<MinterResponse> {
}

pub fn token_pool(deps: Deps) -> StdResult<TokenPoolResponse> {
let pool_count = ID_ASSIGNED
.range(deps.storage, None, None, Order::Ascending)
.filter(|item| {
if let Ok((_, assigned)) = item {
!assigned
} else {
false
}
})
let pool_count = UNASSIGNED_IDS
.keys(deps.storage, None, None, Order::Ascending)
.count();

let token_id_cap = TOKEN_ID_CAP.load(deps.storage)?;
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw404/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub const OWNED: Map<String, Vec<Uint128>> = Map::new("owned");
pub const OWNED_INDEX: Map<String, Uint128> = Map::new("owned_index");
pub const APPROVED_FOR_ALL: Map<(String, String), bool> = Map::new("approved_for_all");

/// @dev Tracks assignment flag, tokenId -> isAssigned
pub const ID_ASSIGNED: Map<String, bool> = Map::new("id_assigned");
/// @dev Tracks assignment flag, tokenId -> isUnassigned
pub const UNASSIGNED_IDS: Map<String, bool> = Map::new("unassigned_ids");

/// Additional features
/// @dev prevents being burnt due to transfers made in mistake
Expand Down
17 changes: 17 additions & 0 deletions scripts/e2e-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "e2e-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@injectivelabs/networks": "^1.14.6",
"@injectivelabs/sdk-ts": "^1.14.7",
"@injectivelabs/utils": "^1.14.6"
}
}
Loading

0 comments on commit 9fab2ac

Please sign in to comment.