Skip to content

Commit

Permalink
Some refactor, mainly for types folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQZQ committed Jul 12, 2023
1 parent 0d4f585 commit 907a914
Show file tree
Hide file tree
Showing 26 changed files with 1,251 additions and 975 deletions.
453 changes: 230 additions & 223 deletions src/core/Client.ts

Large diffs are not rendered by default.

112 changes: 57 additions & 55 deletions src/core/WalletClient.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,67 @@
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { AptosClient, Types } from "aptos";
import { createEntryPayload } from "./createEntryPayload.js";
import type { ABIRoot, ABIWalletClient, EntryOptions, EntryPayload } from "../types/index.js";
import type { TransactionResponse } from "../types/common.js";
import { useWallet } from '@aptos-labs/wallet-adapter-react';
import { AptosClient, Types } from 'aptos';
import { createEntryPayload } from './createEntryPayload.js';
import type {
ABIRoot,
ABIWalletClient,
EntryOptions,
EntryPayload,
TransactionResponse,
} from '../types/index.js';

type Wallet = ReturnType<typeof useWallet>;

export class WalletClient {
private wallet: Wallet;
private client: AptosClient;
private wallet: Wallet;
private client: AptosClient;

constructor({ wallet, nodeUrl }: {
wallet: Wallet,
nodeUrl: string
}) {
this.wallet = wallet;
this.client = new AptosClient(nodeUrl);
}
constructor({ wallet, nodeUrl }: { wallet: Wallet; nodeUrl: string }) {
this.wallet = wallet;
this.client = new AptosClient(nodeUrl);
}

public async submitTransaction(
payload: EntryPayload,
_: EntryOptions | undefined = undefined
): Promise<TransactionResponse> {
const request = payload.rawPayload;
public async submitTransaction(
payload: EntryPayload,
_: EntryOptions | undefined = undefined,
): Promise<TransactionResponse> {
const request = payload.rawPayload;

// TODO: use the BCS API instead
const { hash } = await this.wallet.signAndSubmitTransaction({
type: "entry_function_payload",
...request,
arguments: request.arguments.map((arg: any) => {
if (Array.isArray(arg)) {
// TODO: support nested array, or use the BCS API instead
return arg.map((item: any) => item.toString());
} else if (typeof arg === "object") {
throw new Error(`a value of struct type: ${arg} is not supported`);
} else {
return arg.toString();
}
}),
});
// TODO: use the BCS API instead
const { hash } = await this.wallet.signAndSubmitTransaction({
type: 'entry_function_payload',
...request,
arguments: request.arguments.map((arg: any) => {
if (Array.isArray(arg)) {
// TODO: support nested array, or use the BCS API instead
return arg.map((item: any) => item.toString());
} else if (typeof arg === 'object') {
throw new Error(`a value of struct type: ${arg} is not supported`);
} else {
return arg.toString();
}
}),
});

const result = (await this.client.waitForTransactionWithResult(hash, {
checkSuccess: true,
})) as Types.Transaction_UserTransaction;
const result = (await this.client.waitForTransactionWithResult(hash, {
checkSuccess: true,
})) as Types.Transaction_UserTransaction;

return result;
}
return result;
}

public useABI<T extends ABIRoot>(abi: T) {
return new Proxy({} as ABIWalletClient<T>, {
get: (_, prop) => {
const functionName = prop.toString();
return (...args) => {
const payload = createEntryPayload(abi, {
function: functionName,
type_arguments: args[0].type_arguments,
arguments: args[0].arguments,
});
return this.submitTransaction(payload);
};
}
});
}
}
public useABI<T extends ABIRoot>(abi: T) {
return new Proxy({} as ABIWalletClient<T>, {
get: (_, prop) => {
const functionName = prop.toString();
return (...args) => {
const payload = createEntryPayload(abi, {
function: functionName,
type_arguments: args[0].type_arguments,
arguments: args[0].arguments,
});
return this.submitTransaction(payload);
};
},
});
}
}
Loading

0 comments on commit 907a914

Please sign in to comment.