Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap bigint for system input formatting #101

Merged
merged 2 commits into from
Jul 24, 2024
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@influenceth/sdk",
"version": "2.1.3",
"version": "2.1.4",
"description": "Influence SDK",
"type": "module",
"module": "./build/index.js",
Expand Down
12 changes: 7 additions & 5 deletions src/lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const Systems = Object.keys(SystemData).reduce((acc, name) => {
return acc;
}, {});

const toBigInt = (value) => BigInt(Math.round(Number(value || 0)));

const formatCalldataValue = (type, value) => {
if (type === 'ContractAddress') {
return value;
Expand All @@ -53,22 +55,22 @@ const formatCalldataValue = (type, value) => {
} else if (type === 'Boolean') {
return value;
} else if (type === 'BigNumber') {
return BigInt(value);
return toBigInt(value);
} else if (type === 'Ether') {
return uint256.bnToUint256(value);
} else if (type === 'InventoryItem') {
return [value.product, value.amount];
} else if (type === 'Withdrawal') {
return { recipient: value.recipient, amount: uint256.bnToUint256(BigInt(value.amount)) };
return { recipient: value.recipient, amount: uint256.bnToUint256(toBigInt(value.amount)) };
} else if (type === 'Boolean') {
return !!value;
} else if (type === 'Fixed64') {
const neg = value < 0;
const val = BigInt(Math.floor(Math.abs(value) * 2 ** 32));
const val = toBigInt(Math.floor(Math.abs(value) * 2 ** 32));
return [val, neg ? 1 : 0];
} else if (type === 'Fixed128') {
const neg = value < 0;
const val = BigInt(Math.floor(Math.abs(value) * 2 ** 64)); // TODO: this will cause precision loss, use bignumber
const val = toBigInt(Math.floor(Math.abs(value) * 2 ** 64)); // TODO: this will cause precision loss, use bignumber
return [val, neg ? 1 : 0];
} else if (type === 'EscrowHook') {
if (!value) return { contract: 0, entry_point_selector: '0', calldata: [] };
Expand Down Expand Up @@ -168,7 +170,7 @@ const getTransferWithConfirmationCall = (recipient, amount, memo, consumerAddres
[
{ value: recipient, type: 'ContractAddress' },
{ value: amount, type: 'BigNumber' },
{ value: Array.isArray(memo) ? ec.starkCurve.poseidonHashMany(memo.map((v) => BigInt(v))) : memo },
{ value: Array.isArray(memo) ? ec.starkCurve.poseidonHashMany(memo.map((v) => toBigInt(v))) : memo },
{ value: consumerAddress, type: 'ContractAddress' }
]
);
Expand Down
Loading