Skip to content

Commit

Permalink
Upgrade starknet-js and get-starknet, remove BN
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Aug 27, 2023
1 parent 858fe45 commit 037633f
Show file tree
Hide file tree
Showing 29 changed files with 654 additions and 428 deletions.
529 changes: 394 additions & 135 deletions packages/starksheet-webapp/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/starksheet-webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.2.0",
"private": true,
"dependencies": {
"@argent/get-starknet": "^6.4.7",
"@emotion/react": "^11.9.3",
"@emotion/styled": "^11.9.3",
"@mui/material": "^5.8.6",
Expand All @@ -19,15 +20,14 @@
"ethers": "5.7.2",
"ethers-multisend": "^2.4.0",
"expr-eval": "^2.0.2",
"get-starknet": "^2.0.0",
"notistack": "^2.0.5",
"react": "^18.2.0",
"react-contenteditable": "^3.3.6",
"react-dom": "^18.2.0",
"react-hotkeys": "^2.0.0",
"react-router-dom": "^6.10.0",
"react-scripts": "5.0.1",
"starknet": "^4.22.0",
"starknet": "^5.18.0",
"ts-toolbelt": "^9.6.0",
"web-vitals": "^2.1.4",
"wido-widget": "^1.0.8"
Expand Down
22 changes: 10 additions & 12 deletions packages/starksheet-webapp/src/components/ActionBar/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Box, BoxProps } from "@mui/material";
import { useSnackbar } from "notistack";
import React, { useCallback, useContext, useEffect } from "react";
import ContentEditable from "react-contenteditable";
import { number } from "starknet";
import { CELL_BORDER_WIDTH } from "../../config";
import { AbisContext } from "../../contexts/AbisContext";
import { AccountContext } from "../../contexts/AccountContext";
Expand All @@ -11,7 +10,7 @@ import { OnsheetContext } from "../../contexts/OnsheetContext";
import { chainConfig } from "../../provider/chains";
import { CellData, CellGraph, Cell as CellType } from "../../types";
import { RC_BOUND } from "../../utils/constants";
import { bn2hex, str2hex } from "../../utils/hexUtils";
import { bigint2hex, str2hex } from "../../utils/hexUtils";
import {
resolveContractAddress,
tokenIdToCellName,
Expand Down Expand Up @@ -60,10 +59,10 @@ function ActionBar({ inputRef, sx }: ActionBarProps) {
const currentCell = currentCells[currentCellId];

if (
currentCell.contractAddress.eq(cellData.contractAddress) &&
currentCell.selector.eq(cellData.selector) &&
currentCell.contractAddress === cellData.contractAddress &&
currentCell.selector === cellData.selector &&
currentCell.calldata.length === cellData.calldata.length &&
currentCell.calldata.every((c, i) => c.eq(cellData.calldata[i]))
currentCell.calldata.every((c, i) => c === cellData.calldata[i])
) {
return;
}
Expand Down Expand Up @@ -149,14 +148,14 @@ function ActionBar({ inputRef, sx }: ActionBarProps) {
const _contractCall = parseContractCall(_value);

if (!_contractCall) {
let selector = number.toBN(0);
let selector = BigInt(0);
try {
selector = number.toBN(_value);
selector = BigInt(_value);
} catch (e) {
try {
selector = number.toBN(str2hex(_value));
selector = BigInt(str2hex(_value));
} catch (e) {
selector = number.toBN(0);
selector = BigInt(0);
}
}
setCellData({
Expand All @@ -169,7 +168,7 @@ function ActionBar({ inputRef, sx }: ActionBarProps) {

let contractAddress;
try {
contractAddress = number.toBN(_contractCall.contractAddress);
contractAddress = BigInt(_contractCall.contractAddress);
} catch (e) {
return;
}
Expand All @@ -179,7 +178,7 @@ function ActionBar({ inputRef, sx }: ActionBarProps) {
contractAddress
);

getAbiForContract(bn2hex(resolvedContractAddress)).then((abi) => {
getAbiForContract(bigint2hex(resolvedContractAddress)).then((abi) => {
const _cellData = parse(_contractCall, abi, chainConfig.chainType);
setCellData(_cellData);
});
Expand All @@ -206,7 +205,6 @@ function ActionBar({ inputRef, sx }: ActionBarProps) {
}, [selectedCell, currentCells]);

const owner = currentCells[selectedCell]?.owner?.toString(16);

return (
<Box sx={{ display: "flex", ...sx }}>
<Cell sx={{ width: "134px", "& .content": { textAlign: "center" } }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BN from "bn.js";
import { BigNumber, ethers } from "ethers";
import { FunctionAbi, number, uint256 } from "starknet";
import { FunctionAbi, uint256 } from "starknet";
import { CellData, ChainType, ContractAbi } from "../../types";
import {
ARGS_SEP,
Expand All @@ -11,7 +10,7 @@ import {
HEX_STRING_REGEX,
RC_BOUND,
} from "../../utils/constants";
import { bn2hex, bn2uint, hex2str } from "../../utils/hexUtils";
import { bigint2hex, bigint2uint, hex2str } from "../../utils/hexUtils";
import {
cellNameToTokenId,
encodeConst,
Expand All @@ -21,7 +20,7 @@ import {
} from "../../utils/sheetUtils";

const isBigNumber = (arg: any): boolean => {
return arg instanceof BigNumber || arg instanceof BN;
return arg instanceof BigNumber || typeof arg === "bigint";
};

export function toPlainTextFormula(
Expand All @@ -31,24 +30,23 @@ export function toPlainTextFormula(
if (!cellData) return "0";

const { contractAddress, selector, calldata, abi } = cellData;
if (contractAddress.eq(RC_BOUND)) {
return selector.gte(RC_BOUND) ? bn2hex(selector) : selector.toString();
if (contractAddress === RC_BOUND) {
return selector >= RC_BOUND ? bigint2hex(selector) : selector.toString();
}

const contractName = contractAddress.lt(RC_BOUND)
? tokenIdToCellName(contractAddress.toNumber())
: bn2hex(contractAddress);
const selectorHexString = bn2hex(selector);
const contractName =
contractAddress <= RC_BOUND
? tokenIdToCellName(Number(contractAddress))
: bigint2hex(contractAddress);
const selectorHexString = bigint2hex(selector);
const operator = abi?.name || selectorHexString;

const args = calldata.map((arg) =>
isDependency(arg)
? tokenIdToCellName(
arg.sub(number.toBN(1)).div(number.toBN(2)).toNumber()
)
: arg.gte(RC_BOUND)
? "0x" + arg.div(number.toBN(2)).toString(16)
: arg.div(number.toBN(2)).toString()
? tokenIdToCellName(Number((arg - 1n) / 2n))
: arg >= RC_BOUND
? "0x" + (arg / 2n).toString(16)
: (arg / 2n).toString()
);
let displayedArgs = [];
if (!!abi) {
Expand Down Expand Up @@ -105,12 +103,10 @@ export function toPlainTextFormula(
)
._hex.slice(2)
.padStart(64, "0");
mapping[placeholder] = tokenIdToCellName(
arg.sub(number.toBN(1)).div(number.toBN(2)).toNumber()
);
mapping[placeholder] = tokenIdToCellName(Number((arg - 1n) / 2n));
return placeholder;
}
return bn2uint(32)(arg.div(number.toBN(2)));
return bigint2uint(32)(arg / 2n);
})
.join("");
const decodedData = ethers.utils.defaultAbiCoder.decode(
Expand Down Expand Up @@ -233,7 +229,7 @@ export function parse(
const encodedArgs = encodeInputs(args) as any[];
// Flatten the object prefixing arrays with their len
// Result dismisses the first value, with is the len of the initial array of args
calldata = flattenWithLen(encodedArgs).slice(1) as BN[];
calldata = flattenWithLen(encodedArgs).slice(1) as bigint[];
} else if (chainType === ChainType.EVM) {
const m: Record<string, string> = {};
const mappedArgs = mapCellsToRandom(m)(args);
Expand All @@ -251,23 +247,21 @@ export function parse(
return encodeTokenId(cellNameToTokenId(cellName));
}
return encodeConst("0x" + bytes32);
}) || []) as BN[];
}) || []) as bigint[];
} else {
throw new Error(`No parsing function for chainType ${chainType}`);
}

return {
contractAddress: number.toBN(rawCall.contractAddress),
selector: number.toBN(selector),
contractAddress: BigInt(rawCall.contractAddress),
selector: BigInt(selector),
calldata,
abi: selectorAbi,
};
}

export const decode = (_arg: BN) =>
isDependency(_arg)
? _arg.sub(number.toBN(1)).div(number.toBN(2))
: _arg.div(number.toBN(2));
export const decode = (_arg: bigint) =>
isDependency(_arg) ? (_arg - 1n) / 2n : _arg / 2n;

/**
*
Expand Down Expand Up @@ -341,7 +335,7 @@ export function buildFormulaDisplay(

if (settings?.text) {
try {
return hex2str(bn2hex(number.toBN(formula)));
return hex2str(bigint2hex(BigInt(formula)));
} catch (e) {
return formula;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useSnackbar } from "notistack";
import { useContext, useMemo } from "react";
import { constants, number } from "starknet";
import { constants } from "starknet";
import Tooltip from "../../Tooltip/Tooltip";
import { CELL_BORDER_WIDTH, CELL_WIDTH } from "../../config";
import { AccountContext } from "../../contexts/AccountContext";
Expand All @@ -11,7 +11,7 @@ import { useLocalStorage } from "../../hooks/useLocalStorage";
import { chainConfig } from "../../provider/chains";
import { CellGraph, Cell as CellType } from "../../types";
import { RC_BOUND } from "../../utils/constants";
import { bn2hex, hex2str } from "../../utils/hexUtils";
import { bigint2hex, hex2str } from "../../utils/hexUtils";
import Cell from "../Cell/Cell";

const BLUE = "#0000FF";
Expand Down Expand Up @@ -52,18 +52,15 @@ function ComputedCell({ cell }: ComputedCellProps) {

const { background, borderColor, color } = useMemo(() => {
const background =
cell.owner.eq(number.toBN(0)) &&
cell.contractAddress.eq(RC_BOUND) &&
cell.selector.eq(number.toBN(0))
cell.owner === 0n &&
cell.contractAddress === RC_BOUND &&
cell.selector === 0n
? WHITE
: isInvoke
? GREEN
: accountAddress === bn2hex(cell.owner) ||
(cell.owner.eq(number.toBN(0)) &&
!(
cell.contractAddress.eq(RC_BOUND) &&
cell.selector.eq(number.toBN(0))
))
: accountAddress === bigint2hex(cell.owner) ||
(cell.owner === 0n &&
!(cell.contractAddress === RC_BOUND && cell.selector === 0n))
? BLUE
: GREY;

Expand All @@ -83,25 +80,15 @@ function ComputedCell({ cell }: ComputedCellProps) {
cell.abi?.name === "name" ||
cell.abi?.name === "symbol" ||
cellSettings.text;
if (renderString) return hex2str(bn2hex(value));
if (cell.contractAddress.eq(number.toBN(chainConfig.addresses.math))) {
return value
.add(
number
.toBN("0x" + constants.FIELD_PRIME)
.div(number.toBN(2))
.abs()
)
.mod(number.toBN("0x" + constants.FIELD_PRIME))
.sub(
number
.toBN("0x" + constants.FIELD_PRIME)
.div(number.toBN(2))
.abs()
)
.toString();
if (renderString) return hex2str(bigint2hex(value));
if (cell.contractAddress === BigInt(chainConfig.addresses.math)) {
return (
((value + BigInt("0x" + constants.FIELD_PRIME) / 2n) %
BigInt("0x" + constants.FIELD_PRIME)) -
BigInt("0x" + constants.FIELD_PRIME) / 2n
).toString();
}
if (value.gte(RC_BOUND)) return bn2hex(value);
if (value >= RC_BOUND) return bigint2hex(value);
return value.toString();
}, [cell, background, cellSettings.text]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
CELL_NAME_REGEX,
CONTRACT_FUNCTION_SEP,
} from "../../utils/constants";
import { bn2hex } from "../../utils/hexUtils";
import { bigint2hex } from "../../utils/hexUtils";
import { cellNameToTokenId } from "../../utils/sheetUtils";
import { buildFormulaDisplay } from "../ActionBar/formula.utils";

Expand Down Expand Up @@ -55,7 +55,7 @@ function FormulaField({
let _selectedContractAddress = value.split(CONTRACT_FUNCTION_SEP)[0];
setSelectedContractAddress(_selectedContractAddress);
if (_selectedContractAddress.match(CELL_NAME_REGEX)) {
_selectedContractAddress = bn2hex(
_selectedContractAddress = bigint2hex(
currentCells[cellNameToTokenId(_selectedContractAddress)].value
);
}
Expand Down
23 changes: 11 additions & 12 deletions packages/starksheet-webapp/src/components/SheetTable/SheetTable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, BoxProps } from "@mui/material";
import { useCallback, useContext, useEffect, useMemo } from "react";
import { useLoaderData, useNavigate } from "react-router-dom";
import { FunctionAbi, number } from "starknet";
import { FunctionAbi } from "starknet";
import {
CELL_BORDER_WIDTH,
CELL_HEIGHT,
Expand All @@ -16,19 +16,19 @@ import { useSheetContract } from "../../hooks/useSheetContract";
import { chainConfig } from "../../provider/chains";
import { Cell, CellData, CellRendered } from "../../types";
import { RC_BOUND } from "../../utils/constants";
import { bn2hex } from "../../utils/hexUtils";
import { bigint2hex } from "../../utils/hexUtils";
import ComputedCell from "../ComputedCell/ComputedCell";
import GreyCell from "../GreyCell/GreyCell";

const defaultRenderedCell = (tokenId: number): CellRendered => ({
id: tokenId,
owner: number.toBN(0),
value: number.toBN(0),
owner: 0n,
value: 0n,
});

const defaultCellData = (tokenId: number): CellData => ({
contractAddress: RC_BOUND,
selector: number.toBN(0),
selector: 0n,
calldata: [],
});

Expand Down Expand Up @@ -166,17 +166,16 @@ const SheetTable = ({ sx }: SheetTableProps) => {
}
)
.map(async (cell, _, array) => {
const resolvedContractAddress = cell.contractAddress.lt(
RC_BOUND
)
? array[cell.contractAddress.toNumber()].value
: cell.contractAddress;
const resolvedContractAddress =
cell.contractAddress < RC_BOUND
? array[Number(cell.contractAddress)].value
: cell.contractAddress;
const abi = await getAbiForContract(
bn2hex(resolvedContractAddress)
bigint2hex(resolvedContractAddress)
);
return {
...cell,
abi: abi[bn2hex(cell.selector)] as FunctionAbi,
abi: abi[bigint2hex(cell.selector)] as FunctionAbi,
};
})
);
Expand Down
4 changes: 2 additions & 2 deletions packages/starksheet-webapp/src/contexts/AbisContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Abi } from "starknet";
import { useChainProvider } from "../hooks/useChainProvider";
import { ContractAbi, ContractAbis, InitialContractAbis } from "../types";
import { RC_BOUND } from "../utils/constants";
import { bn2hex, normalizeHexString } from "../utils/hexUtils";
import { bigint2hex, normalizeHexString } from "../utils/hexUtils";

export const AbisContext = React.createContext<{
contractAbis: ContractAbis;
Expand All @@ -27,7 +27,7 @@ export const AbisContextProvider = ({
[normalizeHexString(cur[0])]: chainProvider.parseAbi(cur[1] as Abi),
}),
{
[bn2hex(RC_BOUND)]: {},
[bigint2hex(RC_BOUND)]: {},
}
);

Expand Down
Loading

0 comments on commit 037633f

Please sign in to comment.