Skip to content

Commit dca1f12

Browse files
authored
Merge pull request #187 from hyperweb-io/query-client-for-unisigner
Query client for unisigner
2 parents ed4a30d + 9ffd035 commit dca1f12

File tree

18 files changed

+183
-23
lines changed

18 files changed

+183
-23
lines changed

docs/advanced/network-implementation-guide.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,14 @@ The signer interface provides a consistent API across different networks:
456456

457457
```typescript
458458
// Universal signer interface
459-
interface IUniSigner<TAccount, TSignArgs, TBroadcastOpts, TBroadcastResponse> {
459+
interface IUniSigner<
460+
TAccount,
461+
TSignArgs,
462+
TBroadcastOpts,
463+
TBroadcastResponse,
464+
TQueryClient extends IQueryClient = IQueryClient
465+
> {
466+
queryClient: TQueryClient;
460467
// Account management
461468
getAccounts(): Promise<readonly TAccount[]>;
462469

@@ -476,10 +483,10 @@ interface ISigned<TBroadcastOpts, TBroadcastResponse> {
476483
}
477484

478485
// Network-specific signer implementation
479-
class NetworkSigner implements IUniSigner<NetworkAccount, NetworkSignArgs, NetworkBroadcastOpts, NetworkBroadcastResponse> {
486+
class NetworkSigner implements IUniSigner<NetworkAccount, NetworkSignArgs, NetworkBroadcastOpts, NetworkBroadcastResponse, IQueryClient> {
480487
constructor(
481488
private wallet: IWallet,
482-
private queryClient: IQueryClient,
489+
public readonly queryClient: IQueryClient,
483490
private config: NetworkSignerConfig
484491
) {}
485492

docs/advanced/signer.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ export interface IUniSigner<
161161
TSignArgs = unknown,
162162
TBroadcastOpts = unknown,
163163
TBroadcastResponse extends IBroadcastResult<TTxResp> = IBroadcastResult<TTxResp>,
164+
TQueryClient extends IQueryClient = IQueryClient,
164165
> {
166+
// Query interface used for chain reads and broadcasting
167+
queryClient: TQueryClient;
165168
// Account management
166169
getAccounts(): Promise<readonly TAccount[]>;
167170

docs/advanced/tutorial.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ interface IUniSigner<
5757
TSignArgs = unknown,
5858
TBroadcastOpts = unknown,
5959
TBroadcastResponse extends IBroadcastResult<TTxResp> = IBroadcastResult<TTxResp>,
60+
TQueryClient extends IQueryClient = IQueryClient,
6061
> {
62+
// Query interface used for chain reads and broadcasting
63+
queryClient: TQueryClient;
6164
// Account management
6265
getAccounts(): Promise<readonly TAccount[]>;
6366

docs/networks/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"cosmos": "Cosmos",
33
"ethereum": "Ethereum",
4-
"injective": "Injective"
4+
"injective": "Injective",
5+
"solana": "Solana"
56
}

docs/networks/ethereum/index.mdx

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,75 @@ console.log("Endpoint:", queryClient.endpoint);
230230
await queryClient.disconnect();
231231
```
232232

233+
### Frontend: window.ethereum (Browser Wallet)
234+
235+
Use the injected EIP-1193 provider (e.g., MetaMask) to send ETH and execute contract functions by ABI.
236+
237+
```typescript
238+
import { SignerFromBrowser } from "@interchainjs/ethereum";
239+
240+
// Initialize with injected provider
241+
const signer = new SignerFromBrowser(window.ethereum);
242+
243+
// Request accounts (prompts the wallet)
244+
const [address] = await signer.getAddresses(true);
245+
console.log("Connected address:", address);
246+
247+
// 1) Send ETH
248+
const { transactionHash, wait } = await signer.send({
249+
to: "0xRecipient...",
250+
// value accepts bigint | number | string; converted to 0x hex automatically
251+
value: 1000000000000000n // 0.001 ETH
252+
});
253+
console.log("ETH tx:", transactionHash);
254+
const receipt = await wait();
255+
console.log("ETH receipt:", receipt.status);
256+
257+
// 2) Read contract function (by ABI)
258+
// Example: ERC20 balanceOf(address)
259+
const erc20Abi = [
260+
{
261+
name: "balanceOf",
262+
type: "function",
263+
inputs: [{ name: "owner", type: "address" }],
264+
outputs: [{ name: "balance", type: "uint256" }]
265+
}
266+
];
267+
const tokenAddress = "0xToken...";
268+
const balance = await signer.readContract<bigint>({
269+
address: tokenAddress,
270+
abi: erc20Abi,
271+
functionName: "balanceOf",
272+
params: [address]
273+
});
274+
console.log("Token balance:", balance.toString());
275+
276+
// 3) Write contract function (by ABI)
277+
// Example: ERC20 transfer(address,uint256) returns (bool)
278+
const transferAbi = [
279+
{
280+
name: "transfer",
281+
type: "function",
282+
inputs: [
283+
{ name: "to", type: "address" },
284+
{ name: "amount", type: "uint256" }
285+
],
286+
outputs: [{ name: "ok", type: "bool" }]
287+
}
288+
];
289+
const { transactionHash: txHash2, wait: wait2 } = await signer.writeContract({
290+
address: tokenAddress,
291+
abi: transferAbi,
292+
functionName: "transfer",
293+
params: ["0xRecipient...", 1000000000000000000n] // 1 token (18 decimals)
294+
// Optional gas options:
295+
// gas, gasPrice, maxFeePerGas, maxPriorityFeePerGas, nonce, chainId
296+
});
297+
console.log("transfer tx:", txHash2);
298+
const transferReceipt = await wait2();
299+
console.log("transfer status:", transferReceipt.status);
300+
```
301+
233302
### Using Ethereum Signers
234303

235304
#### Creating Signers
@@ -466,7 +535,7 @@ console.log(toChecksumAddress(lower));
466535
### Legacy Support
467536

468537
- **SignerFromPrivateKey**: Original implementation (maintained for backward compatibility)
469-
- **SignerFromBrowser**: Browser wallet integration (maintained for backward compatibility)
538+
- **SignerFromBrowser**: Browser wallet integration via `window.ethereum` (EIP-1193)
470539

471540
## For Developers
472541

docs/networks/solana/_meta.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"index": "Overview"
3+
}

docs/networks/solana/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Solana Chain

docs/packages/types/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ interface IUniSigner<
5555
TSignArgs = unknown,
5656
TBroadcastOpts = unknown,
5757
TBroadcastResponse extends IBroadcastResult<TTxResp> = IBroadcastResult<TTxResp>,
58+
TQueryClient extends IQueryClient = IQueryClient,
5859
> {
60+
queryClient: TQueryClient;
5961
getAccounts(): Promise<readonly TAccount[]>;
6062
signArbitrary(data: Uint8Array, index?: number): Promise<ICryptoBytes>;
6163
sign(args: TSignArgs): Promise<ISigned<TBroadcastOpts, TBroadcastResponse>>;

networks/cosmos/src/signers/base-signer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export abstract class BaseCosmosSigner implements ICosmosSigner, ISigningClient
4343
this.config.queryClient = originalQueryClient;
4444
}
4545

46+
get queryClient() {
47+
return this.config.queryClient;
48+
}
49+
4650
// ICosmosSigner interface methods
4751
async getAccounts(): Promise<readonly AccountData[]> {
4852
if (isOfflineAminoSigner(this.auth) || isOfflineDirectSigner(this.auth)) {
@@ -319,7 +323,7 @@ export abstract class BaseCosmosSigner implements ICosmosSigner, ISigningClient
319323
const tx = Tx.fromPartial({
320324
body: txBody,
321325
authInfo: authInfo,
322-
signatures: new Uint8Array([0]), // Empty signatures for simulation
326+
signatures: [new Uint8Array(0)], // Empty signatures for simulation
323327
});
324328

325329
// Encode transaction to bytes

networks/cosmos/src/signers/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ export interface ICosmosSigner extends IUniSigner<
155155
AccountData, // account type
156156
CosmosSignArgs, // sign args
157157
CosmosBroadcastOptions, // broadcast options
158-
CosmosBroadcastResponse // broadcast response
158+
CosmosBroadcastResponse, // broadcast response
159+
ICosmosQueryClient // query client
159160
> {
160161
getAddresses(): Promise<string[]>;
161162
getChainId(): Promise<string>;
@@ -268,4 +269,4 @@ export type TxOptions = {
268269
// Document types
269270
export type CosmosDirectDoc = SignDoc;
270271
export type CosmosAminoDoc = StdSignDoc;
271-
export type CosmosTx = TxRaw;
272+
export type CosmosTx = TxRaw;

0 commit comments

Comments
 (0)