Skip to content

Commit

Permalink
add query by creator
Browse files Browse the repository at this point in the history
  • Loading branch information
liangping committed Aug 24, 2023
1 parent 909fed3 commit 48adbf4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/modules/[chain]/cosmwasm/WasmStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface WasmRequestRegistry extends AbstractRegistry {
cosmwasm_contract_address_raw_query_data: Request<any>;
cosmwasm_contract_address_smart_query_data: Request<any>;
cosmwasm_contract_address_state: Request<PaginabledContractStates>;
cosmwasm_wasm_contracts_creator: Request<PaginabledContracts>;
}

export const DEFAULT: WasmRequestRegistry = {
Expand Down Expand Up @@ -57,6 +58,10 @@ export const DEFAULT: WasmRequestRegistry = {
url: '/cosmwasm/wasm/v1/contract/{address}/state',
adapter,
},
cosmwasm_wasm_contracts_creator: {
url: '/cosmwasm/wasm/v1/contracts/creator/{creator_address}',
adapter,
},
};

class WasmRestClient extends BaseRestClient<WasmRequestRegistry> {
Expand All @@ -79,6 +84,11 @@ class WasmRestClient extends BaseRestClient<WasmRequestRegistry> {
getWasmContracts(address: string) {
return this.request(this.registry.cosmwasm_contract_address, { address });
}
getWasmContractsByCreator(creator_address: string, page?: PageRequest) {
if(!page) page = new PageRequest()
const query = `?${page.toQueryString()}`
return this.request(this.registry.cosmwasm_wasm_contracts_creator, { creator_address }, query);
}
getWasmContractHistory(address: string) {
return this.request(this.registry.cosmwasm_contract_address_history, {
address,
Expand Down
17 changes: 14 additions & 3 deletions src/modules/[chain]/cosmwasm/[code_id]/contracts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ const wasmStore = useWasmStore();
function loadContract(pageNum: number) {
const pr = new PageRequest();
pr.setPage(pageNum);
wasmStore.wasmClient.getWasmCodeContracts(props.code_id, pr).then((x) => {
response.value = x;
});
if(String(props.code_id).search(/^[\d]+$/) > -1){
// query with code id
wasmStore.wasmClient.getWasmCodeContracts(props.code_id, pr).then((x) => {
response.value = x;
})
} else {
// query by creator
wasmStore.wasmClient.getWasmContractsByCreator(props.code_id, pr).then((x) => {
response.value = {
contracts: x.contract_addresses,
pagination: x.pagination,
};
})
}
}
loadContract(1);
Expand Down
11 changes: 11 additions & 0 deletions src/modules/[chain]/cosmwasm/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ref } from 'vue';
import type { PaginabledCodeInfos } from './types';
import { PageRequest } from '@/types';
import PaginationBar from '@/components/PaginationBar.vue';
import router from '@/router';
const props = defineProps(['chain']);
Expand All @@ -13,6 +14,7 @@ const codes = ref({} as PaginabledCodeInfos);
const pageRequest = ref(new PageRequest())
const wasmStore = useWasmStore();
const dialog = useTxDialog()
const creator = ref("")
function pageload(pageNum: number) {
pageRequest.value.setPage(pageNum)
Expand All @@ -21,10 +23,19 @@ function pageload(pageNum: number) {
});
}
pageload(1)
function myContracts() {
router.push(`/${props.chain}/cosmwasm/${creator.value}/contracts`)
}
</script>
<template>
<div class="bg-base-100 px-4 pt-3 pb-4 rounded mb-4 shadow">
<h2 class="card-title truncate w-full">{{ $t('cosmwasm.title') }}</h2>

<div class="join border border-primary">
<input v-model="creator" type=text class="input input-bordered w-40 join-item" placeholder="creator address" />
<button class="join-item btn btn-primary" @click="myContracts()">{{ $t('cosmwasm.btn_query') }}</button>
</div>
<div class="overflow-x-auto">
<table class="table table-compact w-full mt-4 text-sm">
<thead>
Expand Down
5 changes: 4 additions & 1 deletion src/modules/[chain]/cosmwasm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ export interface PaginabledCodeInfos extends PaginatedResponse {
code_infos: CodeInfo[];
}
export interface PaginabledContracts extends PaginatedResponse {
contracts: string[];
// return type of cosmwasm_code_id_contracts
contracts?: string[];
// return type of cosmwasm_wasm_contracts_creator
contract_addresses?: string[];
}

0 comments on commit 48adbf4

Please sign in to comment.