Skip to content

Commit

Permalink
feat: add method to hash TX on TransactionRequest classes (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Dec 13, 2023
1 parent 166a48b commit a3ca540
Show file tree
Hide file tree
Showing 27 changed files with 641 additions and 346 deletions.
8 changes: 8 additions & 0 deletions .changeset/brown-zebras-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@fuel-ts/hasher": minor
"@fuel-ts/program": minor
"@fuel-ts/providers": minor
"@fuel-ts/wallet": minor
---

add method to hash tx on TransactionRequest classes
163 changes: 163 additions & 0 deletions apps/docs-snippets/src/guide/cookbook/transferring-assets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import type { Contract, Provider, TxParams, WalletUnlocked } from 'fuels';
import { Address, BN, ContractFactory, BaseAssetId, Wallet } from 'fuels';

import {
DocSnippetProjectsEnum,
getDocsSnippetsForcProject,
} from '../../../test/fixtures/forc-projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
let sender: WalletUnlocked;
let deployedContract: Contract;
let provider: Provider;

beforeAll(async () => {
sender = await getTestWallet();

const { abiContents, binHexlified } = getDocsSnippetsForcProject(
DocSnippetProjectsEnum.COUNTER
);
provider = sender.provider;
const factory = new ContractFactory(binHexlified, abiContents, sender);
const { minGasPrice } = sender.provider.getGasConfig();
deployedContract = await factory.deployContract({ gasPrice: minGasPrice });
});

it('should successfully transfer asset to another account', async () => {
// #region transferring-assets-1
// #context import { Wallet, BN, BaseAssetId } from 'fuels';

// #context const sender = Wallet.fromPrivateKey('...');
const destination = Wallet.generate({
provider: sender.provider,
});
const amountToTransfer = 500;
const assetId = BaseAssetId;

const { minGasPrice } = provider.getGasConfig();

const txParams: TxParams = {
gasPrice: minGasPrice,
gasLimit: 1_000,
};

const response = await sender.transfer(
destination.address,
amountToTransfer,
assetId,
txParams
);

await response.wait();

// Retrieve balances
const receiverBalance = await destination.getBalance(assetId);

// Validate new balance
expect(new BN(receiverBalance).toNumber()).toEqual(amountToTransfer);
// #endregion transferring-assets-1
});

it('should successfully prepare transfer to another account', async () => {
const destination = Wallet.generate({
provider: sender.provider,
});

const amountToTransfer = 200;
const assetId = BaseAssetId;

const { minGasPrice } = provider.getGasConfig();

const txParams: TxParams = {
gasPrice: minGasPrice,
gasLimit: 1_000,
};

// #region transferring-assets-2
const transactionRequest = await sender.createTransfer(
destination.address,
amountToTransfer,
assetId,
txParams
);

const chainId = provider.getChainId();

const transactionId = transactionRequest.getTransactionId(chainId);

const response = await sender.sendTransaction(transactionRequest);

const { id } = await response.wait();

// The transaction id should is the same as the one returned by the transaction request
expect(id).toEqual(transactionId);
// #endregion transferring-assets-2
});

it('should validate that modifying the transaction request will result in another TX ID', async () => {
const destination = Wallet.generate({
provider: sender.provider,
});

const amountToTransfer = 200;
const assetId = BaseAssetId;

const { minGasPrice } = provider.getGasConfig();

const txParams: TxParams = {
gasPrice: minGasPrice,
gasLimit: 1_000,
};

// #region transferring-assets-3
const transactionRequest = await sender.createTransfer(
destination.address,
amountToTransfer,
assetId,
txParams
);

const chainId = provider.getChainId();

const transactionId = transactionRequest.getTransactionId(chainId);

transactionRequest.maturity = 1;

const response = await sender.sendTransaction(transactionRequest);

const { id } = await response.wait();

expect(id).not.toEqual(transactionId);
// #endregion transferring-assets-3
});

it('should successfully prepare transfer transaction request', async () => {
const contractId = Address.fromAddressOrString(deployedContract.id);
// #region transferring-assets-4
// #context import { Wallet, BN, BaseAssetId } from 'fuels';

// #context const senderWallet = Wallet.fromPrivateKey('...');

const amountToTransfer = 400;
const assetId = BaseAssetId;
// #context const contractId = Address.fromAddressOrString('0x123...');

const contractBalance = await deployedContract.getBalance(assetId);

const { minGasPrice } = provider.getGasConfig();

const txParams: TxParams = {
gasPrice: minGasPrice,
gasLimit: 1_000,
};

const tx = await sender.transferToContract(contractId, amountToTransfer, assetId, txParams);
expect(new BN(contractBalance).toNumber()).toBe(0);

await tx.waitForResult();

expect(new BN(await deployedContract.getBalance(assetId)).toNumber()).toBe(amountToTransfer);
// #endregion transferring-assets-4
});
});
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
import { safeExec } from '@fuel-ts/errors/test-utils';
import type { Provider } from 'fuels';
import { WalletUnlocked, Predicate, BN, getRandomB256, BaseAssetId } from 'fuels';

import {
WalletUnlocked,
FUEL_NETWORK_URL,
Provider,
Predicate,
BN,
getRandomB256,
BaseAssetId,
} from 'fuels';

import { DocSnippetProjectsEnum, getDocsSnippetsForcProject } from '../../../test/fixtures/forc-projects';
DocSnippetProjectsEnum,
getDocsSnippetsForcProject,
} from '../../../test/fixtures/forc-projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
let walletWithFunds: WalletUnlocked;
let provider: Provider;
let gasPrice: BN;
const { abiContents: abi, binHexlified: bin } = getDocsSnippetsForcProject(
DocSnippetProjectsEnum.SIMPLE_PREDICATE
);

beforeAll(async () => {
walletWithFunds = await getTestWallet();
({ minGasPrice: gasPrice } = walletWithFunds.provider.getGasConfig());
provider = walletWithFunds.provider;
({ minGasPrice: gasPrice } = provider.getGasConfig());
});

it('should successfully use predicate to spend assets', async () => {
// #region send-and-spend-funds-from-predicates-2
const provider = await Provider.create(FUEL_NETWORK_URL);
const predicate = new Predicate(bin, provider, abi);
// #endregion send-and-spend-funds-from-predicates-2

// #region send-and-spend-funds-from-predicates-3
const amountToPredicate = 300_000;
const amountToPredicate = 10_000;

const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, BaseAssetId, {
gasPrice,
gasLimit: 10_000,
gasLimit: 1_000,
});

await tx.waitForResult();
Expand All @@ -58,11 +55,11 @@ describe(__filename, () => {

const tx2 = await predicate.transfer(
receiverWallet.address,
amountToPredicate - 150_000,
amountToPredicate - 1000,
BaseAssetId,
{
gasPrice,
gasLimit: 10_000,
gasLimit: 1_000,
}
);

Expand All @@ -71,14 +68,13 @@ describe(__filename, () => {
});

it('should fail when trying to spend predicates entire amount', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const predicate = new Predicate(bin, provider, abi);

const amountToPredicate = 100;

const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, BaseAssetId, {
gasPrice,
gasLimit: 10_000,
gasLimit: 1_000,
});

await tx.waitForResult();
Expand All @@ -94,7 +90,7 @@ describe(__filename, () => {
const { error } = await safeExec(() =>
predicate.transfer(receiverWallet.address, predicateBalance, BaseAssetId, {
gasPrice,
gasLimit: 10_000,
gasLimit: 1_000,
})
);

Expand All @@ -106,17 +102,16 @@ describe(__filename, () => {
});

it('should fail when set wrong input data for predicate', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const predicateOwner = WalletUnlocked.generate({
provider,
});
const predicate = new Predicate(bin, predicateOwner.provider, abi);

const amountToPredicate = 200_000;
const amountToPredicate = 10_000;

const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, BaseAssetId, {
gasPrice,
gasLimit: 10_000,
gasLimit: 1_000,
});

await tx.waitForResult();
Expand All @@ -130,7 +125,7 @@ describe(__filename, () => {
const { error } = await safeExec(() =>
predicate.transfer(receiverWallet.address, amountToPredicate, BaseAssetId, {
gasPrice,
gasLimit: 10_000,
gasLimit: 1_000,
})
);

Expand All @@ -140,4 +135,48 @@ describe(__filename, () => {

expect((<Error>error).message).toMatch(errorMsg);
});

it('should ensure predicate createTransfer works as expected', async () => {
const predicate = new Predicate(bin, provider, abi);

const amountToPredicate = 10_000;

const tx = await walletWithFunds.transfer(predicate.address, amountToPredicate, BaseAssetId, {
gasPrice,
gasLimit: 1_000,
});

await tx.waitForResult();

const inputAddress = '0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4';

predicate.setData(inputAddress);

const receiverWallet = WalletUnlocked.generate({
provider,
});

// #region send-and-spend-funds-from-predicates-8
const transactionRequest = await predicate.createTransfer(
receiverWallet.address,
amountToPredicate,
BaseAssetId,
{
gasPrice,
gasLimit: 1_000,
}
);

const chainId = provider.getChainId();

const txId = transactionRequest.getTransactionId(chainId);

const res = await predicate.sendTransaction(transactionRequest);

await res.waitForResult();
// #endregion send-and-spend-funds-from-predicates-8
const txIdFromExecutedTx = res.id;

expect(txId).toEqual(txIdFromExecutedTx);
});
});
Loading

0 comments on commit a3ca540

Please sign in to comment.