上一篇:【p2p、分布式,区块链笔记 Blockchain】truffle001 以太坊开发框架truffle初步实践 项目实现了一个简单的可租赁的 NFT 系统,用户可以铸造和销毁 NFT。这是作者写的项目介绍(后边看issue才发现的),建议直接看这篇。 mint 函数mint -  百度翻译
英[ mɪnt]   美[ mɪnt] 
n.  薄荷; 造币厂; 薄荷糖; 铸币厂; 大量的钱; 
vt.  铸 ( 币) ; 铸造 ( 硬币) ; 
adj.  完美的; 新造的; 
burn 函数solidity
pragma solidity >= 0.4 .22  < 0.9 .0 ; 
import  "./ERC4907.sol" ;  
                        
import  "@openzeppelin/contracts/utils/Counters.sol" ;  
contract RentablePets is ERC4907  {  
  using Counters for  Counters. Counter;   
  Counters. Counter private  _tokenIds;   
  constructor ( )  ERC4907 ( "RentablePets" ,  "RP" )  { }   
  function  mint ( string memory _tokenURI )  public  {   
    _tokenIds. increment ( ) ;  
    uint256 newTokenId =  _tokenIds. current ( ) ;   
    
    _safeMint ( msg. sender,  newTokenId) ;  
    _setTokenURI ( newTokenId,  _tokenURI) ;  
  } 
  function  burn ( uint256 tokenId )  public  {   
    _burn ( tokenId) ;  
  } 
} 
ERC4907 和 Counters 导入 :引入了自定义的 ERC4907 标准(可租赁的 NFT 标准)和 OpenZeppelin 的计数器工具,用于自动生成唯一的 token ID。合约定义 :合约 RentablePets 继承了 ERC4907 的所有功能(包括 ERC721 标准和租赁扩展)。构造函数 :初始化合约时,设定 NFT 名称为 “RentablePets”,符号为 “RP”。solc:  { 
      version:  ">=0.4.22 <0.9.0" 
      } 
{ 
  "dependencies" :  { 
    "@openzeppelin/contracts" :  "^4.8.0" , 
    "@truffle/hdwallet-provider" :  "^1.7.0" 
  } , 
  "devDependencies" :  { 
    "@openzeppelin/test-helpers" :  "^0.5.15" 
  } 
} 
migrations
英[ / maɪˈgreɪʃənz / ]   美[ / maɪˈgreɪʃənz / ] 
n. ( 程序或硬件的) 迁移,转移;迁移;迁徙;移居; ( 计算机系统的) 改变
migration的复数
const  RentablePets =  artifacts. require ( "RentablePets" ) ;  
module. exports  =  function  ( deployer )  { 
  deployer. deploy ( RentablePets) ; 
} ; 
var  RentablePets =  artifacts. require ( "RentablePets" ) ; 
const  main  =  async  ( cb )  =>  { 
    try  { 
        const  argv =  require ( 'yargs/yargs' ) ( process. argv. slice ( 4 ) ) 
            . default ( "from" ,  ( await  web3. eth. getAccounts ( ) ) [ 0 ] ) 
            . argv;  
        const  rentablePets =  await  RentablePets. deployed ( ) 
        let  txn =  await  rentablePets. mint ( "fakeURI" ,  { from :  argv. from} )  
                                                                        
                                                                        
        console. log ( txn) ; 
    }  catch ( err)  { 
        console. log ( err) ; 
    } 
    cb ( ) ;  
  } 
  
module. exports =  main; 
➜  workspace git: ( main)  ✗ truffle compile -- all
Compiling your contracts... 
=== === === === === === === === === 
✓ Fetching solc version list from  solc- bin.  Attempt #1 
✓ Downloading compiler.  Attempt #1. 
>  Compiling . / contracts/ ERC4907 . sol
>  Compiling . / contracts/ IERC4907 . sol
>  Compiling . / contracts/ RentablePets. sol
>  Compiling @openzeppelin/ contracts/ token/ ERC721 / ERC721 . sol
>  Compiling @openzeppelin/ contracts/ token/ ERC721 / IERC721 . sol
>  Compiling @openzeppelin/ contracts/ token/ ERC721 / IERC721Receiver. sol
>  Compiling @openzeppelin/ contracts/ token/ ERC721 / extensions/ ERC721URIStorage. sol
>  Compiling @openzeppelin/ contracts/ token/ ERC721 / extensions/ IERC721Metadata. sol
>  Compiling @openzeppelin/ contracts/ utils/ Address. sol
>  Compiling @openzeppelin/ contracts/ utils/ Context. sol
>  Compiling @openzeppelin/ contracts/ utils/ Counters. sol
>  Compiling @openzeppelin/ contracts/ utils/ Strings. sol
>  Compiling @openzeppelin/ contracts/ utils/ introspection/ ERC165 . sol
>  Compiling @openzeppelin/ contracts/ utils/ introspection/ IERC165 . sol
>  Compiling @openzeppelin/ contracts/ utils/ math/ Math. sol
>  Artifacts written to / project/ workspace/ build/ contracts
>  Compiled successfully using: 
   -  solc:  0.8 .15 + commit. e14f2714. Emscripten. clang
➜  workspace git: ( main)  ✗ truffle migrate
Compiling your contracts... 
=== === === === === === === === === 
>  Everything is up to date,  there is nothing to compile. 
Starting migrations... 
=== === === === === === === = 
>  Network name:     'development' 
>  Network id:       1729358815666 
>  Block gas limit:  6721975  ( 0x6691b7 ) 
1 _deploy_contracts. js
=== === === === === === === 
   Deploying 'RentablePets' 
   -- -- -- -- -- -- -- -- -- -- -- -- 
   >  transaction hash:     0xe175c15f56056f7234713dd721c5761d5e5316f8598db9ac030d067b0bd61d2c 
   >  Blocks:  0             Seconds:  0 
   >  contract address:     0x4cd45fa514493686dC9ebd9B82F2b484C4A04791 
   >  block number:         1 
   >  block timestamp:      1729359105 
   >  account:              0xE80E1ab42c8daD385cff236eA11495C719529617 
   >  balance:              99.93983968 
   >  gas used:             3008016  ( 0x2de610 ) 
   >  gas price:            20  gwei
   >  value sent:           0  ETH 
   >  total cost:           0.06016032  ETH 
   >  Saving artifacts
   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 
   >  Total cost:           0.06016032  ETH 
Summary
=== === = 
>  Total deployments:    1 
>  Final cost:           0.06016032  ETH 
➜  workspace git: ( main)  ✗ truffle console
truffle ( development) >  const  Web3 =  require ( 'web3' ) ; 
undefined 
truffle ( development) >  const  web3 =  new  Web3 ( 'http://127.0.0.1:8545' ) ; 
undefined 
truffle ( development) >  const  v =  web3. eth. getAccounts ( ) ; 
undefined 
truffle ( development) >  console. log ( v) 
Promise { 
  [ 
    '0xE80E1ab42c8daD385cff236eA11495C719529617' , 
    '0x5370ab73FCe6E6379065b1bcc28ca2AADf25CcC3' , 
    '0x5e70aa3FF93611F24779eA6d061af288d9E76d29' , 
    '0x78492007d7Fd3c2E367C940c69fB74314ddDa736' , 
    '0x25106c3591c2e3bB48A0D68e3d1252bb4Aa90CDE' , 
    '0x79B45863062ae0216143369De21442F92A1176b7' , 
    '0x4A31abd39EC9ce5F1471ffA6e48a9c65B70D6Da7' , 
    '0xf041c963841d797065dB5AeF0D6D146D7E3f2Fb0' , 
    '0x420D956ad2407533A0cD140d53B826E8D4B1BB95' , 
    '0x87AcFD4c4E02bD337978AB089dF24F83eceB5f4d' 
  ] , 
  [ Symbol ( async_id_symbol) ] :  448 , 
  [ Symbol ( trigger_async_id_symbol) ] :  8 
} 
undefined 
truffle ( development) >  const  rentablePets =  await  RentablePets. deployed ( ) 
undefined 
truffle ( development) >  let  txn =  await  rentablePets. mint ( "fakeURI" ,  { from :  '0xE80E1ab42c8daD385cff236eA11495C719529617' } ) 
undefined 
truffle ( development) >   console. log ( txn) ; 
{ 
  tx:  '0x719254cfa3be55c078e2800a820a04d338862c69a627df16e4d63df4871613b4' , 
  receipt:  { 
    transactionHash:  '0x719254cfa3be55c078e2800a820a04d338862c69a627df16e4d63df4871613b4' , 
    transactionIndex:  0 , 
    blockHash:  '0xa91f80b69644dd3bb8b7d96783230a9163ebfa851cd1ca8ce67434f9811c1147' , 
    blockNumber:  2 , 
    from :  '0xe80e1ab42c8dad385cff236ea11495c719529617' , 
    to:  '0x4cd45fa514493686dc9ebd9b82f2b484c4a04791' , 
    gasUsed:  114934 , 
    cumulativeGasUsed:  114934 , 
    contractAddress:  null , 
    logs:  [  [ Object]  ] , 
    status:  true , 
    logsBloom:  '0x00000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000008000000000000000000040000000000000000000000000000020000000000000000000800000000000000000000000010000000000000000000000000000000000400000000000001000000000000000000000000000004000000000000000000000000000080000000000000000000000000000000000002000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000' , 
    rawLogs:  [  [ Object]  ] 
  } , 
  logs:  [ 
    { 
      logIndex:  0 , 
      transactionIndex:  0 , 
      transactionHash:  '0x719254cfa3be55c078e2800a820a04d338862c69a627df16e4d63df4871613b4' , 
      blockHash:  '0xa91f80b69644dd3bb8b7d96783230a9163ebfa851cd1ca8ce67434f9811c1147' , 
      blockNumber:  2 , 
      address:  '0x4cd45fa514493686dC9ebd9B82F2b484C4A04791' , 
      type:  'mined' , 
      removed:  false , 
      id:  'log_4e200077' , 
      event:  'Transfer' , 
      args:  [ Result] 
    } 
  ] 
} 
undefined 
truffle ( development) >  
  workspace git: ( main)  ✗ ganache- cli                
Ganache CLI  v6. 12.2  ( ganache- core:  2.13 .2 ) 
Available Accounts
=== === === === === === 
( 0 )  0 xE80E1ab42c8daD385cff236eA11495C719529617  ( 100  ETH ) 
( 1 )  0 x5370ab73FCe6E6379065b1bcc28ca2AADf25CcC3  ( 100  ETH ) 
( 2 )  0 x5e70aa3FF93611F24779eA6d061af288d9E76d29  ( 100  ETH ) 
( 3 )  0 x78492007d7Fd3c2E367C940c69fB74314ddDa736  ( 100  ETH ) 
( 4 )  0 x25106c3591c2e3bB48A0D68e3d1252bb4Aa90CDE  ( 100  ETH ) 
( 5 )  0 x79B45863062ae0216143369De21442F92A1176b7  ( 100  ETH ) 
( 6 )  0 x4A31abd39EC9ce5F1471ffA6e48a9c65B70D6Da7  ( 100  ETH ) 
( 7 )  0 xf041c963841d797065dB5AeF0D6D146D7E3f2Fb0  ( 100  ETH ) 
( 8 )  0 x420D956ad2407533A0cD140d53B826E8D4B1BB95  ( 100  ETH ) 
( 9 )  0 x87AcFD4c4E02bD337978AB089dF24F83eceB5f4d  ( 100  ETH ) 
Private Keys
=== === === === === === 
( 0 )  0 xe398d80025ef59f3d1bc31bd55eac87ed2ba30e267a2b1de2f7a2d9bbe1ef573 
( 1 )  0 xefff1fb2953c1808bbc184a502139efc462f6edcd379a98135f3a01999ec721d 
( 2 )  0 xee46e7e7b3f38de97dfdd4b01f6c17866787a9c9b1372c452c04813a2755c1c5 
( 3 )  0 xb1ff4eacd6a96aadb020cac360eed51fb29acf7ed3d097dd307f4a6b5f655f2d 
( 4 )  0 x13a1679fb9420333f814506d384c7bef6a6c923353f389016c5944e5da068998 
( 5 )  0 xfa5f628ac0835d57b433c8278d1fe0d3f7c7f8fbaea74711f8b4decd671f09ec 
( 6 )  0 xd3acdd34343ab8c455fbe3734449dc50fedeb7973de090766a99e0836fa46b1c 
( 7 )  0 x5d20c3b08b8c51a50022d4ec5eb84531b06c71ec805de6aebf1b0b7c42c02154 
( 8 )  0 x9b9920b4b9d3d07dd0069438dc040b95d42ec29e38bee10d63095aff356eb018 
( 9 )  0xcfa72ab66dbc451b803828eda8c6a2ce86925af8f98f8b0c97b6964e6885daf7 
HD  Wallet
=== === === === === === 
Mnemonic:       shop rule advance release dumb unveil pretty where search attract name exclude
Base HD  Path:   m/ 44 '/60' / 0 '/ 0 / { account_index} 
Gas Price
=== === === === === === 
20000000000 
Gas Limit
=== === === === === === 
6721975 
Call Gas Limit
=== === === === === === 
9007199254740991 
Listening on 127.0 .0 .1 : 8545 
eth_blockNumber
net_version
eth_accounts
eth_getBlockByNumber
eth_accounts
net_version
eth_getBlockByNumber
eth_getBlockByNumber
net_version
eth_getBlockByNumber
eth_estimateGas
net_version
eth_blockNumber
eth_getBlockByNumber
eth_estimateGas
eth_getBlockByNumber
eth_gasPrice
eth_sendTransaction
  Transaction:  0xe175c15f56056f7234713dd721c5761d5e5316f8598db9ac030d067b0bd61d2c 
  Contract created:  0x4cd45fa514493686dc9ebd9b82f2b484c4a04791 
  Gas usage:  3008016 
  Block Number:  1 
  Block Time:  Sat Oct 19  2024  17 : 31 : 45  GMT + 0000  ( Coordinated Universal Time) 
eth_getTransactionReceipt
eth_getCode
eth_getTransactionByHash
eth_getBlockByNumber
eth_getBalance
eth_blockNumber
net_version
eth_accounts
eth_accounts
eth_blockNumber
net_version
eth_accounts
eth_accounts
eth_accounts
eth_getBlockByNumber
eth_getBlockByNumber
eth_estimateGas
eth_getBlockByNumber
eth_gasPrice
eth_sendTransaction
  Transaction:  0x719254cfa3be55c078e2800a820a04d338862c69a627df16e4d63df4871613b4 
  Gas usage:  114934 
  Block Number:  2 
  Block Time:  Sat Oct 19  2024  17 : 40 : 55  GMT + 0000  ( Coordinated Universal Time) 
eth_getTransactionReceipt
eth_getBlockByNumber
eth_getBlockByNumber
eth_getBlockByNumber
eth_getBlockByNumber
eth_getBlockByNumber
eth_getBlockByNumber