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

fix: transferToContract method now allows big numbers #3458

Merged
merged 4 commits into from
Dec 9, 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
5 changes: 5 additions & 0 deletions .changeset/lazy-experts-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

fix: `transferToContract` method now allows big numbers
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,27 @@ describe('util', () => {
expect(arrayify).toHaveBeenCalledTimes(2);
expect(encode).toHaveBeenCalledTimes(1);
});

it('should ensure "formatScriptDataForTransferringToContract" returns script data just fine', () => {
const contractId = '0xf3eb53ed00347d305fc6f6e3a57e91ea6c3182a9efc253488db29494f63c9610';
const amount: BigNumberish = bn(2).pow(64).sub(1); // Max u64
const assetId: BytesLike = '0x0f622143ec845f9095bdf02d80273ac48556efcf9f95c1fdadb9351fd8ffcd24';

const scriptData = formatTransferToContractScriptData([
{
contractId,
amount,
assetId,
},
]);

expect(scriptData).toStrictEqual(
new Uint8Array([
243, 235, 83, 237, 0, 52, 125, 48, 95, 198, 246, 227, 165, 126, 145, 234, 108, 49, 130, 169,
239, 194, 83, 72, 141, 178, 148, 148, 246, 60, 150, 16, 255, 255, 255, 255, 255, 255, 255,
255, 15, 98, 33, 67, 236, 132, 95, 144, 149, 189, 240, 45, 128, 39, 58, 196, 133, 86, 239,
207, 159, 149, 193, 253, 173, 185, 53, 31, 216, 255, 205, 36,
])
);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ASSET_ID_LEN, BigNumberCoder, CONTRACT_ID_LEN, WORD_SIZE } from '@fuel-ts/abi-coder';
import { Address } from '@fuel-ts/address';
import type { BytesLike } from '@fuel-ts/interfaces';
import { BN } from '@fuel-ts/math';
import type { BN } from '@fuel-ts/math';
import { arrayify, concat } from '@fuel-ts/utils';
import * as asm from '@fuels/vm-asm';

Expand All @@ -17,7 +17,7 @@ export const formatTransferToContractScriptData = (
const numberCoder = new BigNumberCoder('u64');
return transferParams.reduce((acc, transferParam) => {
const { assetId, amount, contractId } = transferParam;
const encoded = numberCoder.encode(new BN(amount).toNumber());
const encoded = numberCoder.encode(amount);
const scriptData = concat([
Address.fromAddressOrString(contractId).toBytes(),
encoded,
Expand Down
77 changes: 77 additions & 0 deletions packages/fuel-gauge/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,83 @@ describe('Contract', () => {
expect(finalBalance).toBe(initialBalance + amountToContract.toNumber());
});

it('should transferToContract with a large amount of assets', async () => {
using launched = await launchTestNode({
contractsConfigs,
walletsConfig: {
amountPerCoin: 2 ** 62,
},
});
const {
provider,
wallets: [wallet],
contracts: [contract],
} = launched;

const initialBalance = new BN(await contract.getBalance(provider.getBaseAssetId())).toNumber();
const amountToContract = bn(2).pow(62); // Very big number

const tx = await wallet.transferToContract(
contract.id,
amountToContract,
provider.getBaseAssetId()
);

await tx.waitForResult();

const finalBalance = new BN(await contract.getBalance(provider.getBaseAssetId())).toString();
expect(finalBalance).toBe(amountToContract.add(initialBalance).toString());
});

it('should batch transfer with a large amount of assets', async () => {
using launched = await launchTestNode({
contractsConfigs,
walletsConfig: {
amountPerCoin: 2 ** 62,
},
});
const {
provider,
wallets: [wallet],
contracts: [contract],
} = launched;

const baseAssetId = provider.getBaseAssetId();
const contractTransferParams: ContractTransferParams[] = [
{
contractId: contract.id,
amount: bn(2).pow(50),
assetId: baseAssetId,
},
{
contractId: contract.id,
amount: bn(2).pow(10),
assetId: baseAssetId,
},
];

const tx = await wallet.batchTransferToContracts(contractTransferParams);

const { receipts } = await tx.waitForResult();

const transferReceipts = receipts.filter(
({ type }) => type === ReceiptType.Transfer
) as ReceiptTransfer[];

expect(transferReceipts.length).toBe(contractTransferParams.length);

contractTransferParams.forEach(({ amount, contractId, assetId = baseAssetId }) => {
const foundReceipt = transferReceipts.find(
(r) =>
r.amount.eq(amount) &&
r.to.toLowerCase() === contractId.toString().toLowerCase() &&
r.assetId === assetId
);

expect(foundReceipt).toBeDefined();
danielbate marked this conversation as resolved.
Show resolved Hide resolved
});
});

it('should transfer assets to deployed contracts just fine', async () => {
using launched = await launchTestNode({
contractsConfigs: [
Expand Down
Loading