Skip to content
Merged
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
32 changes: 26 additions & 6 deletions app/exp001/goodness.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createPublicClient } from '../../packages/starkweb/dist/esm/clients/createPublicClient'
import { http } from '../../packages/starkweb/dist/esm/clients/transports/http'
import { mainnet } from '../../packages/starkweb/dist/esm/chains/definitions/mainnet'
import { sepolia } from '../../packages/starkweb/dist/esm/chains/definitions/sepolia';


export const crimeAbi = [
Expand Down Expand Up @@ -796,14 +797,33 @@ export const crimeAbi = [

const client = createPublicClient({
transport: http(),
chain: mainnet,
chain: sepolia,
})

const result = await client.readContract({
address: '0x020bd5ec01c672e69e3ca74df376620a6be8a2b104ab70a9f0885be00dd38fb9',
abi: crimeAbi,
functionName: 'owner',
args: [],

const result = await client.transport.request({
jsonrpc: '2.0',
method: 'starknet_call',
params: [
{
"calldata":["0x03b56013adf5ce1febefc5dbea283d4a5fb0ce2b9d9e8a174b8e8df005e88fe2"],
"contract_address":"0x01f7e900d8d0eb877e48c7637496f7727efaccd221c9006ee26f2b2376c9773f","entry_point_selector":"0x00cf37a862e5bf34bd0e858865ea02d4ba6db9cc722f3424eb452c94d4ea567f"

},
{
"calldata":["0x03b56013adf5ce1febefc5dbea283d4a5fb0ce2b9d9e8a174b8e8df005e88fe2"],
"contract_address":"0x01f7e900d8d0eb877e48c7637496f7727efaccd221c9006ee26f2b2376c9773f","entry_point_selector":"0x00cf37a862e5bf34bd0e858865ea02d4ba6db9cc722f3424eb452c94d4ea567f"

},
"latest"
],
id: 1,
})
// const result = await client.readContract({
// address: '0x020bd5ec01c672e69e3ca74df376620a6be8a2b104ab70a9f0885be00dd38fb9',
// abi: crimeAbi,
// functionName: 'owner',
// args: [],
// })

console.log(result)
25 changes: 12 additions & 13 deletions app/next/pages/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { useAccount, useBalance, useWriteContract } from 'starkweb/react';
import { useState, useCallback } from 'react';
import { useTheme } from 'next-themes';
import Link from 'next/link';
import { createWalletClient, custom, WalletClient } from 'starkweb';
import { erc20Abi } from '../utils/abi/strk';
import 'starkweb/window'
import { uint256 } from 'starkweb/utils';
import { Address } from 'starkweb/dist/types/abi/starkweb-abi';

const Wallet: NextPage = () => {
const { theme, setTheme } = useTheme();
const [recipient, setRecipient] = useState<string>('');
Expand Down Expand Up @@ -122,10 +124,10 @@ function SendTransactionCard({
amount,
setAmount
}: {
recipient: string;
setRecipient: (value: string) => void;
amount: string;
setAmount: (value: string) => void;
recipient: Address;
setRecipient: (value: Address) => void;
amount: bigint;
setAmount: (value: bigint) => void;
}) {
const [isPending, setIsPending] = useState(false);
const [isError, setIsError] = useState(false);
Expand All @@ -143,11 +145,8 @@ function SendTransactionCard({
abi: erc20Abi,
functionName: 'transfer',
args: {
recipient: recipient as 'contract_address',
amount: {
high: parseFloat(amount),
low: 0,
} as unknown as 'u256',
recipient: recipient,
amount: uint256(amount),
}
});

Expand Down Expand Up @@ -193,7 +192,7 @@ function SendTransactionCard({
id="recipient"
type="text"
value={recipient}
onChange={(e) => setRecipient(e.target.value)}
onChange={(e) => setRecipient(e.target.value as Address)}
className="w-full p-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
placeholder="0x..."
/>
Expand All @@ -206,8 +205,8 @@ function SendTransactionCard({
<input
id="amount"
type="text"
value={amount}
onChange={(e) => setAmount(e.target.value)}
value={amount.toString()}
onChange={(e) => setAmount(BigInt(e.target.value))}
className="w-full p-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
placeholder="0.0"
/>
Expand Down
46 changes: 39 additions & 7 deletions app/starkweb-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,48 @@
createPublicClient,
createWalletClient,
custom,
erc20Abi,
http,
} from 'starkweb';
import { sepolia } from 'starkweb/chains';
import { getStarknetId, getStarknetIdName } from 'starkweb/actions';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import getStarknetIdName.

Copilot Autofix

AI 12 months ago

To fix the problem, we need to remove the unused import getStarknetIdName from the import statement on line 13. This will clean up the code and remove any confusion about unused imports.

  • Remove the unused import getStarknetIdName from the import statement.
  • Ensure that the remaining code functionality is not affected by this change.
Suggested changeset 1
app/starkweb-client/src/index.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/app/starkweb-client/src/index.ts b/app/starkweb-client/src/index.ts
--- a/app/starkweb-client/src/index.ts
+++ b/app/starkweb-client/src/index.ts
@@ -12,3 +12,3 @@
 } from 'starkweb';
-import { getStarknetId, getStarknetIdName } from 'starkweb/actions';
+import { getStarknetId } from 'starkweb/actions';
 import { mainnet, sepolia } from 'starkweb/chains';
EOF
@@ -12,3 +12,3 @@
} from 'starkweb';
import { getStarknetId, getStarknetIdName } from 'starkweb/actions';
import { getStarknetId } from 'starkweb/actions';
import { mainnet, sepolia } from 'starkweb/chains';
Copilot is powered by AI and may make mistakes. Always verify output.
import { mainnet, sepolia } from 'starkweb/chains';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import sepolia.

Copilot Autofix

AI 12 months ago

To fix the problem, we need to remove the unused import sepolia from the import statement. This will clean up the code and remove any confusion about the usage of sepolia. The change should be made in the file app/starkweb-client/src/index.ts on line 14.

Suggested changeset 1
app/starkweb-client/src/index.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/app/starkweb-client/src/index.ts b/app/starkweb-client/src/index.ts
--- a/app/starkweb-client/src/index.ts
+++ b/app/starkweb-client/src/index.ts
@@ -13,3 +13,3 @@
 import { getStarknetId, getStarknetIdName } from 'starkweb/actions';
-import { mainnet, sepolia } from 'starkweb/chains';
+import { mainnet } from 'starkweb/chains';
 
EOF
@@ -13,3 +13,3 @@
import { getStarknetId, getStarknetIdName } from 'starkweb/actions';
import { mainnet, sepolia } from 'starkweb/chains';
import { mainnet } from 'starkweb/chains';

Copilot is powered by AI and may make mistakes. Always verify output.

const publicClient = createPublicClient({
chain: sepolia,
transport: http(),
chain: mainnet,
transport: http('https://starknet-mainnet.infura.io/v3/db72641028ee47f5b18bcbb791a3f829'),
});
const id = await getStarknetId(publicClient, {
domain: 'solene.stark',
})
// const name = await getStarknetIdName(publicClient, {
// address: '0x061b6c0a78f9edf13cea17b50719f3344533fadd470b8cb29c2b4318014f52d3',
// })
console.log(id)
// console.log(name)

const block = await publicClient.getBlockStateUpdate({
block_tag: 'latest',
});
console.log(block.state_diff);
// const result = await publicClient.readContracts({
// contracts: [
// {
// abi: erc20Abi,
// functionName: 'balanceOf',
// args:
// {
// account: '0x005c475b6089156c0CD4Fc9d64De149992431c442AF882d6332e7c736c99DE91',
// },
// address:
// '0x04718f5a0Fc34cC1AF16A1cdee98fFB20C31f5cD61D6Ab07201858f4287c938D',
// },
// {
// abi: erc20Abi,
// functionName: 'balanceOf',
// args:
// {
// account: '0x034abecf49cedc634d0c3145da7b1caea99d8d4f2da5b5d41e532ea05192d523',
// },
// address:
// '0x04718f5a0Fc34cC1AF16A1cdee98fFB20C31f5cD61D6Ab07201858f4287c938D',
// },
// ],
// })

// console.log(result);
29 changes: 15 additions & 14 deletions packages/starkweb/src/abi/abitype.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Abi } from '../strk-types/abi.js';

import type { Uint512 } from '../strk-types/lib.js';
import type { Uint256 } from '../strk-types/lib.js';
import type {
AbiType,
Address,
} from './starkweb-abi.js';
// import type { testAbi } from './testabi.js';

export type CairoInt = 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256'


export type CairoInt = 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'u512'



Expand All @@ -24,17 +24,18 @@ export type AbiParameterKind = 'inputs' | 'outputs';
*/
interface PrimitiveTypeLookup {
'core::felt252': { inputs: 'felt252'; outputs: 'felt252' };
'core::integer::u8': { inputs: 'u8'; outputs: 'u8' };
'core::integer::u16': { inputs: 'u16'; outputs: 'u16' };
'core::integer::u32': { inputs: 'u32'; outputs: 'u32' };
'core::integer::u64': { inputs: 'u64'; outputs: 'u64' };
'core::integer::u128': { inputs: 'u128'; outputs: 'u128' };
'core::integer::u256': { inputs: 'u256'; outputs: 'u256' };
'core::integer::u8': { inputs: number; outputs: number };
'core::integer::u16': { inputs: number; outputs: number };
'core::integer::u32': { inputs: number; outputs: number };
'core::integer::u64': { inputs: number; outputs: number };
'core::integer::u128': { inputs: number; outputs: number };
'core::integer::u256': { inputs: Uint256; outputs: Uint256 };
'core::integer::u512': { inputs: Uint512; outputs: Uint512 };
'core::array::Array<T>': { inputs: 'T[]'; outputs: 'T[]' };
'core::bool': { inputs: 'boolean'; outputs: 'boolean' };
'core::starknet::contract_address::ContractAddress': { inputs: 'contract_address'; outputs: 'contract_address' };
'core::string::String': { inputs: 'string'; outputs: 'string' };
'core::starknet::class_hash::ClassHash': { inputs: 'string'; outputs: 'string' };
'core::bool': { inputs: boolean; outputs: boolean };
'core::starknet::contract_address::ContractAddress': { inputs: Address; outputs: Address };
'core::string::String': { inputs: string; outputs: string };
'core::starknet::class_hash::ClassHash': { inputs: string; outputs: string };
tuple: Record<string, unknown>;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/starkweb/src/actions/public/readContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export async function readContract<
parameters as ReadContractParameters<TAbi, TFunctionName>
const calldata: string[] = args ? compile(args as any) : []

// Simplified block_id determination

const txCall: CallParameters = {
contract_address: address,
entry_point_selector: getSelectorFromName(functionName),
Expand Down
39 changes: 33 additions & 6 deletions packages/starkweb/src/actions/public/readContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import type { Transport } from '../../clients/transports/createTransport.js'
import { calldataToHex, compile } from '../../strk-utils/calldata/compile.js'
import { getSelectorFromName } from '../../strk-utils/hash/selector.js'
import type { Chain } from '../../types/chain.js'
import { call } from './call.js'
import { call, type CallParameters } from './call.js'
import type {
PrimaryReadContractParameters,
SecondaryReadContractParameters,
} from './readContract.js'
import type { ContractFunctionName } from '../../types/contract.js'
import { createBatchScheduler } from '../../utils/promise/createBatchScheduler.js'
import { decodeFunctionCall } from '../../abi/output.js'

export type ReadContractsParameters<
abi extends Abi | readonly unknown[] = Abi,
Expand Down Expand Up @@ -37,21 +39,46 @@ export async function readContracts<
): Promise<ReadContractsReturnTypes | ReadContractsErrorType> {
const { contracts, blockHash, blockNumber, blockTag } = parameters as ReadContractsParameters

const txCallsPromise = contracts.map((callParams) => {
if (contracts.length === 0) return []

// Prepare all call requests
const callRequests = contracts.map((callParams) => {
const { address, functionName, args } = callParams
const calldata: string[] = args ? compile(args as any) : []

const txCall = {
return {
contract_address: address,
entry_point_selector: getSelectorFromName(functionName),
calldata: calldataToHex(calldata),
block_hash: blockHash,
block_number: blockNumber,
block_tag: blockTag,
}

return call(client, txCall)
})

return Promise.all(txCallsPromise)
// Create a batch scheduler for processing calls
const { schedule } = createBatchScheduler({
id: 'readContracts',
fn: async (requests) => {
// Process all requests in a batch
const results = await Promise.all(
requests.map(request => call(client, request as CallParameters))
)
return results
}
})
// Schedule all calls and collect results
const results = await Promise.all(
callRequests.map((request, index) => {
const contract = contracts[index];
return schedule(request).then(([result]) => {
return decodeFunctionCall(
result as unknown as string[],
contract?.functionName,
contract?.abi as any
);
});
})
)
return results
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function getStarknetIdName<
client: Client<Transport, TChain, TAccount>,
{ address }: GetStarknetIdNameParameters,
): Promise<GetStarknetIdNameReturnType> {
const chainId = client.chain?.id as unknown as StarknetChainId
const chainId = client.chain?.chain_id as unknown as StarknetChainId
if (!chainId) {
throw new Error('Chain ID is required')
}
Expand Down
5 changes: 3 additions & 2 deletions packages/starkweb/src/exports/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,10 @@ export { isBoolean, isNumber, isObject, isString, isUndefined } from '../strk-ut
export { validateTypedData as snValidateTypedData, encodeData, encodeType, encodeValue } from '../strk-utils/typedData.js'

export { CairoFelt } from '../strk-utils/cairoDataTypes/felt.js'
export { CairoUint256 } from '../strk-utils/cairoDataTypes/uint256.js'
export { createUint256 } from '../strk-utils/cairoDataTypes/uint256.js'
export { toUint512 } from '../strk-utils/cairoDataTypes/uint512.js'
export { byteArrayFromString } from '../strk-utils/calldata/byteArray.js'
export { felt, uint256 } from '../strk-utils/calldata/cairo.js'
export { felt, uint256, uint512 } from '../strk-utils/calldata/cairo.js'
export { compile, calldataToHex } from '../strk-utils/calldata/compile.js'
export { CairoCustomEnum } from '../strk-utils/calldata/enum/CairoCustomEnum.js'
export { CairoOption, CairoOptionVariant } from '../strk-utils/calldata/enum/CairoOption.js'
Expand Down
22 changes: 15 additions & 7 deletions packages/starkweb/src/strk-utils/cairoDataTypes/felt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,45 @@ import { isHex, isStringWholeNumber } from '../num.js'
import { encodeShortString, isShortString, isText } from '../shortString.js'
import { isBigInt, isBoolean, isString } from '../typed.js'


/**
* Converts a BigNumberish value to a Cairo felt representation in string format
* @param it The value to convert to a felt
* @returns A string representation of the felt value
* @throws Error if the value cannot be converted to a felt
*/
export function CairoFelt(it: BigNumberish): string {
// BN or number
// Handle BigInt or integer numbers
if (isBigInt(it) || Number.isInteger(it)) {
return it.toString()
}

// Handling strings
// Handle string types
if (isString(it)) {
// Hex strings
// Convert hex strings
if (isHex(it)) {
return BigInt(it).toString()
}
// Text strings that must be short
// Handle text strings (must be short strings)
if (isText(it)) {
if (!isShortString(it)) {
throw new Error(
`${it} is a long string > 31 chars. Please split it into an array of short strings.`,
)
}
// Assuming encodeShortString returns a hex representation of the string
// Convert short strings to felt
return BigInt(encodeShortString(it)).toString()
}
// Whole numeric strings
// Handle whole number strings
if (isStringWholeNumber(it)) {
return it
}
}
// bool to felt
// Handle boolean values
if (isBoolean(it)) {
return `${+it}`
}

// Error for values that cannot be converted
throw new Error(`${it} can't be computed by felt()`)
}
Loading
Loading