Skip to content

Commit

Permalink
format account
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulRbl committed Nov 30, 2023
1 parent bac2186 commit 248ab5d
Showing 1 changed file with 73 additions and 31 deletions.
104 changes: 73 additions & 31 deletions packages/starknet/lib/src/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class Account {
}

/// Get Estimate max fee for Invoke Tx
Future<Felt> getEstimateMaxFeeForInvokeTx({BlockId blockId = BlockId.latest,
Future<Felt> getEstimateMaxFeeForInvokeTx({
BlockId blockId = BlockId.latest,
String version = "0x1",
required List<FunctionCall> functionCalls,
bool useLegacyCalldata = false,
required Felt nonce,
double feeMultiplier = 1.2,
}) async {

final signature = signer.signTransactions(
transactions: functionCalls,
contractAddress: accountAddress,
Expand All @@ -80,61 +80,83 @@ class Account {

if (version == "0x1") {
final calldata = functionCallsToCalldata(
functionCalls: functionCalls,
useLegacyCalldata: useLegacyCalldata,
);
broadcastedTxn = BroadcastedInvokeTxnV1(type: "INVOKE", maxFee: defaultMaxFee, version: version, signature: signature, nonce: nonce, senderAddress: accountAddress, calldata: calldata);
functionCalls: functionCalls,
useLegacyCalldata: useLegacyCalldata,
);
broadcastedTxn = BroadcastedInvokeTxnV1(
type: "INVOKE",
maxFee: defaultMaxFee,
version: version,
signature: signature,
nonce: nonce,
senderAddress: accountAddress,
calldata: calldata);
} else {
final calldata =
functionCallsToCalldataLegacy(functionCalls: functionCalls) +
[nonce];
broadcastedTxn = BroadcastedInvokeTxnV0(type: "INVOKE", maxFee: defaultMaxFee, version: version, signature: signature, nonce: nonce, contractAddress: accountAddress, entryPointSelector: getSelectorByName('__execute__'), calldata: calldata);
}
functionCallsToCalldataLegacy(functionCalls: functionCalls) + [nonce];
broadcastedTxn = BroadcastedInvokeTxnV0(
type: "INVOKE",
maxFee: defaultMaxFee,
version: version,
signature: signature,
nonce: nonce,
contractAddress: accountAddress,
entryPointSelector: getSelectorByName('__execute__'),
calldata: calldata);
}

final maxFee = await getMaxFeeFromBroadcastedTxn(broadcastedTxn, blockId, feeMultiplier);
final maxFee = await getMaxFeeFromBroadcastedTxn(
broadcastedTxn, blockId, feeMultiplier);

return maxFee;
return maxFee;
}

/// Get Estimate max fee for Declare Tx
Future<Felt> getEstimateMaxFeeForDeclareTx({BlockId blockId = BlockId.latest,
Future<Felt> getEstimateMaxFeeForDeclareTx({
BlockId blockId = BlockId.latest,
String version = "0x1",
required Felt nonce,
required ICompiledContract compiledContract,
double feeMultiplier = 1.2,
}) async {

BroadcastedTxn broadcastedTxn;

if ( compiledContract is DeprecatedCompiledContract) {
if (compiledContract is DeprecatedCompiledContract) {
final signature = signer.signDeclareTransactionV1(
compiledContract: compiledContract,
senderAddress: accountAddress,
chainId: chainId,
nonce: nonce,
);
broadcastedTxn = BroadcastedDeclareTxn(type: "DECLARE", maxFee: defaultMaxFee, version: version, signature: signature, nonce: nonce, contractClass: compiledContract.compress() , senderAddress: accountAddress);
}
else {
broadcastedTxn = BroadcastedDeclareTxn(
type: "DECLARE",
maxFee: defaultMaxFee,
version: version,
signature: signature,
nonce: nonce,
contractClass: compiledContract.compress(),
senderAddress: accountAddress);
} else {
// V2 of BroadcastedDeclareTxn is not supported yet
return defaultMaxFee;
}

final maxFee = await getMaxFeeFromBroadcastedTxn(broadcastedTxn, blockId, feeMultiplier);
final maxFee = await getMaxFeeFromBroadcastedTxn(
broadcastedTxn, blockId, feeMultiplier);

return maxFee;
return maxFee;
}

/// Get Estimate max fee for Deploy Tx
Future<Felt> getEstimateMaxFeeForDeployAccountTx({BlockId blockId = BlockId.latest,
Future<Felt> getEstimateMaxFeeForDeployAccountTx({
BlockId blockId = BlockId.latest,
String version = "0x1",
required Felt nonce,
required List<Felt> constructorCalldata,
required Felt contractAddressSalt,
required Felt classHash,
double feeMultiplier = 1.2,
}) async {

final signature = signer.signDeployAccountTransactionV1(
contractAddressSalt: contractAddressSalt,
classHash: classHash,
Expand All @@ -143,15 +165,25 @@ class Account {
nonce: nonce,
);

final broadcastedTxn = BroadcastedDeployAccountTxn(type: "DEPLOY_ACCOUNT", version: version, contractAddressSalt: contractAddressSalt, constructorCalldata: constructorCalldata, maxFee: defaultMaxFee, nonce: nonce, signature: signature, classHash: classHash);
final broadcastedTxn = BroadcastedDeployAccountTxn(
type: "DEPLOY_ACCOUNT",
version: version,
contractAddressSalt: contractAddressSalt,
constructorCalldata: constructorCalldata,
maxFee: defaultMaxFee,
nonce: nonce,
signature: signature,
classHash: classHash);

final maxFee = await getMaxFeeFromBroadcastedTxn(broadcastedTxn, blockId, feeMultiplier);
final maxFee = await getMaxFeeFromBroadcastedTxn(
broadcastedTxn, blockId, feeMultiplier);

return maxFee;
return maxFee;
}

Future<Felt> getMaxFeeFromBroadcastedTxn(BroadcastedTxn broadcastedTxn, BlockId blockId, double feeMultiplier) async {
EstimateFeeRequest estimateFeeRequest = EstimateFeeRequest(
Future<Felt> getMaxFeeFromBroadcastedTxn(BroadcastedTxn broadcastedTxn,
BlockId blockId, double feeMultiplier) async {
EstimateFeeRequest estimateFeeRequest = EstimateFeeRequest(
request: [broadcastedTxn],
blockId: blockId,
);
Expand All @@ -167,9 +199,10 @@ class Account {

final Felt overallFee = Felt.fromHexString(fee.overallFee);
//multiply by feeMultiplier
final Felt maxFee = Felt.fromDouble(overallFee.toBigInt().toDouble() * feeMultiplier);
final Felt maxFee =
Felt.fromDouble(overallFee.toBigInt().toDouble() * feeMultiplier);

return maxFee;
return maxFee;
}

/// Call account contract `__execute__` with given [functionCalls]
Expand All @@ -180,7 +213,14 @@ class Account {
Felt? nonce,
}) async {
nonce = nonce ?? await getNonce();
maxFee = maxFee ?? await getEstimateMaxFeeForInvokeTx(functionCalls: functionCalls, useLegacyCalldata: useLegacyCalldata, nonce: nonce, version: supportedTxVersion == AccountSupportedTxVersion.v1 ? "0x1" : "0x0");
maxFee = maxFee ??
await getEstimateMaxFeeForInvokeTx(
functionCalls: functionCalls,
useLegacyCalldata: useLegacyCalldata,
nonce: nonce,
version: supportedTxVersion == AccountSupportedTxVersion.v1
? "0x1"
: "0x0");

final signature = signer.signTransactions(
transactions: functionCalls,
Expand Down Expand Up @@ -241,7 +281,9 @@ class Account {
CASMCompiledContract? casmCompiledContract,
}) async {
nonce = nonce ?? await getNonce();
maxFee = maxFee ?? await getEstimateMaxFeeForDeclareTx(nonce: nonce, compiledContract: compiledContract);
maxFee = maxFee ??
await getEstimateMaxFeeForDeclareTx(
nonce: nonce, compiledContract: compiledContract);
if (compiledContract is DeprecatedCompiledContract) {
final signature = signer.signDeclareTransactionV1(
compiledContract: compiledContract,
Expand Down

0 comments on commit 248ab5d

Please sign in to comment.