Skip to content

Commit

Permalink
Merge branch 'master' into mc/fix/adjust-global-cache-instances
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad authored Dec 9, 2024
2 parents fa6ac3f + 7390114 commit 1e2051e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
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();
});
});

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

0 comments on commit 1e2051e

Please sign in to comment.