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

WalletAccount: silent instantiation #1257

Closed
Closed
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
34 changes: 25 additions & 9 deletions src/wallet/account.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-underscore-dangle */
import type {
Signature,
AccountChangeEventHandler,
Expand All @@ -17,6 +19,8 @@ import {
ProviderOptions,
TypedData,
UniversalDeployerContractPayload,
type BlockIdentifier,
type Nonce,
} from '../types';
import { extractContractHashes } from '../utils/contract';
import { stringify } from '../utils/json';
Expand All @@ -38,8 +42,6 @@ import { StarknetChainId } from '../constants';

// Represent 'Selected Active' Account inside Connected Wallet
export class WalletAccount extends Account implements AccountInterface {
public address: string = '';

public walletProvider: StarknetWalletProvider;

constructor(
Expand All @@ -49,7 +51,6 @@ export class WalletAccount extends Account implements AccountInterface {
) {
super(providerOrOptions, '', '', cairoVersion); // At this point unknown address
this.walletProvider = walletProvider;

// Update Address on change
this.walletProvider.on('accountsChanged', (res) => {
if (!res) return;
Expand All @@ -63,18 +64,22 @@ export class WalletAccount extends Account implements AccountInterface {
// At the moment channel is stateless but it could change
this.channel.setChainId(res as StarknetChainId);
});
}

// Get and Set Address !!! Post constructor initial empty string
walletProvider
.request({
public async getAddress(): Promise<string> {
if (!this.address) {
console.log('SNJS-WA.getAddress: define WA address.');
const response = await this.walletProvider.request({
type: 'wallet_requestAccounts',
params: {
silent_mode: false,
},
})
.then((res) => {
this.address = res[0].toLowerCase();
});
console.log('SNJS-getAddress-A');
this.address = response[0].toLowerCase();
console.log('SNJS-getAddress-B');
}
return this.address;
}

/**
Expand Down Expand Up @@ -114,6 +119,16 @@ export class WalletAccount extends Account implements AccountInterface {
/**
* ACCOUNT METHODS
*/

override async getNonce(blockIdentifier?: BlockIdentifier): Promise<Nonce> {
await this.getAddress();
return super.getNonce(blockIdentifier);
}

// ************************
// TODO : override the 13 other account methods that uses the account address property (fortunately, all are async)
// ***********************

override execute(calls: AllowArray<Call>) {
const txCalls = [].concat(calls as any).map((it) => {
const { contractAddress, entrypoint, calldata } = it;
Expand Down Expand Up @@ -157,6 +172,7 @@ export class WalletAccount extends Account implements AccountInterface {
override async deploy(
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[]
): Promise<MultiDeployContractResponse> {
await this.getAddress();
const { calls, addresses } = buildUDCCall(payload, this.address);
const invokeResponse = await this.execute(calls);

Expand Down
2 changes: 1 addition & 1 deletion www/docs/guides/walletAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Using the `get-starknet-core` v4 library, you can create your own UI and logic t
So, you instantiate a new WalletAccount with :

```typescript
import { connect } from 'get-starknet'; // v4.0.0 min
import { connect } from '@starknet-io/get-starknet'; // v4.0.3 min
import { WalletAccount } from 'starknet'; // v6.10.0 min
const myFrontendProviderUrl = 'https://free-rpc.nethermind.io/sepolia-juno/v0_7';
// standard UI to select a wallet :
Expand Down