Skip to content

Commit

Permalink
Merge pull request #225 from blackbeard002/createSwapERC1155
Browse files Browse the repository at this point in the history
refactor: ERC1155 included in createSwap() scripts
  • Loading branch information
0xneves authored May 19, 2024
2 parents 7c7ca2c + b4b46cc commit b20f395
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SWAPLACE_ADDRESS=0x000000000000000000000000000000000000000000
# Mocks last deployed contracts addresses.
ERC20_ADDRESS=0x000000000000000000000000000000000000000000
ERC721_ADDRESS=0x000000000000000000000000000000000000000000
ERC1155_ADDRESS=0x000000000000000000000000000000000000000000
# Amount of ERC20 tokens to be minted for signer address.
AMOUNT=1000
# Token ID of ERC721 to be minted for signer address.
Expand Down
32 changes: 24 additions & 8 deletions scripts/createSwap.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { ethers } from "hardhat";
import { Contract } from "ethers";
import { blocktimestamp, storeEnv } from "../test/utils/utils";
import { Swap, composeSwap } from "../test/utils/SwapFactory";
import {
Swap,
composeSwap,
encodeAsset,
encodeConfig,
} from "../test/utils/SwapFactory";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

export async function main() {
/// @dev This is the list of mock deployments addresses that were stored in the `.env` file.
const ERC20_ADDRESS = process.env.ERC20_ADDRESS || 0x0;
const ERC721_ADDRESS = process.env.ERC721_ADDRESS || 0x0;
const ERC1155_ADDRESS = process.env.ERC1155_ADDRESS || 0x0;
/// @dev The Swaplace address also needs to be instance to receive the approvals.
const SWAPLACE_ADDRESS = process.env.SWAPLACE_ADDRESS || 0x0;
/// @dev Will throw an error if any of the addresses were not set in the `.env` file.
if (!ERC20_ADDRESS || !ERC721_ADDRESS || !SWAPLACE_ADDRESS) {
if (
!ERC20_ADDRESS ||
!ERC721_ADDRESS ||
!SWAPLACE_ADDRESS ||
!ERC1155_ADDRESS
) {
throw new Error(
"Invalid ERC20, ERC721 or Swaplace address, please check if the addresse in the `.env` file is set up correctly.",
"Invalid ERC20, ERC721, ERC1155 or Swaplace address, please check if the addresses in the `.env` file are set up correctly.",
);
}

Expand Down Expand Up @@ -65,21 +76,26 @@ export async function main() {
);
}

/// @dev Fill the Swap struct
/// @dev Fill the Swap struct and config
const owner = signers[0].address;
const allowed = ethers.constants.AddressZero;
const expiry = (await blocktimestamp()) * 2;
const recipient = 0;
const value = 0;

/// @dev Encode ERC1155 asset
const amountAndId = await encodeAsset(BigInt(tokenId), BigInt(amount));

/// @dev Build the biding assets
const bidingAddr = [ERC20_ADDRESS];
const bidingAmountOrId = [amount];
const bidingAddr = [ERC20_ADDRESS, ERC1155_ADDRESS];
const bidingAmountOrId = [BigInt(amount), amountAndId];

/// @dev Build the asking assets
const askingAddr = [ERC721_ADDRESS];
const askingAmountOrId = [tokenId];
const askingAmountOrId = [BigInt(tokenId)];

/// @dev Pack the config together
const config = (BigInt(allowed) << BigInt(96)) | BigInt(expiry);
const config = await encodeConfig(allowed, expiry, recipient, value);

/// @dev Compose the above swap into the Swap Struct
const swap: Swap = await composeSwap(
Expand Down
29 changes: 29 additions & 0 deletions test/utils/SwapFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ export async function makeAsset(
return asset;
}

/**
* @dev See {ISwapFactory-encodeAsset}.
*/
export async function encodeAsset(
tokenId: bigint | number,
tokenAmount: bigint | number,
): Promise<bigint> {
// if the amount or ID is negative, it will throw an error
if (tokenId < 0 || tokenAmount < 0) {
throw new Error("tokenId or tokenAmount cannot be less than 0");
}

const uint16Max = 65535;
const uint120Max = BigInt(2) ** BigInt(120) - BigInt(1);

if (tokenId > uint120Max || tokenAmount > uint120Max) {
throw new Error(
"Maxium bits exceeded for tokenId or tokenAmount. Max: 120 bits.",
);
}

return BigInt(
(BigInt(uint16Max) << BigInt(240)) |
(BigInt(tokenId) << BigInt(120)) |
BigInt(tokenAmount),
);
}

/**
* @dev See {ISwapFactory-makeSwap}.
*/
Expand Down Expand Up @@ -182,4 +210,5 @@ module.exports = {
composeSwap,
encodeConfig,
decodeConfig,
encodeAsset,
};

0 comments on commit b20f395

Please sign in to comment.