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: avoid fundFakeUtxo changing inputs/outputs of the transaction #1528

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion .changeset/violet-mice-rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@fuel-ts/program": minor
"@fuel-ts/providers": minor
"@fuel-ts/script": minor
"@fuel-ts/testcases": minor
"@fuel-ts/transactions": minor
"@fuel-ts/utils": minor
"@fuel-ts/versions": minor
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: "Release PR to npm"
runs-on: ubuntu-latest
# comment out if:false to enable release PR to npm
if: false
# if: false
permissions: write-all
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"prettier.configPath": ".prettierrc",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"files.exclude": {
"**/.git": true,
Expand Down
65 changes: 32 additions & 33 deletions packages/providers/src/transaction-request/transaction-request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Address, addressify, getRandomB256 } from '@fuel-ts/address';

Check warning on line 1 in packages/providers/src/transaction-request/transaction-request.ts

View workflow job for this annotation

GitHub Actions / test

'getRandomB256' is defined but never used. Allowed unused vars must match /^_/u
import { BaseAssetId, ZeroBytes32 } from '@fuel-ts/address/configs';
import type { AddressLike, AbstractAddress, AbstractPredicate } from '@fuel-ts/interfaces';
import type { BN, BigNumberish } from '@fuel-ts/math';
import { bn } from '@fuel-ts/math';
import type { TransactionScript, Policy, TransactionCreate } from '@fuel-ts/transactions';
import type {
TransactionScript,
Policy,
TransactionCreate,
InputCoin,

Check warning on line 10 in packages/providers/src/transaction-request/transaction-request.ts

View workflow job for this annotation

GitHub Actions / test

'InputCoin' is defined but never used. Allowed unused vars must match /^_/u
} from '@fuel-ts/transactions';
import {
PolicyType,
TransactionCoder,
Expand Down Expand Up @@ -559,42 +564,36 @@
* @param quantities - CoinQuantity Array.
*/
fundWithFakeUtxos(quantities: CoinQuantity[]) {
const hasBaseAssetId = quantities.some(({ assetId }) => assetId === BaseAssetId);

if (!hasBaseAssetId) {
quantities.push({ assetId: BaseAssetId, amount: bn(1) });
}

const owner = getRandomB256();

const witnessToRemove = this.inputs.reduce(
(acc, input) => {
if (input.type === InputType.Coin || input.type === InputType.Message) {
if (!acc[input.witnessIndex]) {
acc[input.witnessIndex] = true;
}
const getId = () => ZeroBytes32 + String(Math.ceil(Math.random() * 99)).padStart(2, '0');
const addFakeUTXO = (assetId: string, quantity: BN) => {
const assetInput = this.inputs.find((i) => {
if ('assetId' in i) {
return i.assetId === assetId;
}
return false;
});

return acc;
},
{} as Record<number, boolean>
);

this.witnesses = this.witnesses.filter((_, idx) => !witnessToRemove[idx]);
this.inputs = this.inputs.filter((input) => input.type === InputType.Contract);
this.outputs = this.outputs.filter((output) => output.type !== OutputType.Change);
if (assetInput && 'assetId' in assetInput) {
assetInput.id = getId();
assetInput.amount = quantity;
return;
}

const fakeResources = quantities.map(({ assetId, amount }, idx) => ({
id: `${ZeroBytes32}0${idx}`,
amount,
assetId,
owner: Address.fromB256(owner),
maturity: 0,
blockCreated: bn(1),
txCreatedIdx: bn(1),
}));
this.addResources([
{
id: getId(),
amount: quantity,
assetId,
owner: Address.fromRandom(),
maturity: 0,
blockCreated: bn(1),
txCreatedIdx: bn(1),
},
]);
};

this.addResources(fakeResources);
addFakeUTXO(BaseAssetId, bn(100_000_000_000));
quantities.map((q) => addFakeUTXO(q.assetId, q.amount));
}

/**
Expand Down
Loading