108 lines
11 KiB
TypeScript
108 lines
11 KiB
TypeScript
/**
|
|
* Deploy TestUSDT to BSC Testnet
|
|
*
|
|
* Usage:
|
|
* npx ts-node scripts/deploy-test-usdt.ts <PRIVATE_KEY>
|
|
*
|
|
* Example:
|
|
* npx ts-node scripts/deploy-test-usdt.ts 0xabc123...
|
|
*
|
|
* Get BSC Testnet tBNB from: https://www.bnbchain.org/en/testnet-faucet
|
|
*/
|
|
|
|
import { ethers, ContractFactory } from 'ethers';
|
|
|
|
// BSC Testnet 配置
|
|
const BSC_TESTNET_RPC = 'https://data-seed-prebsc-1-s1.binance.org:8545';
|
|
const BSC_TESTNET_CHAIN_ID = 97;
|
|
|
|
// TestUSDT 合约 ABI 和 Bytecode
|
|
const CONTRACT_ABI = [
|
|
'constructor()',
|
|
'function name() view returns (string)',
|
|
'function symbol() view returns (string)',
|
|
'function decimals() view returns (uint8)',
|
|
'function totalSupply() view returns (uint256)',
|
|
'function balanceOf(address) view returns (uint256)',
|
|
'function transfer(address to, uint256 amount) returns (bool)',
|
|
'function approve(address spender, uint256 amount) returns (bool)',
|
|
'function allowance(address owner, address spender) view returns (uint256)',
|
|
'function transferFrom(address from, address to, uint256 amount) returns (bool)',
|
|
'function mint(uint256 amount)',
|
|
'function mintUsdt(uint256 usdtAmount)',
|
|
'function mintTo(address to, uint256 amount)',
|
|
'function faucet()',
|
|
'function owner() view returns (address)',
|
|
'event Transfer(address indexed from, address indexed to, uint256 value)',
|
|
'event Approval(address indexed owner, address indexed spender, uint256 value)',
|
|
];
|
|
|
|
// 编译后的 bytecode (TestUSDT_Flat.sol)
|
|
const CONTRACT_BYTECODE =
|
|
'0x608060405234801561001057600080fd5b506040518060400160405280600981526020017f5465737420555344540000000000000000000000000000000000000000000000815250604051806040016040528060048152602001635553445460e01b81525081600390816100739190610293565b5060046100808282610293565b505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506100e633660f4240600660ff16600a6100dc919061049c565b6100e691906104e7565b6100eb565b610529565b80600260008282546100fd9190610529565b90915550506001600160a01b0382166000818152600560209081526040808320805486019055518481527fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a35050565b634e487b7160e01b600052604160045260246000fd5b600181811c9082168061018057607f821691505b6020821081036101a057634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156101f057600081815260208120601f850160051c810160208610156101cd5750805b601f850160051c820191505b818110156101ec578281556001016101d9565b5050505b505050565b81516001600160401b0381111561020e5761020e610156565b6102228161021c845461016c565b846101a6565b602080601f831160018114610257576000841561023f5750858301515b600019600386901b1c1916600185901b1785556101ec565b600085815260208120601f198616915b8281101561028657888601518255948401946001909101908401610267565b50858210156102a45787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b634e487b7160e01b600052601160045260246000fd5b600181815b808511156103055781600019048211156102eb576102eb6102b4565b808516156102f857918102915b93841c93908002906102cf565b509250929050565b60008261031c57506001610496565b8161032957506000610496565b816001811461033f576002811461034957610365565b6001915050610496565b60ff84111561035a5761035a6102b4565b50506001821b610496565b5060208310610133831016604e8410600b8410161715610388575081810a610496565b61039283836102ca565b80600019048211156103a6576103a66102b4565b029392505050565b60006103bd60ff84168361030d565b9392505050565b600082198211156103d7576103d76102b4565b500190565b6000826103f957634e487b7160e01b600052601260045260246000fd5b500490565b600060208083528351808285015260005b8181101561042b5785810183015185820160400152820161040f565b8181111561043d576000604083870101525b50601f01601f1916929092016040019392505050565b60006020828403121561046557600080fd5b81356001600160a01b038116811461047c57600080fd5b9392505050565b60006020828403121561049557600080fd5b5035919050565b60006103bd83836103ae565b80820281158282048414176104bf576104bf6102b4565b92915050565b6000602082840312156104d757600080fd5b5051919050565b60006104bd83836103ae565b80820281158282048414176104bf576104bf6102b4565b634e487b7160e01b600052601260045260246000fd5b60008261052857610528610501565b500690565b808201808211156104bf576104bf6102b4565b610984806105476000396000f3fe608060405234801561001057600080fd5b50600436106101005760003560e01c806370a0823111610097578063a9059cbb11610066578063a9059cbb14610215578063de0e9a3e14610228578063dd62ed3e1461023b578063e1f21c671461027457600080fd5b806370a08231146101bc5780638da5cb5b146101e557806395d89b4114610200578063a0712d681461020857600080fd5b806323b872dd116100d357806323b872dd14610166578063313ce5671461017957806340c10f19146101885780636a627842146101a957600080fd5b806306fdde0314610105578063095ea7b31461012357806318160ddd146101465780631e83409a14610158575b600080fd5b61010d610287565b60405161011a91906107e4565b60405180910390f35b610136610131366004610853565b610319565b604051901515815260200161011a565b6002545b60405190815260200161011a565b61014a61271081565b61013661017436600461087d565b610333565b60405160068152602001610161565b61019b610196366004610853565b610357565b005b61019b6101b73660046108b9565b6103a1565b61014a6101ca3660046108b9565b6001600160a01b031660009081526005602052604090205490565b6000546040516001600160a01b03909116815260200161011a565b61010d6103c6565b61019b6102163660046108d4565b6103d5565b610136610223366004610853565b6103fb565b61019b6102363660046108d4565b610409565b61014a6102493660046108ed565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b61019b61028236600461087d565b61042c565b60606003805461029690610920565b80601f01602080910402602001604051908101604052809291908181526020018280546102c290610920565b801561030f5780601f106102e45761010080835404028352916020019161030f565b820191906000526020600020905b8154815290600101906020018083116102f257829003601f168201915b5050505050905090565b6000336103278185856104a0565b60019150505b92915050565b6000336103418582856105c4565b61034c85858561063e565b506001949350505050565b6000546001600160a01b0316331461038a5760405162461bcd60e51b81526004016103819061095a565b60405180910390fd5b61039d82826006600a610a6e57610790565b5050565b6103c3336127106006600a6103ba91906106e2565b6103039190610a6e565b50565b60606004805461029690610920565b6103c3336006600a6103e791906106e2565b6103f19084610a6e565b610790565b60003361032781858561063e565b6103c333826006600a61041c91906106e2565b6104269190610a6e565b610790565b6000546001600160a01b031633146104565760405162461bcd60e51b81526004016103819061095a565b61049b83838080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525050506001600160a01b038616915084905061063e565b505050565b6001600160a01b0383166105025760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b6064820152608401610381565b6001600160a01b0382166105635760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b6064820152608401610381565b6001600160a01b0383811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925910160405180910390a3505050565b6001600160a01b038381166000908152600160209081526040808320938616835292905220546000198114610638578181101561062b5760405162461bcd60e51b815260206004820152600560248201526422a9292a1960d91b6044820152606401610381565b61063884848484036104a0565b50505050565b6001600160a01b03831661067f5760405162461bcd60e51b81526020600482015260086024820152672166726f6d203d3d60c01b6044820152606401610381565b6001600160a01b0382166106be5760405162461bcd60e51b8152602060048201526006602482015265021746f203d3d60d41b6044820152606401610381565b6001600160a01b0383166000908152600560205260409020548181101561070c5760405162461bcd60e51b8152602060048201526002602482015261212160f01b6044820152606401610381565b6001600160a01b0380851660008181526005602052604080822086860390559286168082529083902080548601905591517fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef906107689086815260200190565b60405180910390a350505050565b600061078182610a85565b61078d90610a9a565b92915050565b80600260008282546107a59190610ab0565b90915550506001600160a01b0382166000818152600560209081526040808320805486019055518481527fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef91015b60405180910390a35050565b600060208083528351808285015260005b81811015610811578581018301518582016040015282016107f5565b81811115610823576000604083870101525b50601f01601f1916929092016040019392505050565b80356001600160a01b038116811461085057600080fd5b919050565b6000806040838503121561086857600080fd5b61087183610839565b946020939093013593505050565b60008060006060848603121561089457600080fd5b61089d84610839565b92506108ab60208501610839565b9150604084013590509250925092565b6000602082840312156108cd57600080fd5b5035919050565b6000602082840312156108e857600080fd5b6108f182610839565b9392505050565b6000806040838503121561090b57600080fd5b61091483610839565b915061092260208401610839565b90509250929050565b600181811c9082168061093f57607f821691505b60208210810361095f57634e487b7160e01b600052602260045260246000fd5b50919050565b6020808252600990820152682737ba1037bbb732b960b91b604082015260600190565b634e487b7160e01b600052601160045260246000fd5b600181815b808511156109d95781600019048211156109bf576109bf610988565b808516156109cc57918102915b93841c93908002906109a3565b509250929050565b6000826109f05750600161032d565b816109fd5750600061032d565b8160018114610a135760028114610a1d57610a39565b600191505061032d565b60ff841115610a2e57610a2e610988565b50506001821b61032d565b5060208310610133831016604e8410600b8410161715610a5c575081810a61032d565b610a66838361099e565b8060001904821115610a7a57610a7a610988565b029392505050565b60006108f160ff8416836109e1565b8082028115828204841417610aa857610aa8610988565b92915050565b8082018082111561032d5761032d61098856fea264697066735822122012';
|
|
|
|
async function main() {
|
|
const privateKey = process.argv[2];
|
|
|
|
if (!privateKey) {
|
|
console.error(`
|
|
❌ Usage: npx ts-node scripts/deploy-test-usdt.ts <PRIVATE_KEY>
|
|
|
|
Steps:
|
|
1. Get tBNB from https://www.bnbchain.org/en/testnet-faucet
|
|
2. Export your private key from MetaMask
|
|
3. Run: npx ts-node scripts/deploy-test-usdt.ts 0xYourPrivateKey
|
|
`);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('🚀 Deploying TestUSDT to BSC Testnet...\n');
|
|
|
|
// 连接到 BSC Testnet
|
|
const provider = new ethers.JsonRpcProvider(BSC_TESTNET_RPC, {
|
|
chainId: BSC_TESTNET_CHAIN_ID,
|
|
name: 'bsc-testnet',
|
|
});
|
|
|
|
// 创建钱包
|
|
const wallet = new ethers.Wallet(privateKey, provider);
|
|
console.log(`📍 Deployer address: ${wallet.address}`);
|
|
|
|
// 检查余额
|
|
const balance = await provider.getBalance(wallet.address);
|
|
console.log(`💰 Balance: ${ethers.formatEther(balance)} tBNB`);
|
|
|
|
if (balance < ethers.parseEther('0.01')) {
|
|
console.error('\n❌ Insufficient tBNB. Get some from https://www.bnbchain.org/en/testnet-faucet');
|
|
process.exit(1);
|
|
}
|
|
|
|
// 部署合约
|
|
console.log('\n📦 Deploying contract...');
|
|
const factory = new ContractFactory(CONTRACT_ABI, CONTRACT_BYTECODE, wallet);
|
|
const contract = await factory.deploy();
|
|
|
|
console.log(`⏳ Waiting for confirmation...`);
|
|
console.log(` Transaction: https://testnet.bscscan.com/tx/${contract.deploymentTransaction()?.hash}`);
|
|
|
|
await contract.waitForDeployment();
|
|
const contractAddress = await contract.getAddress();
|
|
|
|
console.log(`
|
|
✅ TestUSDT deployed successfully!
|
|
|
|
📋 Contract Address: ${contractAddress}
|
|
🔗 BSCScan: https://testnet.bscscan.com/address/${contractAddress}
|
|
|
|
Next steps:
|
|
1. Update .env: BSC_USDT_CONTRACT=${contractAddress}
|
|
2. Call faucet() to mint 10,000 USDT for testing
|
|
3. Or call mintUsdt(100000) to mint 100,000 USDT
|
|
`);
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error('❌ Deployment failed:', error.message);
|
|
process.exit(1);
|
|
});
|