Skip to content

Commit b202f37

Browse files
committed
feat: implement new interface on account class
1 parent dad1eea commit b202f37

File tree

18 files changed

+445
-1476
lines changed

18 files changed

+445
-1476
lines changed

src/account/default.ts

Lines changed: 23 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1-
import assert from 'minimalistic-assert';
2-
31
import { ZERO } from '../constants';
4-
import { Provider, ProviderOptions } from '../provider';
2+
import { ProviderOptions } from '../provider';
3+
import { Provider } from '../provider/default';
54
import { Signer, SignerInterface } from '../signer';
65
import {
76
Abi,
8-
AddTransactionResponse,
97
Call,
108
InvocationsDetails,
119
InvocationsSignerDetails,
12-
InvokeFunctionTransaction,
10+
InvokeFunctionResponse,
1311
KeyPair,
1412
Signature,
15-
Transaction,
1613
} from '../types';
1714
import { EstimateFee, EstimateFeeDetails } from '../types/account';
18-
import { sign } from '../utils/ellipticCurve';
19-
import {
20-
computeHashOnElements,
21-
feeTransactionVersion,
22-
getSelectorFromName,
23-
transactionVersion,
24-
} from '../utils/hash';
25-
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
26-
import { encodeShortString } from '../utils/shortString';
15+
import { feeTransactionVersion, transactionVersion } from '../utils/hash';
16+
import { BigNumberish, toBN, toHex } from '../utils/number';
2717
import { compileCalldata, estimatedFeeToMaxFee } from '../utils/stark';
2818
import { fromCallsToExecuteCalldataWithNonce } from '../utils/transaction';
2919
import { TypedData, getMessageHash } from '../utils/typedData';
@@ -48,7 +38,8 @@ export class Account extends Provider implements AccountInterface {
4838
public async getNonce(): Promise<string> {
4939
const { result } = await this.callContract({
5040
contractAddress: this.address,
51-
entrypoint: 'get_nonce',
41+
entryPointSelector: 'get_nonce',
42+
calldata: [],
5243
});
5344
return toHex(toBN(result[0]));
5445
}
@@ -72,18 +63,12 @@ export class Account extends Provider implements AccountInterface {
7263
const signature = await this.signer.signTransaction(transactions, signerDetails);
7364

7465
const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
75-
const fetchedEstimate = await this.fetchEndpoint(
76-
'estimate_fee',
77-
{ blockIdentifier },
78-
{
79-
contract_address: this.address,
80-
entry_point_selector: getSelectorFromName('__execute__'),
81-
calldata,
82-
version: toHex(version),
83-
signature: bigNumberishArrayToDecimalStringArray(signature),
84-
}
66+
const fetchedEstimate = await super.getEstimateFee(
67+
{ contractAddress: this.address, entryPointSelector: '__execute__', calldata, signature },
68+
blockIdentifier
8569
);
86-
const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.amount);
70+
71+
const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.overallFee);
8772

8873
return {
8974
...fetchedEstimate,
@@ -105,7 +90,7 @@ export class Account extends Provider implements AccountInterface {
10590
calls: Call | Call[],
10691
abis: Abi[] | undefined = undefined,
10792
transactionsDetail: InvocationsDetails = {}
108-
): Promise<AddTransactionResponse> {
93+
): Promise<InvokeFunctionResponse> {
10994
const transactions = Array.isArray(calls) ? calls : [calls];
11095
const nonce = toBN(transactionsDetail.nonce ?? (await this.getNonce()));
11196
let maxFee: BigNumberish = '0';
@@ -116,124 +101,27 @@ export class Account extends Provider implements AccountInterface {
116101
maxFee = suggestedMaxFee.toString();
117102
}
118103

104+
const version = toBN(transactionVersion);
105+
119106
const signerDetails: InvocationsSignerDetails = {
120107
walletAddress: this.address,
121108
nonce,
122109
maxFee,
123-
version: toBN(transactionVersion),
110+
version,
124111
chainId: this.chainId,
125112
};
126113

127114
const signature = await this.signer.signTransaction(transactions, signerDetails, abis);
128115

129116
const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
130-
return this.fetchEndpoint('add_transaction', undefined, {
131-
type: 'INVOKE_FUNCTION',
132-
contract_address: this.address,
133-
entry_point_selector: getSelectorFromName('__execute__'),
134-
calldata,
135-
signature: bigNumberishArrayToDecimalStringArray(signature),
136-
max_fee: toHex(toBN(maxFee)),
137-
});
138-
}
139117

140-
/**
141-
* Temporary method to allow dapps on starknet.js v2 to work with Argent X v3
142-
* @deprecated to remove ASAP
143-
*/
144-
public async LEGACY_addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
145-
if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
146-
if (transaction.type === 'DECLARE') throw new Error('No DECLARES');
147-
148-
assert(
149-
!transaction.signature,
150-
"Adding signatures to a signer transaction currently isn't supported"
151-
);
152-
153-
let nonceBn;
154-
if (transaction.nonce) {
155-
nonceBn = toBN(transaction.nonce);
156-
} else {
157-
const { result } = await this.callContract({
158-
contractAddress: this.address,
159-
entrypoint: 'get_nonce',
160-
});
161-
nonceBn = toBN(result[0]);
162-
}
163-
164-
function hashMulticall(
165-
account: string,
166-
transactions: InvokeFunctionTransaction[],
167-
nonce: string,
168-
maxFee: string
169-
) {
170-
const hashArray = transactions
171-
.map(({ contract_address, entry_point_selector, calldata }) => [
172-
contract_address,
173-
entry_point_selector,
174-
computeHashOnElements(calldata || []),
175-
])
176-
.map(bigNumberishArrayToDecimalStringArray)
177-
.map(computeHashOnElements);
178-
179-
return computeHashOnElements([
180-
encodeShortString('StarkNet Transaction'),
181-
account,
182-
computeHashOnElements(hashArray),
183-
nonce,
118+
return this.invokeFunction(
119+
{ contractAddress: this.address, entrypoint: '__execute__', calldata, signature },
120+
{
184121
maxFee,
185-
transactionVersion,
186-
]);
187-
}
188-
const msgHash = hashMulticall(this.address, [transaction], nonceBn.toString(), '0');
189-
if (!('keyPair' in this.signer)) {
190-
throw new Error('No keyPair');
191-
}
192-
const signature = sign((this.signer as any).keyPair, msgHash);
193-
194-
const transformCallsToMulticallArrays = (calls: InvokeFunctionTransaction[]) => {
195-
const callArray: any[] = [];
196-
const calldata: BigNumberish[] = [];
197-
calls.forEach((call) => {
198-
const data = call.calldata || [];
199-
callArray.push({
200-
to: toBN(call.contract_address).toString(10),
201-
selector: toBN(call.entry_point_selector).toString(10),
202-
data_offset: calldata.length.toString(),
203-
data_len: data.length.toString(),
204-
});
205-
calldata.push(...data);
206-
});
207-
return {
208-
callArray,
209-
calldata: bigNumberishArrayToDecimalStringArray(calldata),
210-
};
211-
};
212-
213-
const fromCallsToExecuteCalldata2 = (calls: InvokeFunctionTransaction[]): string[] => {
214-
const { callArray, calldata } = transformCallsToMulticallArrays(calls);
215-
return [
216-
callArray.length.toString(),
217-
...callArray
218-
.map(
219-
({ to, selector, data_offset, data_len }) =>
220-
[to, selector, data_offset, data_len] as string[]
221-
)
222-
.flat(),
223-
calldata.length.toString(),
224-
...calldata,
225-
];
226-
};
227-
228-
const calldata = [...fromCallsToExecuteCalldata2([transaction]), nonceBn.toString()];
229-
230-
return this.fetchEndpoint('add_transaction', undefined, {
231-
type: 'INVOKE_FUNCTION',
232-
contract_address: this.address,
233-
entry_point_selector: getSelectorFromName('__execute__'),
234-
calldata,
235-
signature: bigNumberishArrayToDecimalStringArray(signature),
236-
});
122+
version,
123+
}
124+
);
237125
}
238126

239127
/**
@@ -271,7 +159,7 @@ export class Account extends Provider implements AccountInterface {
271159
try {
272160
await this.callContract({
273161
contractAddress: this.address,
274-
entrypoint: 'is_valid_signature',
162+
entryPointSelector: 'is_valid_signature',
275163
calldata: compileCalldata({
276164
hash: toBN(hash).toString(),
277165
signature: signature.map((x) => toBN(x).toString()),

src/account/interface.ts

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { ProviderInterface } from '../provider';
22
import { SignerInterface } from '../signer';
3-
import {
4-
Abi,
5-
AddTransactionResponse,
6-
Call,
7-
DeployContractPayload,
8-
InvocationsDetails,
9-
Signature,
10-
} from '../types';
11-
import { EstimateFee, EstimateFeeDetails } from '../types/account';
3+
import { Abi, Call, InvocationsDetails, InvokeFunctionResponse, Signature } from '../types';
124
import { BigNumberish } from '../utils/number';
135
import { TypedData } from '../utils/typedData/types';
146

@@ -17,37 +9,6 @@ export abstract class AccountInterface extends ProviderInterface {
179

1810
public abstract signer: SignerInterface;
1911

20-
/**
21-
* Deploys a given compiled contract (json) to starknet
22-
*
23-
* @param payload payload to be deployed containing:
24-
* - compiled contract code
25-
* - constructor calldata
26-
* - address salt
27-
* @param abi the abi of the contract
28-
* @returns a confirmation of sending a transaction on the starknet contract
29-
*/
30-
public abstract override deployContract(
31-
payload: DeployContractPayload,
32-
abi?: Abi
33-
): Promise<AddTransactionResponse>;
34-
35-
/**
36-
* Estimate Fee for a method on starknet
37-
*
38-
* @param invocation the invocation object containing:
39-
* - contractAddress - the address of the contract
40-
* - entrypoint - the entrypoint of the contract
41-
* - calldata - (defaults to []) the calldata
42-
* - signature - (defaults to []) the signature
43-
*
44-
* @returns response from addTransaction
45-
*/
46-
public abstract estimateFee(
47-
calls: Call | Call[],
48-
estimateFeeDetails?: EstimateFeeDetails
49-
): Promise<EstimateFee>;
50-
5112
/**
5213
* Invoke execute function in account contract
5314
*
@@ -64,7 +25,7 @@ export abstract class AccountInterface extends ProviderInterface {
6425
transactions: Call | Call[],
6526
abis?: Abi[],
6627
transactionsDetail?: InvocationsDetails
67-
): Promise<AddTransactionResponse>;
28+
): Promise<InvokeFunctionResponse>;
6829

6930
/**
7031
* Sign an JSON object for off-chain usage with the starknet private key and return the signature

0 commit comments

Comments
 (0)