-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: create evm address interface * feat: create evm address clas * feat: unit tests for evm address wrapper class * chore: linting * chore: linting * feat: support from public key in evm address * feat: remove evm address class and unit tests * feat: create types for B256 Evm and Evm address * feat: create address utility function to convert a b256 to an evm b256 * feat: add address function to return as evm addres * feat: add unit tests for address to evm address and utility function * feat: create snippets util function generate test wallet and deploy a contract * test: create docs snippets tests for evm address * chore: remove redundant gitignore from evm address doc snippet contract * test: rename evm address docs snippet test * docs: create documentation for evm address * docs: modify docs snippets for evm address * docs: add evm address to docs config * test: alter evm address test asserttions * docs: update evm address title capitalisation * feat: add evm address type to typegen * test: add unit test for evm address type with forc project * test: add evm address to full typegen test suit * feat: add ignore evm struct to struct typegen * feat: add evm address type to supported types in typegen * chore: changeset * feat: add forc projects to workspace * test: add address import to dts test * Update packages/abi-typegen/test/fixtures/forc-projects/evm-address/Forc.toml Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com> * feat: remove redundant gitignore from evm address cargo * feat: remove address import for evm address typegen * chore: force rebuild * feat: DRY evm address contract in docs snippets * refactor: DRY EVM Address sway contract in snippets * test: remove address import assert from typegen evm test * feat: support from evm address in address --------- Co-authored-by: danielbate <--global> Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com>
- Loading branch information
1 parent
df7573f
commit ac9362c
Showing
25 changed files
with
349 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@fuel-ts/abi-typegen": patch | ||
"@fuel-ts/address": patch | ||
"@fuel-ts/interfaces": patch | ||
--- | ||
|
||
Support EVM Address type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[project] | ||
authors = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "echo-evm-address" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// #region evm-address-1 | ||
contract; | ||
|
||
use std::vm::evm::evm_address::EvmAddress; | ||
|
||
configurable { | ||
B256_ADDR: b256 = 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6, | ||
} | ||
|
||
abi EvmTest { | ||
fn echo_address() -> EvmAddress; | ||
fn echo_address_comparison(evm_addr: EvmAddress) -> bool; | ||
} | ||
|
||
impl EvmTest for Contract { | ||
fn echo_address() -> EvmAddress { | ||
return EvmAddress::from(B256_ADDR); | ||
} | ||
|
||
fn echo_address_comparison(evm_addr: EvmAddress) -> bool { | ||
let evm_addr2 = EvmAddress::from(B256_ADDR); | ||
|
||
evm_addr == evm_addr2 | ||
} | ||
} | ||
// #endregion evm-address-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { B256AddressEvm, Contract, EvmAddress } from 'fuels'; | ||
import { Address } from 'fuels'; | ||
|
||
import { SnippetProjectEnum } from '../../../projects'; | ||
import { createAndDeployContractFromProject } from '../../utils'; | ||
|
||
describe('EvMAddress', () => { | ||
let contract: Contract; | ||
const Bits256: B256AddressEvm = | ||
'0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6'; | ||
|
||
beforeAll(async () => { | ||
contract = await createAndDeployContractFromProject(SnippetProjectEnum.ECHO_EVM_ADDRESS); | ||
}); | ||
|
||
it('should demonstrate typed evm address example', () => { | ||
// #region evm-address-1 | ||
// #context import type { EvmAddress } from 'fuels'; | ||
|
||
const evmAddress: EvmAddress = { | ||
value: Bits256, | ||
}; | ||
// #endregion evm-address-1 | ||
|
||
expect(evmAddress.value).toBe(Bits256); | ||
}); | ||
|
||
it('should create an Evm Address from a B256Address', async () => { | ||
// #region evm-address-2 | ||
// #context import type { EvmAddress } from 'fuels'; | ||
// #context import { Address } from 'fuels'; | ||
|
||
const b256Address = '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6'; | ||
|
||
const address = Address.fromB256(b256Address); | ||
|
||
const evmAddress: EvmAddress = address.toEvmAddress(); | ||
// #endregion evm-address-2 | ||
|
||
const { value } = await contract.functions.echo_address_comparison(evmAddress).get(); | ||
|
||
expect(value).toBeTruthy(); | ||
}); | ||
|
||
it('should pass an evm address to a contract', async () => { | ||
// #region evm-address-3 | ||
// #context import type { EvmAddress } from 'fuels'; | ||
|
||
const evmAddress: EvmAddress = { | ||
value: Bits256, | ||
}; | ||
|
||
const { value } = await contract.functions.echo_address_comparison(evmAddress).get(); | ||
|
||
expect(value).toBeTruthy(); | ||
// #endregion evm-address-3 | ||
}); | ||
|
||
it('should retrieve an evm address from a contract', async () => { | ||
// #region evm-address-4 | ||
// #context import type { EvmAddress } from 'fuels'; | ||
|
||
const evmAddress: EvmAddress = { | ||
value: Bits256, | ||
}; | ||
|
||
const { value } = await contract.functions.echo_address().get(); | ||
|
||
expect(value).toEqual(evmAddress); | ||
// #endregion evm-address-4 | ||
|
||
expect(value.value).toEqual(Bits256); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# EvmAddress | ||
|
||
An Ethereum Virtual Machine (EVM) Address can be represented using the `EvmAddress` type. It's definition matches the Sway standard library type being a `Struct` wrapper around an inner `Bits256` value. | ||
|
||
<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-1{ts:line-numbers} | ||
|
||
## Creating an EVM Address | ||
|
||
An EVM Address only has 20 bytes therefore the first 12 bytes of the `Bits256` value are set to 0. Within the SDK, an `Address` can be instantiated and converted to an EVM Address using the `toEvmAddress()` function: | ||
|
||
<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-2{ts:line-numbers} | ||
|
||
## Using an EVM Address | ||
|
||
The `EvmAddress` type can be integrated with your contract calls. Consider the following contract that can compare and return an EVM Address: | ||
|
||
<<< @/../../docs-snippets/projects/echo-evm-address/src/main.sw#evm-address-1{ts:line-numbers} | ||
|
||
The `EvmAddress` type can be used with the SDK and passed to the contract function as follows: | ||
|
||
<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-3{ts:line-numbers} | ||
|
||
And to validate the returned value: | ||
|
||
<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-4{ts:line-numbers} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { getProjectResources, ForcProjectsEnum } from '../../../test/fixtures/forc-projects/index'; | ||
import type { IRawAbiTypeRoot } from '../../index'; | ||
import { findType } from '../../utils/findType'; | ||
import { makeType } from '../../utils/makeType'; | ||
import * as parseTypeArgumentsMod from '../../utils/parseTypeArguments'; | ||
|
||
import { EvmAddressType } from './EvmAddressType'; | ||
import { StructType } from './StructType'; | ||
import { VectorType } from './VectorType'; | ||
|
||
describe('EvmAddressType.ts', () => { | ||
test('should properly parse type attributes', () => { | ||
const parseTypeArguments = jest.spyOn(parseTypeArgumentsMod, 'parseTypeArguments'); | ||
|
||
const project = getProjectResources(ForcProjectsEnum.EVM_ADDRESS); | ||
|
||
const rawTypes = project.abiContents.types; | ||
const types = rawTypes.map((rawAbiType: IRawAbiTypeRoot) => makeType({ rawAbiType })); | ||
|
||
const suitableForEvmAddress = EvmAddressType.isSuitableFor({ type: EvmAddressType.swayType }); | ||
const suitableForStruct = EvmAddressType.isSuitableFor({ type: StructType.swayType }); | ||
const suitableForVector = EvmAddressType.isSuitableFor({ type: VectorType.swayType }); | ||
|
||
expect(suitableForEvmAddress).toEqual(true); | ||
expect(suitableForStruct).toEqual(false); | ||
expect(suitableForVector).toEqual(false); | ||
|
||
parseTypeArguments.mockClear(); | ||
|
||
const evmAddress = findType({ types, typeId: 1 }) as EvmAddressType; | ||
|
||
expect(evmAddress.attributes.inputLabel).toEqual('EvmAddress'); | ||
expect(evmAddress.attributes.outputLabel).toEqual('EvmAddress'); | ||
expect(evmAddress.requiredFuelsMembersImports).toStrictEqual(['EvmAddress']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { IType } from '../../types/interfaces/IType'; | ||
|
||
import { AType } from './AType'; | ||
|
||
export class EvmAddressType extends AType implements IType { | ||
public static swayType = 'struct EvmAddress'; | ||
|
||
public name = 'evmAddress'; | ||
|
||
static MATCH_REGEX: RegExp = /^struct EvmAddress$/m; | ||
|
||
static isSuitableFor(params: { type: string }) { | ||
return EvmAddressType.MATCH_REGEX.test(params.type); | ||
} | ||
|
||
public parseComponentsAttributes(_params: { types: IType[] }) { | ||
const capitalizedName = 'EvmAddress'; | ||
|
||
this.attributes = { | ||
inputLabel: capitalizedName, | ||
outputLabel: capitalizedName, | ||
}; | ||
|
||
this.requiredFuelsMembersImports = [capitalizedName]; | ||
|
||
return this.attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/abi-typegen/test/fixtures/forc-projects/evm-address/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[project] | ||
authors = ["FuelLabs"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "evm-address" | ||
|
||
[dependencies] |
13 changes: 13 additions & 0 deletions
13
packages/abi-typegen/test/fixtures/forc-projects/evm-address/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
contract; | ||
|
||
use std::vm::evm::evm_address::EvmAddress; | ||
|
||
abi EvmAddressTest { | ||
fn main(raw_address: b256) -> EvmAddress; | ||
} | ||
|
||
impl EvmAddressTest for Contract { | ||
fn main(raw_address: b256) -> EvmAddress { | ||
EvmAddress::from(raw_address) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.