Solidity

如何從 Ganache 獲取地址?

  • September 21, 2022

我正在嘗試在 Remix IDE 中實現虛擬智能投票。

我想從Ganache獲取剩餘的 99 個地址來創建一個迭代並讓他們在投票站註冊然後投票。

我如何獲得這些地址?

在此處輸入圖像描述

在 Ganache 上,您可以點擊設置按鈕:

在此處輸入圖像描述

然後在 Accounts & Keys 按鈕上,將 TOTAL ACCOUNTS TO GENERATE 值設置為 100:

在此處輸入圖像描述

然後,您可以使用web3獲取包含所有帳戶的數組:

const Web3 = require("web3");

const web3 = new Web3("http://127.0.0.1:7545");

web3.eth.getAccounts().then((accounts) => {
 console.log("accounts: ", accounts);
});

就我而言,它返回:

[
 '0xcb21123cD81A08B754735712e099a5Ed5e7748e3',
 '0x520D209015098CC4D9797183e9F9305AbfFa5870',
 '0xdfDa1e966F26E93FFdEfb589fdf66A30f953a0aC',
 '0xD98A293Ca55b8D3f791327ade3e07880f099451c',
 '0x1827b4Ab3769df679A1BaAB75591C787D89770C9',
 '0xCbCC0339969593BA7BdA79fAf34993dD8A52B481',
 '0xfdEe4cE8B0Fd655fA079A22445feA29E5342593b',
 '0x86466bf947510c5F3798e4bfeE9031aaBedC89E2',
 '0x685908bFde40cE2e57a8EcA3Ce6251D0F157616C',
 '0xFd4152bb8138Af7be842765Ee6DBeaAC2093f2f7',
 '0x8be98fc957A113AF65CFf11CDECc7F463C5505c2',
 '0x4222DbAfa498C3116995FA924d0F0551B94f51A5',
 '0xf13BF546f8D54524E3B2ADe22C68832991b8f77a',
 '0xf11efc833E829091AE6922CdC729f38277ad22c1',
 '0xAA7C92c66E2aD7d32B9e2053a1Db8154B420E538',
 '0x797461AE98AD5eb234b68b295Ae8F8Ee9FfAe9bf',
 '0x84A0bB7A97fa7737a3144a76222E25EE7B96a0e6',
 '0x0FFf1a9A172AB40aFf518883231d5508a6e69eA4',
 '0xddb693bDA0d0bE777195444186e474109181f290',
 '0xFCC281748e66EA6d8a50Bcf3Bb21DEb8aC67B773',
 '0xf16f23934369e8D39C9919925f60D829173cB17f',
 '0x65bBCa00980721Dbd3c07D312d9303562B4F95C0',
 '0x427D5cbc30D944EB470AFbef3078Ac6d47166d7C',
 '0x203a32d4f3959A7646Bd42d335A78d817923058a',
 '0x5025f7Ac76eF2Fb93956ce98a4bf7FdCDaDAc00b',
 '0x33deEDFcB2C5296fd7A5E6e811Ac5e835D8A9c41',
 '0x331651bF97A46818Ea6553F08589010CC3435EA9',
 '0x3F33E9018d666B2A4c52567d9C6919c02A1f6389',
 '0xCebe1F74B38e7b26849dcBFc660F11931c559535',
 '0x4323e51939daD78de33ACa92D6b2E761ca8F7A44',
 '0xBcfc3DdEf7eE7A18AF7F8d2B9f3b73Ffc1Df176A',
 '0xE25af33EB7A658B62de495F7249715c5Bc2d2036',
 '0x9Dd5F7B2C9df7A6f2Fc38D38ef52618b56Faccb4',
 '0x75772b1cb54164E7634D582D982b664053dEC701',
 '0x7093993aD560a439fAe313f771E7899D10F10E4b',
 '0xF71716876c355C99da56467758195A23E9edB0E9',
 '0xb8F84fF8a4b8B6F0d37afFF5556118803880D17A',
 '0xb23a82F85f95c6DC3a277A2DBA02E9CFd20D8B0f',
 '0x6c4Ab420c9E178D470Fc829D9Dc49e82296d54bd',
 '0x27e0E996273fF9C7cEc8Eb94c165726c6eFc0147',
 '0x33b3Cb9eE7130608918F70C5c93dd710b72151a5',
 '0x887409002C5C8D95D6036572eFD2d564689D6Ec4',
 '0xAC30F961551Bd8c7bb70f2e36d04904c6F1Ba739',
 '0xa41e3400E8EeE0C5C4191E5c161B7FCc865131e7',
 '0x68dbd6607ba4965434c7072Dc2C990b9BaBD21a1',
 '0x8928D01D14Ec13f9350710A234Eb27BBfb87A1C6',
 '0xEECD9Fb5e4407F8C8519D487659Bba8224b17009',
 '0xc12B6F6Df80f5Df669e976C920A3B0F560ecfEe1',
 '0xE45bED92a7CDf9f1B16dF31500084B94F9429247',
 '0xac7a859046382BF85E800FD2a705E0965C4d3125',
 '0xcFD6a0f7495502a1d04EE4c1B594DA51cf1A6C8c',
 '0x8d32BBe6960D16C1fC0baaC28d53193EDE466d36',
 '0xf76CeBBD5e47018ff731E02dBbf5423398cf0d4B',
 '0x4E1178C9d2b2E02C6B56D8877b890D0ff80cc048',
 '0x649F4e6dBbD51b5F9bf82e2De30335DAC276585E',
 '0x7F798fd4795fDaBd05fD98907046548e98a24471',
 '0x806552e28574cA95a29684ce325586D8a94c50a4',
 '0x1F152a96217788F1816c0A3340f17700a8Ba9821',
 '0x4688dfF7688aFCaf99F3e9c1ADC5608bd719642E',
 '0xfaa675eD00948c878658D051087D5BdA94B538B0',
 '0xf666c9CE798DED1CaEEbcf977099B00bfBEFEAAe',
 '0x5c098ff352a653ed9897D8a1ae41b990a9b29bb8',
 '0x2968f823629C468132b42644EFfD4d77bb970bCD',
 '0x47e7e4A2A25E3181957FC76Ccb2C4851B67eE575',
 '0x2f4Fb29dC9FAE0a4E3cB6661F858a2AACCbA81d9',
 '0xBaE9f00612F16cFac2D02c74891351024C5D38fa',
 '0xa90F527cEEd0bD90D5300A37a5bEbDF919862C65',
 '0x4D847040D58F96941d9c2fb34a6a1E6491A8B2aC',
 '0x7bE7E3ff9925054e553263b506c739a2AE55b84d',
 '0xE38b7b65337CFF3F1db09febCE7d43A1bC257552',
 '0x9c10d2bb11af54F3443C420dF9072177102B96C4',
 '0x05F4Fdd33474a4Bd56744f9aBa799277f808Db48',
 '0xF011eee2638B1A2988E0a0115688665eC3dA27c9',
 '0x742b61D97f6bD7fC403c9162874B5eA13A5b2f3b',
 '0x046f6a982C84816d8f54C3A7935D52B50f8fDA35',
 '0xa025fa7E2dfc7B33bd5ed9107DE0dcEdad0529A8',
 '0xaf7Bb0E651D8a36Be89eAbCE4D6419728F389fb2',
 '0xd3C98E67559Be1944E7497bfEF5F3a49bE2b4e31',
 '0x3382f8361d1005785fcc78351a44fb9aAf4CEC2E',
 '0x9616f596cEd138C1B9FD1f6F784B6ADF3adC593E',
 '0xB0A755761C63CBe86C15905A0b7a40C3a2033000',
 '0xCbaF8F9FC2b6c76ac6dc45fBD563D12bBAd99ebf',
 '0x2d3F177Af44a94820824dC6eA51C0bb44D7f12FA',
 '0xB246f6266a96AbB4dc19a2B9F36A199443E37DB9',
 '0xca2930F1F6B8caC276d390b1693FBFDF738FE0aa',
 '0x7f3BA300Ea1Ac5AE9e62Efb0ED5DBeDE75687566',
 '0xb39EEFA6344b022AD3Ab22c5752062256A44671D',
 '0x966e73f3E67cf2580254Ec9f0D64C246184a4058',
 '0xe5D5378d394eE93C7d28AC65bd90760686A8f138',
 '0x2B3940b944B530233301C4bE9329485316E0C7BC',
 '0xf337402D16340079485C1e5BFdeC2AF71fde55F2',
 '0x033EA26B11D7608D061aB3E76C1553f222D3BAa6',
 '0x5772C785072B5bAA3DA6DDce9A78eD3B55B062Fe',
 '0x708E9913a515dE216F758fabd6574eAA9416Cd61',
 '0x994d8a1E43a5dE3f12B79602AF38Df268Ba0d060',
 '0xE8e6dadC6E18d47f36eDDF63457e18679AB2598A',
 '0x4961F86DBF240BFDaa141D8659289e8E174c8084',
 '0xace54FcFae6967BdF0A9BecfCF60c8c23dFcDC1B',
 '0xb920DCF7F525766927A5fA26062fEeF33fAaf787',
 '0x777429AcDBE49d6FFBcf4Ab2Ffd8e78ea94ac940'
]

讓我知道這是否有幫助。

要在合約中接收一組地址並對其進行處理,您可以嘗試:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

contract Contract {

   address[] public addresses;

   function getAddresses() public view returns(address[] memory) {
       return addresses;
   }

   // Adds one address to the storage addresses array 
   function addAddress(address addr) public {
       addresses.push(addr);
   }

   // Replaces the storage addresses with this new array of addresses
   function addAddresses(address[] calldata _addresses) public {
       addresses = _addresses;
   }

   // Does something on an array of address passed
   function operate(address[] calldata _addresses) public pure {

       for(uint256 i = 0; i < _addresses.length; i++) {
           address addr = _addresses[i];
           // do something with addr
       }

   }

   // Does something on the storage array of addresses
   function operateOnStorageArrayOfAddresses() public view {
       // Makes a copy of the storage address in memory to save gas
       address[] memory addressesCopy = addresses;
       for(uint256 i = 0; i < addressesCopy.length; i++) {
           address addr = addressesCopy[i];
           // do something with addr
       }

   }

}

要通過發送和接收地址與合約進行互動web3,您可以執行以下操作:

const Web3 = require("web3");

const web3 = new Web3("http://127.0.0.1:7545");

web3.eth.getAccounts().then((accounts) => {
 // console.log("accounts: ", accounts);
});

const abi = [
 {
   inputs: [
     {
       internalType: "address",
       name: "addr",
       type: "address",
     },
   ],
   name: "addAddress",
   outputs: [],
   stateMutability: "nonpayable",
   type: "function",
 },
 {
   inputs: [
     {
       internalType: "address[]",
       name: "_addresses",
       type: "address[]",
     },
   ],
   name: "addAddresses",
   outputs: [],
   stateMutability: "nonpayable",
   type: "function",
 },
 {
   inputs: [
     {
       internalType: "uint256",
       name: "",
       type: "uint256",
     },
   ],
   name: "addresses",
   outputs: [
     {
       internalType: "address",
       name: "",
       type: "address",
     },
   ],
   stateMutability: "view",
   type: "function",
 },
 {
   inputs: [],
   name: "getAddresses",
   outputs: [
     {
       internalType: "address[]",
       name: "",
       type: "address[]",
     },
   ],
   stateMutability: "view",
   type: "function",
 },
 {
   inputs: [
     {
       internalType: "address[]",
       name: "_addresses",
       type: "address[]",
     },
   ],
   name: "operate",
   outputs: [],
   stateMutability: "pure",
   type: "function",
 },
 {
   inputs: [],
   name: "operateOnStorageArrayOfAddresses",
   outputs: [],
   stateMutability: "view",
   type: "function",
 },
];

const contractAddress = "0x99Aeb08Ab2fD121138D02F0e5b20D57e0a3a6082";

const contract = new web3.eth.Contract(abi, contractAddress, {
 from: "0xcb21123cD81A08B754735712e099a5Ed5e7748e3",
 gas: 3000000,
});

async function sendAndGetAddresses() {
 const addresses = await web3.eth.getAccounts();

 await contract.methods.addAddresses(addresses).send();

 const retrievedAddressesFromTheContract = await contract.methods
   .getAddresses()
   .call();

 console.log("retrievedAddresses: ", retrievedAddressesFromTheContract);
}

sendAndGetAddresses();

引用自:https://ethereum.stackexchange.com/questions/136076