Skip to content

Commit

Permalink
chore: Address constructor now accepts a range of inputs. (#3600)
Browse files Browse the repository at this point in the history
* chore: nested address test

* chore: refactored the `Address` DX

* chore: update comment

* chore: changeset

* docs: update address docs

* chore: update all `Address` factory methods

* Update .changeset/chilly-buses-add.md

Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com>

---------

Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com>
  • Loading branch information
petertonysmith94 and Torres-ssf authored Jan 24, 2025
1 parent 68e4b5a commit 86b8e94
Show file tree
Hide file tree
Showing 36 changed files with 573 additions and 384 deletions.
8 changes: 8 additions & 0 deletions .changeset/chilly-buses-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@internal/check-imports": patch
"@fuel-ts/account": patch
"@fuel-ts/address": patch
"@fuel-ts/program": patch
---

chore: `Address` constructor now accepts a range of inputs.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const baseAssetId = await provider.getBaseAssetId();
// 0x...

// Instantiate our recipients address
const recipientAddress = Address.fromAddressOrString(WALLET_ADDRESS);
const recipientAddress = new Address(WALLET_ADDRESS);

// Create a transaction request
const transactionRequest = new ScriptTransactionRequest();
Expand Down
26 changes: 18 additions & 8 deletions apps/docs/src/guide/types/address.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,28 @@ To create an [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_a

<<< @./snippets/address/from-a-public-key.ts#full{ts:line-numbers}

## Utility Functions
### From an EVM Address

The [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) class also provides some practical utility functions:
To create an [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) from an EVM address, use the following code snippet:

1. `fromString`: Create a new [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) from an ambiguous source that may be a `B256` address:
<<< @./snippets/address/from-an-evm-address.ts#full{ts:line-numbers}

<<< @./snippets/address/utilities-function-1.ts#full{ts:line-numbers}
### From an existing Address

2. `fromDynamicInput`: Create a new [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) when the address source is unknown:
To create an [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) from an existing [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) instance, use the following code snippet:

<<< @./snippets/address/utilities-function-2.ts#full{ts:line-numbers}
<<< @./snippets/address/from-an-existing-address.ts#full{ts:line-numbers}

3. `equals:` As you may already notice, the `equals` function can compare addresses instances:
## Utility functions

<<< @./snippets/address/utilities-function-3.ts#full{ts:line-numbers}
### `equals`

As you may already notice, the `equals` function can compare addresses instances:

<<< @./snippets/address/utilities-function-equals.ts#full{ts:line-numbers}

### `toChecksum`

To convert an address to a checksum address, use the `toChecksum` function:

<<< @./snippets/address/utilities-function-to-checksum.ts#full{ts:line-numbers}
2 changes: 1 addition & 1 deletion apps/docs/src/guide/types/snippets/address/from-a-b256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const b256 =
'0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';
// #endregion b256-1

const address = Address.fromB256(b256);
const address = new Address(b256);

console.log('b256', address.toB256());
// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const provider = new Provider(LOCAL_NETWORK_URL);

const wallet = Wallet.generate({ provider });

const address = Address.fromPublicKey(wallet.publicKey);
const address = new Address(wallet.publicKey);
// #endregion full

console.log('address', address);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// #region full
import { Address } from 'fuels';

const evmAddress = '0x675b68aa4d9c2d3bb3f0397048e62e6b7192079c';

const address = new Address(evmAddress);
// #endregion full

console.log('address', address);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// #region full
import { Address } from 'fuels';

const address = Address.fromRandom();

const addressClone = new Address(address);
// #endregion full

console.log('addressCloneFromB256', addressClone);

This file was deleted.

10 changes: 0 additions & 10 deletions apps/docs/src/guide/types/snippets/address/utilities-function-2.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Address } from 'fuels';

const address = Address.fromRandom();

const address1 = Address.fromString(address.toString());
const address2 = Address.fromString(address.toB256());
const address1 = new Address(address.toString());
const address2 = new Address(address.toB256());

console.log('equals', address1.equals(address2));
// true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// #region full
import { Address } from 'fuels';

const b256 =
'0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';

const address = new Address(b256);

console.log('checksum', address.toChecksum());
// true
// #endregion full
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getRandomB256, Address } from 'fuels';

const randomB256: string = getRandomB256();

const address = Address.fromB256(randomB256);
const address = new Address(randomB256);
// #endregion full

console.log('address', address);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const b256Address =
'0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';
// #endregion snippet-2

const address = Address.fromB256(b256Address);
const address = new Address(b256Address);

const evmAddress = address.toEvmAddress();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const response1 = await contract.functions.address(addressInput).get();

// #region address-output
const addressOutput = response1.value;
const addressFromOutput: Address = Address.fromB256(addressOutput.bits);
const addressFromOutput: Address = new Address(addressOutput.bits);
// #endregion address-output

console.log('equals', addressFromOutput.equals(address));
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const response = await contract.functions.identity(addressIdentityInput).get();
const identityFromOutput: IdentityOutput = response.value;
const addressStringFromOutput: AddressOutput =
identityFromOutput.Address as AddressOutput;
const addressFromOutput = Address.fromB256(addressStringFromOutput.bits);
const addressFromOutput = new Address(addressStringFromOutput.bits);
// #endregion identity-address-output

console.log('equals', addressFromOutput.equals(address));
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Address } from 'fuels';

const b256: B256Address =
'0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f';
const address: Address = Address.fromB256(b256);
const address: Address = new Address(b256);
const assetId: AssetId = address.toAssetId();
// {
// bits: '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Counter } from '../../../../typegend/contracts';
const provider = new Provider(LOCAL_NETWORK_URL);

const contractAbi = Counter.abi;
const contractAddress = Address.fromB256(
const contractAddress = new Address(
'0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LOCAL_NETWORK_URL } from '../../../../env';

const provider = new Provider(LOCAL_NETWORK_URL);

const address = Address.fromB256(
const address = new Address(
'0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f'
);

Expand Down
2 changes: 1 addition & 1 deletion internal/check-imports/src/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ log(runCliAction);
* address
*/
log(Address);
log(Address.fromPublicKey('asdfasdf'));
log(new Address('asdfasdf'));

/**
* contract
Expand Down
14 changes: 7 additions & 7 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UTXO_ID_LEN } from '@fuel-ts/abi-coder';
import type { WithAddress } from '@fuel-ts/address';
import type { AddressInput, WithAddress } from '@fuel-ts/address';
import { Address } from '@fuel-ts/address';
import { randomBytes } from '@fuel-ts/crypto';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
Expand Down Expand Up @@ -102,11 +102,11 @@ export class Account extends AbstractAccount implements WithAddress {
* @param provider - A Provider instance (optional).
* @param connector - A FuelConnector instance (optional).
*/
constructor(address: string | Address, provider?: Provider, connector?: FuelConnector) {
constructor(address: AddressInput, provider?: Provider, connector?: FuelConnector) {
super();
this._provider = provider;
this._connector = connector;
this.address = Address.fromDynamicInput(address);
this.address = new Address(address);
}

/**
Expand Down Expand Up @@ -408,7 +408,7 @@ export class Account extends AbstractAccount implements WithAddress {
addTransfer(request: ScriptTransactionRequest, transferParams: TransferParams) {
const { destination, amount, assetId } = transferParams;
this.validateTransferAmount(amount);
request.addCoinOutput(Address.fromAddressOrString(destination), amount, assetId);
request.addCoinOutput(new Address(destination), amount, assetId);
return request;
}

Expand Down Expand Up @@ -462,7 +462,7 @@ export class Account extends AbstractAccount implements WithAddress {

const transferParams = contractTransferParams.map((transferParam) => {
const amount = bn(transferParam.amount);
const contractAddress = Address.fromAddressOrString(transferParam.contractId);
const contractAddress = new Address(transferParam.contractId);

const assetId = transferParam.assetId ? hexlify(transferParam.assetId) : defaultAssetId;

Expand Down Expand Up @@ -502,11 +502,11 @@ export class Account extends AbstractAccount implements WithAddress {
* @returns A promise that resolves to the transaction response.
*/
async withdrawToBaseLayer(
recipient: string | Address,
recipient: AddressInput,
amount: BigNumberish,
txParams: TxParamsType = {}
): Promise<TransactionResponse> {
const recipientAddress = Address.fromAddressOrString(recipient);
const recipientAddress = new Address(recipient);
// add recipient and amount to the transaction script code
const recipientDataArray = arrayify(
'0x'.concat(recipientAddress.toHexString().substring(2).padStart(64, '0'))
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Predicate<
abi,
configurableConstants
);
const address = Address.fromB256(getPredicateRoot(predicateBytes));
const address = new Address(getPredicateRoot(predicateBytes));
super(address, provider);

this.initialBytecode = arrayify(bytecode);
Expand Down
Loading

0 comments on commit 86b8e94

Please sign in to comment.