Skip to content

Commit

Permalink
Switch to underlying RPC provider
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Dec 13, 2023
1 parent 932a77f commit d19c8d2
Show file tree
Hide file tree
Showing 32 changed files with 707 additions and 1,020 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"CAIRO_LANG": "0.11.2",
"STARKNET_DEVNET": "0.6.3",
"STARKNET_DEVNET": "78527decb3f76c4c808fa35f46228557af3df385",
"CAIRO_COMPILER": "2.2.0",
"SCARB_VERSION": "0.7.0"
}
1,332 changes: 503 additions & 829 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"form-data": "^4.0.0",
"glob": "^10.0.0",
"shelljs": "^0.8.5",
"starknet": "~5.19.3",
"starknet": "~5.24.5",
"tar-fs": "^3.0.4"
},
"peerDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions scripts/devnet-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ if [[ -n "${STARKNET_HARDHAT_DEV:-}" ]]; then
fi

STARKNET_DEVNET="${STARKNET_DEVNET:=$STARKNET_DEVNET_DEFAULT}"
docker pull -q shardlabs/starknet-devnet:$STARKNET_DEVNET
container_id=$(docker run --rm --name starknet_hardhat_devnet -d -p 0.0.0.0:$PORT:$PORT shardlabs/starknet-devnet --seed 42)
docker pull -q shardlabs/starknet-devnet-rs:$STARKNET_DEVNET
container_id=$(docker run --rm --name starknet_hardhat_devnet -d -p 0.0.0.0:$PORT:$PORT shardlabs/starknet-devnet-rs --seed 0)
echo "Running devnet in container starknet_hardhat_devnet $container_id"

else
starknet-devnet --host $HOST --port $PORT --seed 42 >/dev/null 2>&1 &
starknet-devnet --host $HOST --port $PORT --seed 0 >/dev/null 2>&1 &
echo "Spawned devnet with PID $!"
fi

Expand Down
4 changes: 2 additions & 2 deletions scripts/set-devnet-vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

set -u

export OZ_ACCOUNT_ADDRESS=0x347be35996a21f6bf0623e75dbce52baba918ad5ae8d83b6f416045ab22961a
export OZ_ACCOUNT_PRIVATE_KEY=0xbdd640fb06671ad11c80317fa3b1799d
export OZ_ACCOUNT_ADDRESS=0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691
export OZ_ACCOUNT_PRIVATE_KEY=0x71d7bb07b9a64f6f78ac4c816aff4da9
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const DEFAULT_STARKNET_ARTIFACTS_PATH = "starknet-artifacts";
export const DEFAULT_STARKNET_ACCOUNT_PATH = "~/.starknet_accounts";
export const CAIRO_CLI_DOCKER_REPOSITORY = "shardlabs/cairo-cli";
export const CAIRO_CLI_DEFAULT_DOCKER_IMAGE_TAG = config["CAIRO_LANG"];
export const DEVNET_DOCKER_REPOSITORY = "shardlabs/starknet-devnet";
export const DEVNET_DOCKER_REPOSITORY = "shardlabs/starknet-devnet-rs";
export const DEFAULT_DEVNET_DOCKER_IMAGE_TAG = config["STARKNET_DEVNET"];
export const DEFAULT_DEVNET_CAIRO_VM = "python";
export const AMARNA_DOCKER_REPOSITORY = "shramee/amarna";
Expand Down
93 changes: 42 additions & 51 deletions src/legacy/account/account-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios, { AxiosError } from "axios";
import { AxiosError } from "axios";
import { HardhatRuntimeEnvironment, StarknetContract, StringMap } from "hardhat/types";
import fs from "node:fs";
import path from "node:path";
import { ec, hash, stark } from "starknet";
import { EstimateFeeResponse, RpcProvider, SierraContractClass, ec, hash, stark } from "starknet";

import {
INTERNAL_ARTIFACTS_DIR,
Expand Down Expand Up @@ -138,27 +138,30 @@ export async function sendDeployAccountTx(
maxFee: string
) {
const hre = await import("hardhat");
const resp = await axios
.post(`${hre.starknet.networkConfig.url}/gateway/add_transaction`, {
max_fee: maxFee,
signature: signatures,
nonce: INITIAL_NONCE,
class_hash: classHash,
contract_address_salt: salt,
constructor_calldata: constructorCalldata,
version: numericToHexString(TRANSACTION_VERSION),
type: "DEPLOY_ACCOUNT"
})
const response = await (hre.starknetProvider as RpcProvider)
.deployAccountContract(
{
classHash,
constructorCalldata,
addressSalt: salt,
signature: signatures
},
{
maxFee,
nonce: INITIAL_NONCE,
version: numericToHexString(TRANSACTION_VERSION)
}
)
.catch((error: AxiosError<starknetTypes.StarkError>) => {
const msg = `Deploying account failed: ${error.response.data.message}`;
throw new StarknetPluginError(msg, error);
});

return new Promise<string>((resolve, reject) => {
iterativelyCheckStatus(
resp.data.transaction_hash,
response.transaction_hash,
hre,
() => resolve(resp.data.transaction_hash),
() => resolve(response.transaction_hash),
reject
);
});
Expand All @@ -174,52 +177,40 @@ export async function sendDeclareV2Tx(
contractClass: Cairo1ContractClass
) {
const hre = await import("hardhat");
const resp = await axios
.post(`${hre.starknet.networkConfig.url}/gateway/add_transaction`, {
type: "DECLARE",
contract_class: contractClass.getCompiledClass(),
signature: signatures,
sender_address: senderAddress,
compiled_class_hash: compiledClassHash,
version: numericToHexString(version),
nonce: numericToHexString(nonce),
max_fee: numericToHexString(maxFee)
})
.catch((error: AxiosError<starknetTypes.StarkError>) => {
const msg = `Declaring contract failed: ${error.response.data.message}`;
const response = await hre.starknetProvider
.declareContract(
{
compiledClassHash,
senderAddress,
contract: contractClass.getCompiledClass() as SierraContractClass,
signature: signatures
},
{
maxFee,
nonce,
version
}
)
.catch((error) => {
const msg = `Declaring contract failed: ${error.message}`;
throw new StarknetPluginError(msg, error);
});

return new Promise<string>((resolve, reject) => {
iterativelyCheckStatus(
resp.data.transaction_hash,
response.transaction_hash,
hre,
() => resolve(resp.data.transaction_hash),
() => resolve(response.transaction_hash),
reject
);
});
}

export async function sendEstimateFeeTx(data: unknown) {
const hre = await import("hardhat");
// To resolve TypeError: Do not know how to serialize a BigInt
// coming from axios
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};

const resp = await axios
.post(`${hre.starknet.networkConfig.url}/feeder_gateway/estimate_fee`, data)
.catch((error: AxiosError<starknetTypes.StarkError>) => {
const msg = `Estimating fees failed: ${error.response.data.message}`;
throw new StarknetPluginError(msg, error);
});

const { gas_price, gas_usage, overall_fee, unit } = resp.data;
export function mapToLegacyFee(estimate: EstimateFeeResponse): starknetTypes.FeeEstimation {
return {
amount: BigInt(overall_fee),
unit,
gas_price: BigInt(gas_price),
gas_usage: BigInt(gas_usage)
} as starknetTypes.FeeEstimation;
amount: estimate.overall_fee,
unit: "wei",
gas_price: estimate.gas_price,
gas_usage: estimate.gas_consumed
};
}
72 changes: 48 additions & 24 deletions src/legacy/account/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { constants, ec, hash, selector, BigNumberish, Call, RawCalldata } from "starknet";
import {
constants,
ec,
hash,
selector,
BigNumberish,
Call,
RawCalldata,
SierraContractClass,
SuccessfulTransactionReceiptResponse
} from "starknet";

import {
UDC_DEPLOY_FUNCTION_NAME,
Expand Down Expand Up @@ -31,7 +41,7 @@ import {
DeclareOptions
} from "../types";
import { InteractChoice } from "../utils";
import { sendEstimateFeeTx, CallParameters, sendDeclareV2Tx } from "./account-utils";
import { CallParameters, mapToLegacyFee, sendDeclareV2Tx } from "./account-utils";

type ExecuteCallParameters = {
to: bigint;
Expand Down Expand Up @@ -121,7 +131,10 @@ export abstract class Account {
);

const hre = await import("hardhat");
const deploymentReceipt = await getTransactionReceiptUtil(deployTxHash, hre);
const deploymentReceipt = (await getTransactionReceiptUtil(
deployTxHash,
hre
)) as SuccessfulTransactionReceiptResponse;
const decodedEvents = udc.decodeEvents(deploymentReceipt.events);
// the only event should be ContractDeployed
const deployedContractAddress = numericToHexString(decodedEvents[0].data.address);
Expand Down Expand Up @@ -190,18 +203,24 @@ export abstract class Account {
compiledClassHash
);
const signatures = this.getSignatures(messageHash);
const contract = readCairo1Contract(
contractFactory.metadataPath
).getCompiledClass() as SierraContractClass;

const data = {
type: "DECLARE",
sender_address: this.address,
compiled_class_hash: compiledClassHash,
contract_class: readCairo1Contract(contractFactory.metadataPath).getCompiledClass(),
signature: bnToDecimalStringArray(signatures || []),
version: numericToHexString(version),
nonce: numericToHexString(nonce)
};

return await sendEstimateFeeTx(data);
const estimate = await hre.starknetProvider.getDeclareEstimateFee(
{
compiledClassHash,
contract,
senderAddress: this.address,
signature: bnToDecimalStringArray(signatures || [])
},
{
maxFee,
nonce,
version: numericToHexString(QUERY_VERSION)
}
);
return mapToLegacyFee(estimate);
}

async estimateDeclareFee(
Expand Down Expand Up @@ -232,17 +251,22 @@ export abstract class Account {
chainId,
numericToHexString(nonce)
]);

const signature = this.getSignatures(messageHash);
const data = {
type: "DECLARE",
sender_address: this.address,
contract_class: readContract(contractFactory.metadataPath),
signature: bnToDecimalStringArray(signature || []),
version: numericToHexString(QUERY_VERSION),
nonce: numericToHexString(nonce)
};
return await sendEstimateFeeTx(data);
const contract = readContract(contractFactory.metadataPath);

const estimate = await hre.starknetProvider.getDeclareEstimateFee(
{
contract,
senderAddress: this.address,
signature: bnToDecimalStringArray(signature || [])
},
{
maxFee,
nonce,
version: numericToHexString(QUERY_VERSION)
}
);
return mapToLegacyFee(estimate);
}

async estimateDeployFee(
Expand Down
27 changes: 15 additions & 12 deletions src/legacy/account/argent-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
CallParameters,
generateKeys,
handleInternalContractArtifacts,
mapToLegacyFee,
sendDeployAccountTx,
sendEstimateFeeTx,
signMultiCall
} from "./account-utils";
import { Account } from "./account";
Expand Down Expand Up @@ -203,19 +203,22 @@ export class ArgentAccount extends Account {
hre.starknet.networkConfig.starknetChainId,
nonce
]);

const signature = this.getSignatures(msgHash);
const data = {
type: "DEPLOY_ACCOUNT",
class_hash: ArgentAccount.PROXY_CLASS_HASH,
constructor_calldata: constructorCalldata,
contract_address_salt: this.salt,
signature: bnToDecimalStringArray(signature || []),
version: numericToHexString(QUERY_VERSION),
nonce
};

return await sendEstimateFeeTx(data);
const estimate = await hre.starknetProvider.getDeployAccountEstimateFee(
{
classHash: ArgentAccount.PROXY_CLASS_HASH,
constructorCalldata,
addressSalt: this.salt,
signature: bnToDecimalStringArray(signature || [])
},
{
maxFee,
nonce,
version: numericToHexString(QUERY_VERSION)
}
);
return mapToLegacyFee(estimate);
}

/**
Expand Down
27 changes: 15 additions & 12 deletions src/legacy/account/open-zeppelin-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
calculateDeployAccountHash,
generateKeys,
handleInternalContractArtifacts,
mapToLegacyFee,
sendDeployAccountTx,
sendEstimateFeeTx,
signMultiCall
} from "./account-utils";
import { Account } from "./account";
Expand Down Expand Up @@ -141,19 +141,22 @@ export class OpenZeppelinAccount extends Account {
hre.starknet.networkConfig.starknetChainId,
nonce
]);

const signature = this.getSignatures(msgHash);
const data = {
type: "DEPLOY_ACCOUNT",
class_hash: classHash,
constructor_calldata: constructorCalldata,
contract_address_salt: this.salt,
signature: bnToDecimalStringArray(signature || []),
version: numericToHexString(QUERY_VERSION),
nonce
};

return await sendEstimateFeeTx(data);
const estimate = await hre.starknetProvider.getDeployAccountEstimateFee(
{
classHash,
constructorCalldata,
addressSalt: this.salt,
signature: bnToDecimalStringArray(signature || [])
},
{
maxFee,
nonce,
version: numericToHexString(QUERY_VERSION)
}
);
return mapToLegacyFee(estimate);
}

public override async deployAccount(options: DeployAccountOptions = {}): Promise<string> {
Expand Down
Loading

0 comments on commit d19c8d2

Please sign in to comment.