Skip to content

Commit

Permalink
[Spike] Refactor evm examples (#30)
Browse files Browse the repository at this point in the history
* Add `@therootnetwork/evm` package

* Refactor utils

* Update `evm` examples

* Prettier format

* Singularize `precompile`
  • Loading branch information
aidan-starke authored Aug 1, 2023
1 parent 95f709b commit 8c36205
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 163 deletions.
7 changes: 2 additions & 5 deletions examples/evm/use-erc1155/src/safeBatchTransferFrom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
createToken,
getERC1155Precompile,
getSignerWallet,
} from "@trne/utils/getERC1155PrecompileContract";
import { createToken, getERC1155Precompile } from "@trne/utils/getERC1155PrecompileContract";
import { getSignerWallet } from "@trne/utils/getSignerWallet";
import assert from "assert";
import { cleanEnv, str } from "envalid";
import { ethers } from "ethers";
Expand Down
7 changes: 2 additions & 5 deletions examples/evm/use-erc1155/src/safeTransferFrom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
createToken,
getERC1155Precompile,
getSignerWallet,
} from "@trne/utils/getERC1155PrecompileContract";
import { createToken, getERC1155Precompile } from "@trne/utils/getERC1155PrecompileContract";
import { getSignerWallet } from "@trne/utils/getSignerWallet";
import assert from "assert";
import { cleanEnv, str } from "envalid";
import { ethers } from "ethers";
Expand Down
7 changes: 2 additions & 5 deletions examples/evm/use-erc1155/src/safeTransferFromOwner.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
createToken,
getERC1155Precompile,
getSignerWallet,
} from "@trne/utils/getERC1155PrecompileContract";
import { createToken, getERC1155Precompile } from "@trne/utils/getERC1155PrecompileContract";
import { getSignerWallet } from "@trne/utils/getSignerWallet";
import assert from "assert";
import { cleanEnv, str } from "envalid";
import { ethers } from "ethers";
Expand Down
3 changes: 2 additions & 1 deletion examples/evm/use-erc1155/src/transferOwnership.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getERC1155Precompile, getSignerWallet } from "@trne/utils/getERC1155PrecompileContract";
import { getERC1155Precompile } from "@trne/utils/getERC1155PrecompileContract";
import { getSignerWallet } from "@trne/utils/getSignerWallet";
import assert from "assert";
import { cleanEnv, str } from "envalid";

Expand Down
5 changes: 2 additions & 3 deletions examples/evm/use-erc20/src/transferFrom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { getERC20PrecompileForAssetId } from "@trne/utils/getERC20PrecompileAddress";
import { getEthersProvider } from "@trne/utils/getEthersProvider";
import { getSignerWallet } from "@trne/utils/getSignerWallet";
import assert from "assert";
import { cleanEnv, str } from "envalid";
import { Wallet } from "ethers";

const env = cleanEnv(process.env, {
CALLER_PRIVATE_KEY: str(), // private key of extrinsic caller
Expand All @@ -18,7 +17,7 @@ export async function main() {
env.CALLER_PRIVATE_KEY,
XRP_TOKEN_ID
);
const bobSigner = new Wallet(env.BOB_PRIVATE_KEY, getEthersProvider("porcini"));
const bobSigner = getSignerWallet(env.BOB_PRIVATE_KEY);
const amountApproved = 200;

const tx = await erc20Precompile.connect(wallet).approve(bobSigner.address, amountApproved);
Expand Down
3 changes: 0 additions & 3 deletions examples/evm/use-erc721/src/isApprovedForAll.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//const precompileGasEstimate = await erc721Precompile
// .connect(alithSigner)
// .estimateGas.isApprovedForAll(alithSigner.address, bobSigner.address);
import { getERC721Precompile } from "@trne/utils/getERC721PrecompileContract";
import assert from "assert";
import { cleanEnv, str } from "envalid";
Expand Down
3 changes: 2 additions & 1 deletion examples/evm/use-erc721/src/safeTransferFrom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getERC721Precompile, getSignerWallet } from "@trne/utils/getERC721PrecompileContract";
import { getERC721Precompile } from "@trne/utils/getERC721PrecompileContract";
import { getSignerWallet } from "@trne/utils/getSignerWallet";
import assert from "assert";
import { cleanEnv, str } from "envalid";

Expand Down
3 changes: 2 additions & 1 deletion examples/evm/use-erc721/src/transferFrom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getERC721Precompile, getSignerWallet } from "@trne/utils/getERC721PrecompileContract";
import { getERC721Precompile } from "@trne/utils/getERC721PrecompileContract";
import { getSignerWallet } from "@trne/utils/getSignerWallet";
import assert from "assert";
import { cleanEnv, str } from "envalid";

Expand Down
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@polkadot/util": "^12.3.2",
"@polkadot/util-crypto": "^12.3.2",
"@therootnetwork/api": "^1.0.2",
"@therootnetwork/evm": "^1.0.0",
"realm-web": "^2.0.0"
}
}
65 changes: 9 additions & 56 deletions packages/utils/src/getERC1155PrecompileContract.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,24 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getEthersProvider } from "@trne/utils/getEthersProvider";
import { ERC1155_PRECOMPILE_ABI } from "@therootnetwork/evm";
import { Contract, ethers, Wallet } from "ethers";
import { getAddress } from "ethers/lib/utils";

export const getCollectionPrecompileAddress = (collectionId: number) => {
import { getSignerWallet } from "./getSignerWallet";

const getCollectionPrecompileAddress = (collectionId: number) => {
const collectionIdBin = (+collectionId).toString(2).padStart(22, "0");
const parachainIdBin = (100).toString(2).padStart(10, "0");
const collectionUuid = parseInt(collectionIdBin + parachainIdBin, 2);
const collectionIdHex = (+collectionUuid).toString(16).padStart(8, "0");
return getAddress(`0xBBBBBBBB${collectionIdHex.toUpperCase()}000000000000000000000000`);
};

const OWNABLE_ABI = [
"event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",

"function owner() public view returns (address)",
"function renounceOwnership()",
"function transferOwnership(address owner)",
];

export const ERC1155_PRECOMPILE_ABI = [
// ERC1155
"event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)",
"event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] balances)",
"event ApprovalForAll(address indexed account, address indexed operator, bool approved)",

"function balanceOf(address owner, uint256 id) external view returns (uint256)",
"function balanceOfBatch(address[] owners, uint256[] ids) external view returns (uint256[] memory)",
"function setApprovalForAll(address operator, bool approved) external",
"function isApprovedForAll(address account, address operator) external view returns (bool)",
"function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes calldata data) external",
"function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata amounts, bytes calldata data) external",

// Burnable
"function burn(address account, uint256 id, uint256 value) external",
"function burnBatch(address account, uint256[] ids, uint256[] values) external",

// Supply
"function totalSupply(uint256 id) external view returns (uint256)",
"function exists(uint256 id) external view returns (bool)",

// Metadata
"function uri(uint256 id) external view returns (string memory)",

// TRN
"event TokenCreated(uint32 indexed serialNumber)",
"event MaxSupplyUpdated(uint128 indexed maxSupply)",
"event BaseURIUpdated(string baseURI)",

"function createToken(bytes name, uint128 initialIssuance, uint128 maxIssuance, address tokenOwner) external returns (uint32)",
"function mint(address owner, uint256 id, uint256 amount) external",
"function mintBatch(address owner, uint256[] ids, uint256[] amounts) external",
"function setMaxSupply(uint256 id, uint32 maxSupply) external",
"function setBaseURI(bytes baseURI) external",

// Ownable
...OWNABLE_ABI,
];

export const getERC1155Precompile = (
const getERC1155Precompile = (
privateKey: string,
precompileAddress: string | null,
collectionId: string | number | null
) => {
const wallet = new Wallet(privateKey, getEthersProvider("porcini"));
const wallet = getSignerWallet(privateKey);

const erc1155PrecompileAddress = precompileAddress
? precompileAddress
Expand All @@ -72,14 +27,14 @@ export const getERC1155Precompile = (
console.log("erc1155PrecompileAddress:", erc1155PrecompileAddress);
const erc1155Precompile = new Contract(erc1155PrecompileAddress, ERC1155_PRECOMPILE_ABI, wallet);

// Create precompiles contract
// Create precompile contract
return {
erc1155Precompile,
wallet,
};
};

export async function createToken(
async function createToken(
erc1155Precompile: Contract,
initialIssuance: number,
tokenOwner: Wallet
Expand All @@ -95,6 +50,4 @@ export async function createToken(
return serialNumber;
}

export function getSignerWallet(privateKey: string) {
return new Wallet(privateKey, getEthersProvider("porcini"));
}
export { ERC1155_PRECOMPILE_ABI, getERC1155Precompile, createToken };
34 changes: 9 additions & 25 deletions packages/utils/src/getERC20PrecompileAddress.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
import { getPublicProviderUrl } from "@therootnetwork/api";
import { Contract, getDefaultProvider, Wallet } from "ethers";
import { getAddress } from "ethers/lib/utils";
import { assetIdToERC20Address, ERC20_ABI } from "@therootnetwork/evm";
import { Contract } from "ethers";

export const assetIdToERC20ContractAddress = (assetId: string | number): string => {
const asset_id_hex = (+assetId).toString(16).padStart(8, "0");
return getAddress(`0xCCCCCCCC${asset_id_hex.toUpperCase()}000000000000000000000000`);
};

export const ERC20_ABI = [
"event Transfer(address indexed from, address indexed to, uint256 value)",
"event Approval(address indexed owner, address indexed spender, uint256 value)",
"function approve(address spender, uint256 amount) public returns (bool)",
"function allowance(address owner, address spender) public view returns (uint256)",
"function balanceOf(address who) public view returns (uint256)",
"function name() public view returns (string memory)",
"function symbol() public view returns (string memory)",
"function decimals() public view returns (uint8)",
"function totalSupply() external view returns (uint256)",
"function transfer(address who, uint256 amount)",
"function transferFrom(address from, address to, uint256 amount)",
];
import { getSignerWallet } from "./getSignerWallet";

export const getERC20PrecompileForAssetId = (privateKey: string, assetId: string | number) => {
const wallet = new Wallet(privateKey, getDefaultProvider(getPublicProviderUrl("porcini")));
const getERC20PrecompileForAssetId = (privateKey: string, assetId: string | number) => {
const wallet = getSignerWallet(privateKey);

const erc20PrecompileAddress = assetIdToERC20ContractAddress(assetId);
const erc20PrecompileAddress = assetIdToERC20Address(assetId);

// Create precompiles contract
// Create precompile contract
return {
erc20Precompile: new Contract(erc20PrecompileAddress, ERC20_ABI, wallet),
wallet,
};
};

export { ERC20_ABI, getERC20PrecompileForAssetId };
63 changes: 7 additions & 56 deletions packages/utils/src/getERC721PrecompileContract.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,26 @@
import { Contract, Wallet } from "ethers";
import { getAddress } from "ethers/lib/utils";
import { collectionIdToERC721Address, ERC721_PRECOMPILE_ABI } from "@therootnetwork/evm";
import { Contract } from "ethers";

import { getEthersProvider } from "./getEthersProvider";
import { getSignerWallet } from "./getSignerWallet";

export const collectionIdToERC721Address = (collectionId: string | number): string => {
const collection_id_hex = (+collectionId).toString(16).padStart(8, "0");
return getAddress(`0xAAAAAAAA${collection_id_hex.toUpperCase()}000000000000000000000000`);
};

const OWNABLE_ABI = [
"event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",

"function owner() public view returns (address)",
"function renounceOwnership()",
"function transferOwnership(address owner)",
];

export const ERC721_PRECOMPILE_ABI = [
// ERC721
"event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)",
"event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId)",
"event ApprovalForAll(address indexed owner, address indexed operator, bool approved)",

"function balanceOf(address who) public view returns (uint256)",
"function ownerOf(uint256 tokenId) public view returns (address)",
"function safeTransferFrom(address from, address to, uint256 tokenId)",
"function transferFrom(address from, address to, uint256 tokenId)",
"function approve(address to, uint256 tokenId)",
"function getApproved(uint256 tokenId) public view returns (address)",
"function setApprovalForAll(address operator, bool _approved)",
"function isApprovedForAll(address owner, address operator) public view returns (bool)",

// ERC721 Metadata
"function name() public view returns (string memory)",
"function symbol() public view returns (string memory)",
"function tokenURI(uint256 tokenId) public view returns (string memory)",

// Root specific precompiles
"event MaxSupplyUpdated(uint32 maxSupply)",
"event BaseURIUpdated(string baseURI)",

"function totalSupply() external view returns (uint256)",
"function mint(address owner, uint32 quantity)",
"function setMaxSupply(uint32 maxSupply)",
"function setBaseURI(bytes baseURI)",
"function ownedTokens(address who, uint16 limit, uint32 cursor) public view returns (uint32, uint32, uint32[] memory)",

// Ownable
...OWNABLE_ABI,
];

export const getERC721Precompile = (
const getERC721Precompile = (
privateKey: string,
precompileAddress: string,
collectionId: string | number | null
) => {
const wallet = new Wallet(privateKey, getEthersProvider("porcini"));
const wallet = getSignerWallet(privateKey);

const erc721PrecompileAddress = precompileAddress
? precompileAddress
: collectionIdToERC721Address(collectionId as string);

const erc721Precompile = new Contract(erc721PrecompileAddress, ERC721_PRECOMPILE_ABI, wallet);

// Create precompiles contract
// Create precompile contract
return {
erc721Precompile,
wallet,
};
};

export function getSignerWallet(privateKey: string) {
return new Wallet(privateKey, getEthersProvider("porcini"));
}
export { ERC721_PRECOMPILE_ABI, getERC721Precompile };
2 changes: 1 addition & 1 deletion packages/utils/src/getNFTPrecompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getNFTPrecompile = (privateKey: string) => {
const wallet = new Wallet(privateKey, getEthersProvider("porcini"));
const nftPrecompile = new Contract(NFT_PRECOMPILE_ADDRESS, NFT_PRECOMPILE_ABI, wallet);

// Create precompiles contract
// Create precompile contract
return {
nftPrecompile,
wallet,
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/getSFTPrecompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getSFTPrecompile = (privateKey: string) => {
const wallet = new Wallet(privateKey, getEthersProvider("porcini"));
const sftPrecompile = new Contract(SFT_PRECOMPILE_ADDRESS, SFT_PRECOMPILE_ABI, wallet);

// Create precompiles contract
// Create precompile contract
return {
sftPrecompile,
wallet,
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/src/getSignerWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getPublicProviderUrl } from "@therootnetwork/evm";
import { getDefaultProvider, Wallet } from "ethers";

export function getSignerWallet(privateKey: string) {
return new Wallet(privateKey, getDefaultProvider(getPublicProviderUrl("porcini")));
}
Loading

0 comments on commit 8c36205

Please sign in to comment.