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: consider assetId in check account utility #3618

Draft
wants to merge 7 commits into
base: st/chore/[email protected]
Choose a base branch
from
Draft
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/fifty-seals-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

feat: consider `assetId` in check account utility
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module.exports = {
'import/prefer-default-export': 'off',
'tsdoc/syntax': 'warn',
'require-await': 'off',
'no-empty': ['error', { allowEmptyCatch: true }],
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-unused-vars': [
Expand Down
30 changes: 19 additions & 11 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export type GetBlocksResponse = {
pageInfo: PageInfo;
};

export type GetAddressTypeResponse = 'Account' | 'Contract' | 'Transaction' | 'Blob' | 'Asset';

/**
* Deployed Contract bytecode and contract id
*/
Expand Down Expand Up @@ -2193,19 +2195,17 @@ export default class Provider {
* @returns A promise that resolves to the result of the check.
*/
async isUserAccount(id: string): Promise<boolean> {
const { contract, blob, transaction } = await this.operations.isUserAccount({
blobId: id,
contractId: id,
transactionId: id,
});

if (contract || blob || transaction) {
return false;
}
return true;
const type = await this.getAddressType(id);
return type === 'Account';
}

async getAddressType(id: string): Promise<'Account' | 'Contract' | 'Transaction' | 'Blob'> {
/**
* Determines the type of address based on the provided ID.
*
* @param id - The ID to be checked.
* @returns A promise that resolves to a string indicating the type of address.
*/
async getAddressType(id: string): Promise<GetAddressTypeResponse> {
const { contract, blob, transaction } = await this.operations.isUserAccount({
blobId: id,
contractId: id,
Expand All @@ -2222,6 +2222,14 @@ export default class Provider {
return 'Transaction';
}

try {
// Unlike the previous queries this one will throw if the ID is not an assetId
const asset = await this.getAssetDetails(id);
if (asset) {
return 'Asset';
}
} catch (e) {}

return 'Account';
}

Expand Down
51 changes: 49 additions & 2 deletions packages/fuel-gauge/src/check-user-account.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { launchTestNode } from 'fuels/test-utils';

import { TokenContractFactory } from '../test/typegen';
import { AdvancedLoggingFactory } from '../test/typegen/contracts/AdvancedLoggingFactory';
import { ScriptMainArgBool } from '../test/typegen/scripts';

Expand Down Expand Up @@ -66,14 +67,60 @@ describe('User account tests', () => {
expect(await provider.isUserAccount(tx.id)).toBe(false);
});

it('should return false for a Asset ID', async () => {
using launch = await launchTestNode({
contractsConfigs: [{ factory: TokenContractFactory }],
});

const {
provider,
contracts: [tokenContract],
} = launch;

const call = await tokenContract.functions.mint_coins(1).call();
const {
transactionResult: { mintedAssets },
} = await call.waitForResult();

const [{ assetId }] = mintedAssets;

expect(await provider.isUserAccount(assetId)).toBe(false);
});

it('should return the correct address type', async () => {
using launch = await launchTestNode();
using launch = await launchTestNode({
contractsConfigs: [{ factory: TokenContractFactory }],
});

const {
provider,
wallets: [wallet],
contracts: [tokenContract],
} = launch;

expect(await provider.getAddressType(wallet.address.toB256())).toBe('Account');
const script = new ScriptMainArgBool(wallet);
const { blobId, waitForResult } = await script.deploy(wallet);
await waitForResult();

const call = await tokenContract.functions.mint_coins(1000000).call();
const {
transactionResult: { mintedAssets, id },
} = await call.waitForResult();
const [{ assetId }] = mintedAssets;

let type = await provider.getAddressType(assetId);
expect(type).toBe('Asset');

type = await provider.getAddressType(id);
expect(type).toBe('Transaction');

type = await provider.getAddressType(blobId);
expect(type).toBe('Blob');

type = await provider.getAddressType(tokenContract.id.toB256());
expect(type).toBe('Contract');

type = await provider.getAddressType(wallet.address.toB256());
expect(type).toBe('Account');
});
});