IAgent.getAgentIds
getAgentIds(
address wallet,
PageRequest pageRequest
) external view returns (uint256[] agentIds, PageResponse pageResponse) 지갑에 등록된 agent ID를 페이지네이션으로 반환하는 역방향 조회입니다. IdentityRegistry 스토리지를 컨트랙트 내부에서 순회하는 비용이 너무 크기 때문에 Agent 프리컴파일이 이 메서드를 제공하며, 프리컴파일은 동기화되어 유지되는 체인 측 인덱스에서 읽습니다. 등록, attestation, 폐지, 메타데이터 등 그 외의 작업은 0x8004000000000000000000000000000000000001의 ERC-8004 IdentityRegistry preinstall을 직접 호출합니다.
파라미터
| 이름 | 타입 | 필수 | 설명 |
|---|---|---|---|
wallet | address | ✓ | 조회할 지갑 주소입니다. 이 자리에 주소가 아닌 값이 ABI 디코딩되어 들어오면 InvalidAddress(string bad)로 revert되며, 문제가 된 값을 디버그용 문자열로 함께 전달합니다. |
pageRequest | tuple | ✓ | 표준 페이지네이션 튜플로 (bytes key, uint64 offset, uint64 limit, bool countTotal, bool reverse) 형식입니다. 튜플 형식이 잘못되면 InvalidPageRequest(string method, uint256 index, string value)로 revert됩니다. |
반환값
타입:
tuple (uint256[] agentIds, PageResponse pageResponse)를 반환합니다. 마지막 페이지에 도달하면 pageResponse.nextKey가 비어 있습니다.
에러
| 코드 | 이름 | 설명 |
|---|---|---|
InvalidNumberOfArgs | InvalidNumberOfArgs | ABI 디코딩된 인자 개수가 이 메서드가 요구하는 두 개와 다를 때 revert됩니다. InvalidNumberOfArgs(uint256 expected, uint256 got) 형태로 인코딩됩니다. |
InvalidAddress | InvalidAddress | 첫 번째 인자가 address로 ABI 디코딩되지 않을 때 revert됩니다. InvalidAddress(string bad) 형태로 인코딩되며, bad에는 문제가 된 값을 디버그용 문자열로 담습니다. |
InvalidPageRequest | InvalidPageRequest | 두 번째 인자가 기대하는 PageRequest 튜플로 ABI 디코딩되지 않을 때 revert됩니다. InvalidPageRequest(string method, uint256 index, string value) 형태로 인코딩됩니다. |
QueryFailed | QueryFailed | 체인 측 인덱스 조회가 실패할 때 revert됩니다. QueryFailed(string queryMethod, string reason) 형태로 인코딩됩니다. |
UnknownMethod | UnknownMethod | calldata가 이 프리컴파일에서 인식하는 메서드 selector와 일치하지 않을 때 revert됩니다. UnknownMethod(string methodName) 형태로 인코딩됩니다. |
예제
지갑의 모든 agent ID 페이지네이션
nextKey가 비게 될 때까지 반복하여 전체 목록을 수집합니다.
import { createPublicClient, http } from "viem";
const AGENT_PRECOMPILE = "0x100000000000000000000000000000000000000A" as const;
const agentAbi = [{
type: "function", name: "getAgentIds", stateMutability: "view",
inputs: [
{ name: "wallet", type: "address" },
{ name: "pageRequest", type: "tuple", components: [
{ name: "key", type: "bytes" },
{ name: "offset", type: "uint64" },
{ name: "limit", type: "uint64" },
{ name: "countTotal", type: "bool" },
{ name: "reverse", type: "bool" },
]},
],
outputs: [
{ name: "agentIds", type: "uint256[]" },
{ name: "pageResponse", type: "tuple", components: [
{ name: "nextKey", type: "bytes" },
{ name: "total", type: "uint64" },
]},
],
}] as const;
const publicClient = createPublicClient({ transport: http("https://rpc-testnet.maroo.io") });
// TODO: replace with the real owner wallet before production.
const wallet = "0x8F3ac2B1d9E74c05A6B18FE27Dc4913e5A0F7b62";
let key: `0x${string}` = "0x";
const allIds: bigint[] = [];
do {
const [ids, page] = await publicClient.readContract({
address: AGENT_PRECOMPILE,
abi: agentAbi,
functionName: "getAgentIds",
args: [wallet, { key, offset: 0n, limit: 100n, countTotal: false, reverse: false }],
});
allIds.push(...ids);
key = page.nextKey as `0x${string}`;
} while (key !== "0x"); 공유 IPrecompile 오류 디코드
오류 형태가 바뀌었습니다. 예전의 별도 정의 InvalidArgsLength(method, got, want)는 사라졌고, InvalidAddress와 InvalidPageRequest는 이제 공유 IPrecompile 시그니처를 사용합니다.
import { decodeErrorResult } from "viem";
const iPrecompileErrorsAbi = [
{ type: "error", name: "InvalidNumberOfArgs",
inputs: [{ name: "expected", type: "uint256" }, { name: "got", type: "uint256" }] },
{ type: "error", name: "InvalidAddress", inputs: [{ name: "bad", type: "string" }] },
{ type: "error", name: "InvalidPageRequest",
inputs: [
{ name: "method", type: "string" },
{ name: "index", type: "uint256" },
{ name: "value", type: "string" },
] },
{ type: "error", name: "QueryFailed",
inputs: [{ name: "queryMethod", type: "string" }, { name: "reason", type: "string" }] },
] as const;
try {
await publicClient.readContract({
address: AGENT_PRECOMPILE,
abi: agentAbi,
functionName: "getAgentIds",
args: [wallet, { key: "0x", offset: 0n, limit: 100n, countTotal: false, reverse: false }],
});
} catch (err: any) {
if (err?.data) {
const decoded = decodeErrorResult({ abi: iPrecompileErrorsAbi, data: err.data });
console.error("getAgentIds reverted:", decoded.errorName, decoded.args);
} else {
throw err;
}
}