Skip to content

Commit

Permalink
Merge pull request #101 from influenceth/wrap-bigints
Browse files Browse the repository at this point in the history
Wrap bigint for system input formatting
  • Loading branch information
clexmond authored Jul 24, 2024
2 parents f13ae32 + 8798801 commit a50de06
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
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

0 comments on commit a50de06

Please sign in to comment.