-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some refactor, mainly for types folder.
- Loading branch information
Showing
26 changed files
with
1,251 additions
and
975 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}, | ||
}); | ||
} | ||
} |
Oops, something went wrong.