Skip to content

Commit 3f8b514

Browse files
authored
feat: getCreate2 Address (#1299)
* feat: getCreate2 Address * REvert
1 parent 87b258e commit 3f8b514

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

.changeset/violet-chairs-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-ts': minor
3+
---
4+
5+
introduce new Etherum utility to get a CREATE2 Address

packages/ts/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,32 @@ export namespace log {
137137
log.log(Level.DEBUG, format(msg, args));
138138
}
139139
}
140+
141+
/**
142+
* Helper functions for Ethereum.
143+
*/
144+
export namespace EthereumUtils {
145+
/**
146+
* Returns the contract address that would result from the given CREATE2 call.
147+
* @param from The Ethereum address of the account that is initiating the contract creation.
148+
* @param salt A 32-byte value that is used to create a deterministic address for the contract. This can be any arbitrary value, but it should be unique to the contract being created.
149+
* @param initCodeHash he compiled code that will be executed when the contract is created. This should be a hex-encoded string that represents the compiled bytecode.
150+
* @returns Address of the contract that would be created.
151+
*/
152+
export function getCreate2Address(from: Bytes, salt: Bytes, initCodeHash: Bytes): Bytes {
153+
return Bytes.fromHexString(
154+
Bytes.fromByteArray(
155+
crypto.keccak256(
156+
Bytes.fromHexString(
157+
'0xff' +
158+
from.toHexString().slice(2) +
159+
salt.toHexString().slice(2) +
160+
initCodeHash.toHexString().slice(2),
161+
),
162+
),
163+
)
164+
.toHexString()
165+
.slice(26),
166+
);
167+
}
168+
}

0 commit comments

Comments
 (0)