Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring boilerplate Wallet class in core library #1276

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 85 additions & 5 deletions packages/core/src/lib/wallet-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ import type {
} from "./wallet-selector.types";
import { EventEmitter, Logger, WalletModules, Provider } from "./services";
import type { Wallet } from "./wallet";
import type { Store } from "./store.types";
import type { Store, WalletSelectorState } from "./store.types";
import type { NetworkId, Options } from "./options.types";
import type {
AccountView,
FinalExecutionOutcome,
RpcQueryRequest,
} from "near-api-js/lib/providers/provider";
import { providers } from "near-api-js";

let walletSelectorInstance: WalletSelector | null = null;

const createSelector = (
options: Options,
store: Store,
walletModules: WalletModules,
emitter: EventEmitter<WalletSelectorEvents>
emitter: EventEmitter<WalletSelectorEvents>,
provider: Provider
): WalletSelector => {
return {
options,
Expand Down Expand Up @@ -68,6 +75,77 @@ const createSelector = (
off: (eventName, callback) => {
emitter.off(eventName, callback);
},
async getSignedInAccountBalance() {
const accountId = this.getAccountId();

if (!accountId) {
throw new Error(`Not signed in`);
}

const request: RpcQueryRequest = {
request_type: "view_account",
account_id: accountId,
finality: "final",
};
const account = await provider.query<AccountView>(request);

return account.amount || "0";
},
subscribeOnAccountChange(onAccountChangeFn) {
this.store.observable.subscribe(async (state: WalletSelectorState) => {
const signedAccount = state?.accounts.find(
(account) => account.active
)?.accountId;

onAccountChangeFn(signedAccount || "");
});
},
async viewMethod(contractId, method, args) {
const request: RpcQueryRequest = {
request_type: "call_function",
account_id: contractId,
method_name: method,
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
finality: "optimistic",
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const response = await provider.query<any>(request);

return JSON.parse(Buffer.from(response.result).toString());
},
async callMethod(contractId, method, args, gas, deposit) {
const wallet = await this.wallet();

const outcome = await wallet.signAndSendTransaction({
receiverId: contractId,
actions: [
{
type: "FunctionCall",
params: {
methodName: method,
args,
gas,
deposit,
},
},
],
});

return providers.getTransactionLastResult(
outcome as FinalExecutionOutcome
);
},
getAccountId() {
const { accounts } = store.getState();

if (accounts.length === 0) {
return undefined;
}

const { accountId } = accounts.at(0)!;

return accountId;
},
};
};

Expand All @@ -94,27 +172,29 @@ export const setupWalletSelector = async (
? params.fallbackRpcUrls
: [network.nodeUrl];

const provider = new Provider(rpcProviderUrls);
const walletModules = new WalletModules({
factories: params.modules,
storage,
options,
store,
emitter,
provider: new Provider(rpcProviderUrls),
provider,
});

await walletModules.setup();

if (params.allowMultipleSelectors) {
return createSelector(options, store, walletModules, emitter);
return createSelector(options, store, walletModules, emitter, provider);
}

if (!walletSelectorInstance) {
walletSelectorInstance = createSelector(
options,
store,
walletModules,
emitter
emitter,
provider
);
}

Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/lib/wallet-selector.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,39 @@ export interface WalletSelector {
eventName: EventName,
callback: (event: WalletSelectorEvents[EventName]) => void
): void;

/**
* Sets up a callback function that triggers whenever the accountId is updated.
*/
subscribeOnAccountChange(onAccountChangeFn: (account: string) => void): void;
/**
* Executes a view function on a specified smart contract.
*/
viewMethod(
contractId: string,
method: string,
args: Record<string, unknown>
): Promise<unknown>;
/**
* Executes a mutable function on a specified smart contract.
* Requires to be signed in.
* @throws {Error} if a user isn't signed in
*/
callMethod(
contractId: string,
method: string,
args: Record<string, unknown>,
gas: string,
deposit: string
): Promise<unknown>;
/**
* Retrieves the account's balance in yoctoNear by querying the specified account's state.
* Requires to be signed in.
* @throws {Error} if a user isn't signed in
*/
getSignedInAccountBalance(): Promise<string>;
/**
* Retrieves signed in account's ID
*/
getAccountId(): string | undefined;
}
Loading