@ethersproject/solidity#keccak256 TypeScript Examples
The following examples show how to use
@ethersproject/solidity#keccak256.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getCafeSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
CAFESWAP_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
CAFESWAP_INIT_CODE_HASH
);
}
Example #2
Source File: pair.ts From sdk with MIT License | 6 votes |
public static getAddress(tokenA: Token, tokenB: Token, chainId: ChainId = ChainId.AVALANCHE): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
FACTORY_ADDRESS[chainId],
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
INIT_CODE_HASH
)
}
}
}
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
}
Example #3
Source File: pair.ts From pancake-swap-sdk with MIT License | 6 votes |
public static getAddress(tokenA: Token, tokenB: Token): string {
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
const key = composeKey(token0, token1)
if (PAIR_ADDRESS_CACHE?.[key] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[key]: getCreate2Address(
FACTORY_ADDRESS_MAP[token0.chainId],
keccak256(['bytes'], [pack(['address', 'address'], [token0.address, token1.address])]),
INIT_CODE_HASH_MAP[token0.chainId]
)
}
}
return PAIR_ADDRESS_CACHE[key]
}
Example #4
Source File: pair.ts From sushiswap-exchange with GNU General Public License v3.0 | 6 votes |
public static getAddress(tokenA: Token, tokenB: Token): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
FACTORY_ADDRESS,
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
INIT_CODE_HASH
)
}
}
}
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
}
Example #5
Source File: pair.ts From pancake-swap-sdk-testnet with MIT License | 6 votes |
public static getAddress(tokenA: Token, tokenB: Token): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
FACTORY_ADDRESS,
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
INIT_CODE_HASH
),
},
}
}
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
}
Example #6
Source File: pair.ts From vvs-ui with GNU General Public License v3.0 | 6 votes |
public static getAddress(tokenA: Token, tokenB: Token): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
const { chainId } = tokenA
if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
FACTORY_ADDRESSES[chainId],
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
INIT_CODE_HASHES[chainId]
),
},
}
}
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
}
Example #7
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getDefySwapPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
DEFYSWAP_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
DEFYSWAP_INIT_CODE_HASH
);
}
Example #8
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getTraderJoePairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
TRADERJOE_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
TRADERJOE_INIT_CODE_HASH
);
}
Example #9
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getPancakeSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
PANCAKESWAP_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
PANCAKESWAP_INIT_CODE_HASH
);
}
Example #10
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getPolydexPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
POLYDEX_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
POLYDEX_INIT_CODE_HASH
);
}
Example #11
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getUniswapPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
UNISWAP_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
UNISWAP_INIT_CODE_HASH
);
}
Example #12
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getSpookySwapPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
SPOOKY_SWAP_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
SPOOKY_SWAP_INIT_CODE_HASH
);
}
Example #13
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getQuickSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
QUICK_SWAP_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
QUICK_SWAP_INIT_CODE_HASH
);
}
Example #14
Source File: pairs.ts From limit-orders-lib with GNU General Public License v3.0 | 6 votes |
getSpiritSwapPairAddress = (tokenA: Token, tokenB: Token): string => {
const tokens = tokenA.sortsBefore(tokenB)
? [tokenA, tokenB]
: [tokenB, tokenA]; // does safety checks
return getCreate2Address(
SPIRIT_SWAP_FACTORY_ADDRESS,
keccak256(
["bytes"],
[pack(["address", "address"], [tokens[0].address, tokens[1].address])]
),
SPIRIT_SWAP_INIT_CODE_HASH
);
}
Example #15
Source File: pair.ts From Elastos.Essentials.App with MIT License | 6 votes |
computePairAddress = ({
initCodeHash,
factoryAddress,
tokenA,
tokenB
}: {
factoryAddress: string
initCodeHash: string
tokenA: Token
tokenB: Token
}): string => {
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
return getCreate2Address(
factoryAddress,
keccak256(['bytes'], [pack(['address', 'address'], [token0.address, token1.address])]),
initCodeHash
)
}
Example #16
Source File: pair.ts From spookyswap-sdk with MIT License | 6 votes |
public static getAddress(tokenA: Token, tokenB: Token): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
FACTORY_ADDRESS,
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
INIT_CODE_HASH
)
}
}
}
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
}
Example #17
Source File: pair.ts From QuickSwap-sdk with MIT License | 6 votes |
public static getAddress(tokenA: Token, tokenB: Token): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
FACTORY_ADDRESS,
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
INIT_CODE_HASH
)
}
}
}
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
}
Example #18
Source File: constants.test.ts From vvs-ui with GNU General Public License v3.0 | 5 votes |
COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])
Example #19
Source File: index.ts From snapshot-plugins with MIT License | 5 votes |
export function getSafeHash(safe: SafeData) {
const hashes = safe.txs.map((batch) => batch.hash);
const valid = hashes.every((hash) => hash);
if (!valid || !hashes.length) return null;
return keccak256(['bytes32[]'], [hashes]);
}
Example #20
Source File: constants.test.ts From pancake-swap-sdk with MIT License | 5 votes |
COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])
Example #21
Source File: constants.test.ts From pancake-swap-sdk-testnet with MIT License | 5 votes |
COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])
Example #22
Source File: constants.test.ts From uniswap-sdk with MIT License | 5 votes |
COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])
Example #23
Source File: constants.test.ts From QuickSwap-sdk with MIT License | 5 votes |
COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])
Example #24
Source File: provider.test.ts From ccip-read with MIT License | 4 votes |
describe('ethers-ccip-read-provider', () => {
const baseProvider = new ethers.providers.Web3Provider(ganache.provider());
const messageSigner = new ethers.Wallet(TEST_PRIVATE_KEY);
let ccipProvider: CCIPReadProvider;
let utilsContract: ethers.Contract;
let contract: ethers.Contract;
let account: string;
let snapshot: number;
const server = new Server();
server.add(
['function getSignedBalance(address addr) view returns(uint256 balance, bytes memory sig)'],
[
{
type: 'getSignedBalance',
func: async (args) => {
const [addr] = args;
const balance = ethers.BigNumber.from('1000000000000000000000');
let messageHash = keccak256(['uint256', 'address'], [balance, addr]);
let messageHashBinary = arrayify(messageHash);
const signature = await messageSigner.signMessage(messageHashBinary);
return [balance, signature];
},
},
]
);
function fetcher(url: string, json?: string, _processFunc?: (value: any, response: FetchJsonResponse) => any) {
if(json === undefined) {
const [_match, to, data] = url.match(/http:\/\/localhost:8000\/rpc\/([^/]+)\/([^/]+).json/) as RegExpMatchArray;
return server.call({ to, data });
} else {
expect(url).to.equal(TEST_POST_URL);
const {sender, data} = JSON.parse(json);
return server.call({ to: sender, data});
}
}
beforeAll(async () => {
const signer = await baseProvider.getSigner();
account = await signer.getAddress();
const proxyMiddleware = new RevertNormalisingMiddleware(baseProvider);
ccipProvider = new CCIPReadProvider(proxyMiddleware, fetcher);
utilsContract = (await deploySolidity(testUtils, signer)).connect(ccipProvider);
const c = await deploySolidity(token, signer, 'Test', 'TST', 0);
await c.setSigner(await messageSigner.getAddress());
await c.setUrls([TEST_URL]);
contract = c.connect(ccipProvider);
snapshot = await baseProvider.send('evm_snapshot', []);
});
afterEach(async () => {
await baseProvider.send('evm_revert', [snapshot]);
});
describe('CCIPReadProvider', () => {
it('passes calls through to the underlying provider', async () => {
const network = await baseProvider.getNetwork();
expect((await ccipProvider.getNetwork()).chainId).to.equal(network.chainId);
});
it('handles an OffchainLookup', async () => {
expect((await contract.connect(ccipProvider).balanceOf(account)).toString()).to.equal('1000000000000000000000');
});
it('handles an OffchainLookup via POST', async () => {
await contract.connect(await baseProvider.getSigner()).setUrls([TEST_POST_URL]);
expect((await contract.connect(ccipProvider).balanceOf(account)).toString()).to.equal('1000000000000000000000');
});
it('throws an error if the OffchainLookup is thrown in a nested scope', async () => {
await expect(utilsContract.balanceOf(contract.address, account)).to.be.rejectedWith(
'OffchainLookup thrown in nested scope'
);
});
});
describe('CCIPReadSigner', () => {
let signer: CCIPReadSigner;
beforeAll(async () => {
signer = await ccipProvider.getSigner();
});
it('sends regular transactions', async () => {
await contract.connect(signer).setUrls([TEST_URL]);
expect(await contract.urls(0)).to.equal(TEST_URL);
});
it('translates CCIP read transactions', async () => {
expect((await contract.connect(signer).balanceOf(account)).toString()).to.equal('1000000000000000000000');
const tx = await contract.connect(signer).transfer(TEST_ACCOUNT, '1000000000000000000');
const receipt = await tx.wait();
expect(receipt.status).to.equal(1);
expect((await contract.balanceOf(account)).toString()).to.equal('999000000000000000000');
expect((await contract.balanceOf(TEST_ACCOUNT)).toString()).to.equal('1001000000000000000000');
});
});
});
Example #25
Source File: MasterDeployer.test.ts From trident with GNU General Public License v3.0 | 4 votes |
describe("MasterDeployer", function () {
before(async function () {
this.feeTo = await ethers.getNamedSigner("feeTo");
this.MasterDeployer = await ethers.getContractFactory("MasterDeployer");
this.ConstantProductPoolFactory = await ethers.getContractFactory("ConstantProductPoolFactory");
this.BentoBox = await ethers.getContractFactory("BentoBoxV1");
this.ERC20 = await ethers.getContractFactory("ERC20Mock");
this.sushi = await this.ERC20.deploy("SushiToken", "SUSHI", ethers.constants.MaxUint256);
await this.sushi.deployed();
this.WETH9 = await ethers.getContractFactory("WETH9");
this.weth = await this.WETH9.deploy();
await this.weth.deployed();
this.bentoBox = await this.BentoBox.deploy(this.weth.address);
await this.bentoBox.deployed();
});
it("Reverts on invalid fee", async function () {
await expect(
this.MasterDeployer.deploy(MAX_FEE.add(1), this.feeTo.address, this.bentoBox.address)
).to.be.revertedWith("InvalidBarFee");
});
it("Reverts on fee to zero address", async function () {
await expect(
this.MasterDeployer.deploy(MAX_FEE, ethers.constants.AddressZero, this.bentoBox.address)
).to.be.revertedWith("ZeroAddress");
});
it("Reverts on bento zero address", async function () {
await expect(
this.MasterDeployer.deploy(MAX_FEE, this.feeTo.address, ethers.constants.AddressZero)
).to.be.revertedWith("ZeroAddress");
});
beforeEach(async function () {
this.masterDeployer = await this.MasterDeployer.deploy(MAX_FEE, this.feeTo.address, this.bentoBox.address);
await this.masterDeployer.deployed();
this.constantProductPoolFactory = await this.ConstantProductPoolFactory.deploy(this.masterDeployer.address);
});
describe("#deployPool", async function () {
it("Reverts on non-whitelisted factory", async function () {
const deployData = defaultAbiCoder.encode(
["address", "address", "uint256", "bool"],
[...[this.weth.address, this.sushi.address].sort(), 30, true]
);
await expect(
this.masterDeployer.deployPool(this.constantProductPoolFactory.address, deployData)
).to.be.revertedWith("NotWhitelisted");
});
it("Adds address to pools array", async function () {
await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);
const deployData = defaultAbiCoder.encode(
["address", "address", "uint256", "bool"],
[...[this.weth.address, this.sushi.address].sort(), 30, true]
);
await this.masterDeployer.deployPool(this.constantProductPoolFactory.address, deployData);
});
it("Reverts on direct deployment via factory", async function () {
const deployData = defaultAbiCoder.encode(
["address", "address", "uint256", "bool"],
[...[this.weth.address, this.sushi.address].sort(), 30, true]
);
await expect(this.constantProductPoolFactory.deployPool(deployData)).to.be.revertedWith(
customError("UnauthorisedDeployer")
);
});
// TODO: Fix this
it("Emits event on successful deployment", async function () {
await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);
const deployData = defaultAbiCoder.encode(
["address", "address", "uint256", "bool"],
[...[this.weth.address, this.sushi.address].sort(), 30, true]
);
const INIT_CODE_HASH = keccak256(["bytes"], [constantProductPoolBytecode]);
const computedConstantProductPoolAddress = getCreate2Address(
this.constantProductPoolFactory.address,
keccak256(["bytes"], [deployData]),
INIT_CODE_HASH
);
await expect(this.masterDeployer.deployPool(this.constantProductPoolFactory.address, deployData))
.to.emit(this.masterDeployer, "DeployPool")
.withArgs(this.constantProductPoolFactory.address, computedConstantProductPoolAddress, deployData);
});
});
describe("#addToWhiteList", async function () {
it("Adds factory to whitelist", async function () {
await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);
expect(await this.masterDeployer.whitelistedFactories(this.constantProductPoolFactory.address)).to.be.true;
});
});
describe("#removeFromWhitelist", async function () {
it("Removes factory from whitelist", async function () {
await this.masterDeployer.addToWhitelist(this.constantProductPoolFactory.address);
await this.masterDeployer.removeFromWhitelist(this.constantProductPoolFactory.address);
expect(await this.masterDeployer.whitelistedFactories(this.constantProductPoolFactory.address)).to.be.false;
});
});
describe("#setBarFee", async function () {
it("Reverts on invalid fee", async function () {
await expect(this.masterDeployer.setBarFee(MAX_FEE.add(1))).to.be.revertedWith("InvalidBarFee");
});
it("Mutates on valid fee", async function () {
this.masterDeployer.setBarFee(0);
expect(await this.masterDeployer.barFee()).to.equal(0);
});
});
});