Initial commit
This commit is contained in:
85
packages/tokens/README.md
Normal file
85
packages/tokens/README.md
Normal file
@@ -0,0 +1,85 @@
|
||||
# @dbis-thirdweb/tokens
|
||||
|
||||
ERC20/721/1155 token deployment and management for Chain 138.
|
||||
|
||||
## Usage
|
||||
|
||||
### Token Factory
|
||||
|
||||
```typescript
|
||||
import { createTokenFactory } from '@dbis-thirdweb/tokens';
|
||||
import { ThirdwebSDK } from '@thirdweb-dev/sdk';
|
||||
import { chain138 } from '@dbis-thirdweb/chain';
|
||||
|
||||
const sdk = new ThirdwebSDK(chain138, privateKey);
|
||||
const factory = createTokenFactory(sdk);
|
||||
|
||||
// Deploy ERC20
|
||||
const erc20Address = await factory.deployERC20({
|
||||
name: 'My Token',
|
||||
symbol: 'MTK',
|
||||
initialSupply: '1000000',
|
||||
});
|
||||
|
||||
// Deploy ERC721
|
||||
const erc721Address = await factory.deployERC721({
|
||||
name: 'My NFT',
|
||||
symbol: 'MNFT',
|
||||
});
|
||||
|
||||
// Deploy ERC1155
|
||||
const erc1155Address = await factory.deployERC1155({
|
||||
name: 'My Edition',
|
||||
});
|
||||
```
|
||||
|
||||
### ERC20 Operations
|
||||
|
||||
```typescript
|
||||
import { mintERC20, transferERC20, getERC20Balance } from '@dbis-thirdweb/tokens';
|
||||
|
||||
await mintERC20(sdk, erc20Address, '1000', recipientAddress);
|
||||
await transferERC20(sdk, erc20Address, recipientAddress, '100');
|
||||
const balance = await getERC20Balance(sdk, erc20Address, address);
|
||||
```
|
||||
|
||||
### ERC721 Operations
|
||||
|
||||
```typescript
|
||||
import { mintERC721, transferERC721, getERC721Metadata } from '@dbis-thirdweb/tokens';
|
||||
|
||||
const tokenId = await mintERC721(sdk, erc721Address, {
|
||||
name: 'My NFT #1',
|
||||
description: 'Description',
|
||||
image: 'ipfs://...',
|
||||
});
|
||||
|
||||
await transferERC721(sdk, erc721Address, tokenId, recipientAddress);
|
||||
const metadata = await getERC721Metadata(sdk, erc721Address, tokenId);
|
||||
```
|
||||
|
||||
### ERC1155 Operations
|
||||
|
||||
```typescript
|
||||
import { mintERC1155, batchMintERC1155, getERC1155Balance } from '@dbis-thirdweb/tokens';
|
||||
|
||||
await mintERC1155(sdk, erc1155Address, 0n, '100', {
|
||||
name: 'Edition #1',
|
||||
image: 'ipfs://...',
|
||||
});
|
||||
|
||||
await batchMintERC1155(sdk, erc1155Address, [
|
||||
{ tokenId: 1n, amount: '50', metadata: {...} },
|
||||
{ tokenId: 2n, amount: '25', metadata: {...} },
|
||||
]);
|
||||
|
||||
const balance = await getERC1155Balance(sdk, erc1155Address, address, 0n);
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- ERC20 deploy/mint/transfer/balance
|
||||
- ERC721 deploy/mint/transfer/metadata
|
||||
- ERC1155 deploy/batch mint/transfer
|
||||
- Metadata hosting strategy (IPFS/thirdweb storage)
|
||||
- Token factory utilities
|
||||
40
packages/tokens/package.json
Normal file
40
packages/tokens/package.json
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "@dbis-thirdweb/tokens",
|
||||
"version": "0.1.0",
|
||||
"description": "ERC20/721/1155 token deployment and management for Chain 138",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsup src/index.ts --format cjs,esm --dts",
|
||||
"lint": "eslint src",
|
||||
"test": "echo \"No tests yet\""
|
||||
},
|
||||
"keywords": [
|
||||
"thirdweb",
|
||||
"tokens",
|
||||
"ERC20",
|
||||
"ERC721",
|
||||
"ERC1155",
|
||||
"chain-138"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@dbis-thirdweb/chain": "workspace:*",
|
||||
"@thirdweb-dev/sdk": "^4.0.0",
|
||||
"ethers": "^5.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"tsup": "^8.0.0",
|
||||
"typescript": "^5.0.0"
|
||||
}
|
||||
}
|
||||
116
packages/tokens/src/erc1155.ts
Normal file
116
packages/tokens/src/erc1155.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import type { ThirdwebSDK } from '@thirdweb-dev/sdk';
|
||||
import type { TokenMetadata } from './metadata';
|
||||
import type { BigNumberish } from 'ethers';
|
||||
|
||||
/**
|
||||
* ERC1155 token deployment parameters
|
||||
*/
|
||||
export interface ERC1155DeployParams {
|
||||
name: string;
|
||||
description?: string;
|
||||
image?: string;
|
||||
royaltyRecipient?: string;
|
||||
royaltyBps?: number; // Basis points
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy ERC1155 token contract
|
||||
*/
|
||||
export async function deployERC1155(
|
||||
sdk: ThirdwebSDK,
|
||||
params: ERC1155DeployParams
|
||||
): Promise<string> {
|
||||
const contractAddress = await sdk.deployer.deployEdition({
|
||||
name: params.name,
|
||||
description: params.description,
|
||||
image: params.image,
|
||||
primary_sale_recipient: await sdk.getSigner()?.getAddress(),
|
||||
});
|
||||
|
||||
return contractAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mint ERC1155 tokens (batch mint)
|
||||
*/
|
||||
export async function mintERC1155(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
tokenId: bigint,
|
||||
amount: BigNumberish,
|
||||
metadata: TokenMetadata,
|
||||
to?: string
|
||||
): Promise<void> {
|
||||
const contract = await sdk.getContract(contractAddress, 'edition');
|
||||
const recipient = to || (await sdk.getSigner()?.getAddress()) || '';
|
||||
|
||||
await contract.erc1155.mintTo(recipient, {
|
||||
metadata,
|
||||
supply: amount.toString(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch mint ERC1155 tokens
|
||||
*/
|
||||
export async function batchMintERC1155(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
mints: Array<{
|
||||
tokenId: bigint;
|
||||
amount: BigNumberish;
|
||||
metadata: TokenMetadata;
|
||||
}>,
|
||||
to?: string
|
||||
): Promise<void> {
|
||||
const contract = await sdk.getContract(contractAddress, 'edition');
|
||||
const recipient = to || (await sdk.getSigner()?.getAddress()) || '';
|
||||
|
||||
const payloads = mints.map((mint) => ({
|
||||
metadata: mint.metadata,
|
||||
supply: mint.amount.toString(),
|
||||
}));
|
||||
|
||||
await contract.erc1155.mintBatchTo(recipient, payloads);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer ERC1155 tokens
|
||||
*/
|
||||
export async function transferERC1155(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
tokenId: bigint,
|
||||
amount: BigNumberish,
|
||||
to: string
|
||||
): Promise<void> {
|
||||
const contract = await sdk.getContract(contractAddress, 'edition');
|
||||
await contract.erc1155.transfer(to, tokenId.toString(), amount.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ERC1155 token balance
|
||||
*/
|
||||
export async function getERC1155Balance(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
address: string,
|
||||
tokenId: bigint
|
||||
): Promise<bigint> {
|
||||
const contract = await sdk.getContract(contractAddress, 'edition');
|
||||
const balance = await contract.erc1155.balanceOf(address, tokenId);
|
||||
return BigInt(balance.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ERC1155 token metadata
|
||||
*/
|
||||
export async function getERC1155Metadata(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
tokenId: bigint
|
||||
): Promise<TokenMetadata> {
|
||||
const contract = await sdk.getContract(contractAddress, 'edition');
|
||||
const nft = await contract.erc1155.get(tokenId.toString());
|
||||
return nft.metadata as TokenMetadata;
|
||||
}
|
||||
81
packages/tokens/src/erc20.ts
Normal file
81
packages/tokens/src/erc20.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import type { Signer } from 'ethers';
|
||||
import type { ThirdwebSDK } from '@thirdweb-dev/sdk';
|
||||
import type { BigNumberish } from 'ethers';
|
||||
|
||||
/**
|
||||
* ERC20 token deployment parameters
|
||||
*/
|
||||
export interface ERC20DeployParams {
|
||||
name: string;
|
||||
symbol: string;
|
||||
description?: string;
|
||||
image?: string;
|
||||
initialSupply?: BigNumberish;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy ERC20 token
|
||||
*/
|
||||
export async function deployERC20(
|
||||
sdk: ThirdwebSDK,
|
||||
params: ERC20DeployParams
|
||||
): Promise<string> {
|
||||
// Use thirdweb SDK to deploy ERC20
|
||||
// This uses thirdweb's prebuilt ERC20 contract
|
||||
const contractAddress = await sdk.deployer.deployToken({
|
||||
name: params.name,
|
||||
symbol: params.symbol,
|
||||
description: params.description,
|
||||
image: params.image,
|
||||
primary_sale_recipient: await sdk.getSigner()?.getAddress(),
|
||||
});
|
||||
|
||||
// If initial supply is specified, mint it
|
||||
if (params.initialSupply) {
|
||||
const contract = await sdk.getContract(contractAddress, 'token');
|
||||
await contract.erc20.mint(params.initialSupply.toString());
|
||||
}
|
||||
|
||||
return contractAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mint ERC20 tokens
|
||||
*/
|
||||
export async function mintERC20(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
amount: BigNumberish,
|
||||
to?: string
|
||||
): Promise<void> {
|
||||
const contract = await sdk.getContract(contractAddress, 'token');
|
||||
const recipient = to || (await sdk.getSigner()?.getAddress()) || '';
|
||||
|
||||
await contract.erc20.mintTo(recipient, amount.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer ERC20 tokens
|
||||
*/
|
||||
export async function transferERC20(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
to: string,
|
||||
amount: BigNumberish
|
||||
): Promise<void> {
|
||||
const contract = await sdk.getContract(contractAddress, 'token');
|
||||
await contract.erc20.transfer(to, amount.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ERC20 balance
|
||||
*/
|
||||
export async function getERC20Balance(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
address: string
|
||||
): Promise<bigint> {
|
||||
const contract = await sdk.getContract(contractAddress, 'token');
|
||||
const balance = await contract.erc20.balanceOf(address);
|
||||
return BigInt(balance.value.toString());
|
||||
}
|
||||
87
packages/tokens/src/erc721.ts
Normal file
87
packages/tokens/src/erc721.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { ThirdwebSDK } from '@thirdweb-dev/sdk';
|
||||
import type { TokenMetadata } from './metadata';
|
||||
|
||||
/**
|
||||
* ERC721 token deployment parameters
|
||||
*/
|
||||
export interface ERC721DeployParams {
|
||||
name: string;
|
||||
symbol: string;
|
||||
description?: string;
|
||||
image?: string;
|
||||
royaltyRecipient?: string;
|
||||
royaltyBps?: number; // Basis points (e.g., 500 = 5%)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy ERC721 token contract
|
||||
*/
|
||||
export async function deployERC721(
|
||||
sdk: ThirdwebSDK,
|
||||
params: ERC721DeployParams
|
||||
): Promise<string> {
|
||||
const contractAddress = await sdk.deployer.deployNFTCollection({
|
||||
name: params.name,
|
||||
symbol: params.symbol,
|
||||
description: params.description,
|
||||
image: params.image,
|
||||
primary_sale_recipient: await sdk.getSigner()?.getAddress(),
|
||||
});
|
||||
|
||||
return contractAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mint ERC721 token
|
||||
*/
|
||||
export async function mintERC721(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
metadata: TokenMetadata,
|
||||
to?: string
|
||||
): Promise<bigint> {
|
||||
const contract = await sdk.getContract(contractAddress, 'nft-collection');
|
||||
const recipient = to || (await sdk.getSigner()?.getAddress()) || '';
|
||||
|
||||
const result = await contract.mintTo(recipient, metadata);
|
||||
return BigInt(result.id.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer ERC721 token
|
||||
*/
|
||||
export async function transferERC721(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
tokenId: bigint,
|
||||
to: string
|
||||
): Promise<void> {
|
||||
const contract = await sdk.getContract(contractAddress, 'nft-collection');
|
||||
await contract.transfer(tokenId.toString(), to);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ERC721 token metadata
|
||||
*/
|
||||
export async function getERC721Metadata(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
tokenId: bigint
|
||||
): Promise<TokenMetadata> {
|
||||
const contract = await sdk.getContract(contractAddress, 'nft-collection');
|
||||
const nft = await contract.erc721.get(tokenId);
|
||||
return nft.metadata as TokenMetadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ERC721 balance (number of tokens owned)
|
||||
*/
|
||||
export async function getERC721Balance(
|
||||
sdk: ThirdwebSDK,
|
||||
contractAddress: string,
|
||||
address: string
|
||||
): Promise<bigint> {
|
||||
const contract = await sdk.getContract(contractAddress, 'nft-collection');
|
||||
const balance = await contract.erc721.balanceOf(address);
|
||||
return BigInt(balance.toString());
|
||||
}
|
||||
39
packages/tokens/src/factory.ts
Normal file
39
packages/tokens/src/factory.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { ThirdwebSDK } from '@thirdweb-dev/sdk';
|
||||
import { deployERC20, type ERC20DeployParams } from './erc20';
|
||||
import { deployERC721, type ERC721DeployParams } from './erc721';
|
||||
import { deployERC1155, type ERC1155DeployParams } from './erc1155';
|
||||
|
||||
/**
|
||||
* Token factory for deploying different token types
|
||||
*/
|
||||
export class TokenFactory {
|
||||
constructor(private sdk: ThirdwebSDK) {}
|
||||
|
||||
/**
|
||||
* Deploy ERC20 token
|
||||
*/
|
||||
async deployERC20(params: ERC20DeployParams): Promise<string> {
|
||||
return deployERC20(this.sdk, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy ERC721 token
|
||||
*/
|
||||
async deployERC721(params: ERC721DeployParams): Promise<string> {
|
||||
return deployERC721(this.sdk, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy ERC1155 token
|
||||
*/
|
||||
async deployERC1155(params: ERC1155DeployParams): Promise<string> {
|
||||
return deployERC1155(this.sdk, params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create token factory instance
|
||||
*/
|
||||
export function createTokenFactory(sdk: ThirdwebSDK): TokenFactory {
|
||||
return new TokenFactory(sdk);
|
||||
}
|
||||
5
packages/tokens/src/index.ts
Normal file
5
packages/tokens/src/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from './metadata';
|
||||
export * from './erc20';
|
||||
export * from './erc721';
|
||||
export * from './erc1155';
|
||||
export * from './factory';
|
||||
74
packages/tokens/src/metadata.ts
Normal file
74
packages/tokens/src/metadata.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Metadata hosting strategy configuration
|
||||
*/
|
||||
export interface MetadataConfig {
|
||||
/**
|
||||
* IPFS gateway URL for uploading/fetching metadata
|
||||
*/
|
||||
ipfsGateway: string;
|
||||
|
||||
/**
|
||||
* Whether to pin metadata to IPFS
|
||||
*/
|
||||
pinToIpfs: boolean;
|
||||
|
||||
/**
|
||||
* Alternative metadata storage options
|
||||
*/
|
||||
storage?: {
|
||||
/**
|
||||
* Use thirdweb storage
|
||||
*/
|
||||
useThirdwebStorage?: boolean;
|
||||
|
||||
/**
|
||||
* Custom storage endpoint
|
||||
*/
|
||||
customEndpoint?: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Default metadata configuration
|
||||
*/
|
||||
export const defaultMetadataConfig: MetadataConfig = {
|
||||
ipfsGateway: 'https://ipfs.io/ipfs/',
|
||||
pinToIpfs: true,
|
||||
storage: {
|
||||
useThirdwebStorage: true,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Token metadata structure
|
||||
*/
|
||||
export interface TokenMetadata {
|
||||
name: string;
|
||||
description?: string;
|
||||
image?: string;
|
||||
external_url?: string;
|
||||
attributes?: Array<{
|
||||
trait_type: string;
|
||||
value: string | number;
|
||||
}>;
|
||||
// Additional properties can be added as needed
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate token metadata URI
|
||||
*/
|
||||
export function generateMetadataURI(
|
||||
metadata: TokenMetadata,
|
||||
config: MetadataConfig = defaultMetadataConfig
|
||||
): string {
|
||||
// In production, this would upload to IPFS/thirdweb storage
|
||||
// For now, return a placeholder
|
||||
if (config.storage?.useThirdwebStorage) {
|
||||
// Would use thirdweb storage SDK here
|
||||
return 'ipfs://...';
|
||||
}
|
||||
|
||||
// Fallback to direct IPFS gateway
|
||||
return 'ipfs://...';
|
||||
}
|
||||
12
packages/tokens/tsconfig.json
Normal file
12
packages/tokens/tsconfig.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"composite": false
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"references": [
|
||||
{ "path": "../chain" }
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user