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

feat: add method to hash TX on TransactionRequest classes #1485

Merged
merged 34 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
cbaaeeb
remove provider and transaction from hasher package
Torres-ssf Dec 7, 2023
9c94e09
remove hashTransaction from hasher
Torres-ssf Dec 7, 2023
d240646
implement hashTransaction on providers package
Torres-ssf Dec 7, 2023
5c13a73
adding fixture
Torres-ssf Dec 7, 2023
bd67fd1
add method getTransactionId to baseTransactionRequest
Torres-ssf Dec 7, 2023
e0d8870
implementing getTransactionId on transaction request classes
Torres-ssf Dec 7, 2023
91afaaf
add hasher package to providers
Torres-ssf Dec 7, 2023
7198612
hashing tx directly from request
Torres-ssf Dec 7, 2023
ae55105
add changeset
Torres-ssf Dec 7, 2023
40d5110
using private instead of #
Torres-ssf Dec 7, 2023
7809d27
implement createTransfer on account class
Torres-ssf Dec 7, 2023
c456a60
add test for Account createTransfer
Torres-ssf Dec 7, 2023
4f0fd7c
add createTransfer on predicate class
Torres-ssf Dec 7, 2023
6356a90
updating docs
Torres-ssf Dec 7, 2023
9b6e986
remove TODO
Torres-ssf Dec 8, 2023
89b6704
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 8, 2023
1ca9d0c
Re-triggering workflow
Torres-ssf Dec 8, 2023
bd9220f
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 10, 2023
13e4e58
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
arboleya Dec 11, 2023
54ef0a8
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
arboleya Dec 11, 2023
84bd31e
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 11, 2023
956749e
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
danielbate Dec 11, 2023
252dac1
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
danielbate Dec 11, 2023
d923865
add doc block
Torres-ssf Dec 12, 2023
9c0b6da
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 12, 2023
7a7e67f
moving page transfering assets to cookbook section
Torres-ssf Dec 12, 2023
6d64bf3
Merge branch 'st/feat/hash-tx-on-transaction-request' of github.com:F…
Torres-ssf Dec 12, 2023
95dda6d
improving doc page Asset Transfers
Torres-ssf Dec 12, 2023
dc9e3e5
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
danielbate Dec 12, 2023
6a49d7d
re-triggering workflow
Torres-ssf Dec 12, 2023
d9b48d6
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 12, 2023
d03a65a
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 13, 2023
74fbc98
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 13, 2023
492c5d8
Merge branch 'master' into st/feat/hash-tx-on-transaction-request
Torres-ssf Dec 13, 2023
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
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
Loading