Solidity
從帳戶更改 WEB3J 中的 ganache 所有者帳戶000記賬111或帳戶222
我正在尋找一種方法來更改 java web3j 中智能合約的所有者,就像我們在 web3 javascript 中所做的那樣,使用
from
://using account for transaction in javascript await contract.function(param1, param2, {from: account1, value: balance}); await contract.function(param1, param2, {from: account2, value: balance});
對於 java web3j 更改交易帳戶,我找不到任何解決方案。
contract.function(param1, param2).sendAsync();
它使用預設帳戶
$$ 0 $$對於所有交易。我想更改各種交易的所有者。如何用
{from: account2, value: balance})
java編寫 很高興為 web3j 更改帳戶獲得合適的答案,謝謝。我堅持的程式碼:
public class accessControl { Web3j web3j = Web3j.build(new HttpService("HTTP://127.0.0.1:9545")); private final static String PRIVATE_KEY = "*************************"; private final static BigInteger GAS_LIMIT = valueOf(6721975L); private final static BigInteger GAS_PRICE = valueOf(20000000000L); String ownerID = web3j.ethAccounts().send().getAccounts().get(0); String manufacturerID = web3j.ethAccounts().send().getAccounts().get(1); String wholesalerID = web3j.ethAccounts().send().getAccounts().get(2); String retailerID = web3j.ethAccounts().send().getAccounts().get(3); private final String CONTRACT_ADDRESS = deployContract(web3j, getCredentialsFromPrivateKey()); public static void main(String[] args) { try { new accessControl(); } catch (Exception e) { e.printStackTrace(); } } private accessControl() throws Exception { printWeb3Version(web3j); System.out.println("<-------------------ACCOUNTS----------------------->"); System.out.println("CONTRACT ADDRESS: " + CONTRACT_ADDRESS); System.out.println("Contract Owner: " + ownerID); System.out.println("Manufacturer ID: " + manufacturerID); System.out.println("Wholesaler ID: " + wholesalerID); System.out.println("Retailer ID: " + retailerID); //System.out.println("DA: "+deployAddress); SupplyChain supplychain= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey()); supplychain.addRetailer(retailerID).send(); supplychain.addWholesaler(wholesalerID).send(); supplychain.addManufacturer(manufacturerID).send(); supplychain.produceItemByManufacturer(valueOf(1), "mullick", "milk", valueOf(1)).send(); supplychain.packageItemByManufacturer(valueOf(1)).send(); supplychain.sellItemByManufacturer(valueOf(1), valueOf(1)).send(); supplychain.purchaseItemByWholesaler(valueOf(1), "mr. mullick", BigInteger.valueOf(1)).send(); supplychain.shippedItemByManufacturer(valueOf(1)).send(); supplychain.receivedItemByWholesaler(valueOf(1)).send(); supplychain.sellItemByWholesaler(valueOf(1), BigInteger.valueOf(1)).send(); supplychain.purchaseItemByRetailer(valueOf(1),"mrs. mullick", BigInteger.valueOf(1)).send(); supplychain.shippedItemByWholesaler(valueOf(1)).send(); supplychain.receivedItemByRetailer(valueOf(1)).send(); supplychain.fetchItemBufferOne(valueOf(1)).send(); supplychain.fetchItemBufferTwo(valueOf(1)).send(); supplychain.fetchWholesaler(valueOf(1)).send(); supplychain.fetchRetailer(valueOf(1)).send(); supplychain.fetchitemHistory(valueOf(1)).send(); } private void printWeb3Version(Web3j web3j) { Web3ClientVersion web3ClientVersion = null; try { web3ClientVersion = web3j.web3ClientVersion().send(); } catch (IOException e) { e.printStackTrace(); } assert web3ClientVersion != null; String web3ClientVersionString = web3ClientVersion.getWeb3ClientVersion(); System.out.println("Web3j client version: " + web3ClientVersionString); } private Credentials getCredentialsFromPrivateKey() { return Credentials.create(PRIVATE_KEY); } private String deployContract(Web3j web3j, Credentials credentials) throws Exception { return SupplyChain.deploy(web3j, credentials, GAS_PRICE, GAS_LIMIT).send().getContractAddress(); } private SupplyChain loadContract(String contractAddress, Web3j web3j, Credentials credentials) throws Exception { return SupplyChain.load(CONTRACT_ADDRESS, web3j, credentials, GAS_PRICE, GAS_LIMIT); } }
當所有者從製造商(帳戶)更改時,我需要更改 ID
$$ 1 $$)-> 批發商(帳號$$ 2 $$) -> 零售商(帳戶$$ 3 $$),與他們的 ID 相對應,因此付款會從他們的帳戶中扣除,而不是從帳戶中扣除$$ 0 $$每次
你用
getCredentialsFromPrivateKey()
在載入合約中,將私鑰更改為賬戶擁有的私鑰。您可以為每個您想要的帳戶創建一份契約。
編輯:
private Credentials getCredentialsFromPrivateKey(String private_key) { return Credentials.create(private_key); }
比
private accessControl() throws Exception { ... SupplyChain supply_chain_account0= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey(account0_privatekey)); SupplyChain supply_chain_account1= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey(account1_privatekey)); SupplyChain supply_chain_account2= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey(account2_privatekey)); ... }
正如你所展示的,我所做的改變,
private final static String account0_privatekey = "0**********"; private final static String account1_privatekey = "1**********"; private final static String account2_privatekey = "2**********"; private final static String account3_privatekey = "3**********"; //generate contract address private final String CONTRACT_ADDRESS = deployContract(web3j, getCredentialsFromPrivateKey(account0_privatekey));
和,
private accessControl() throws Exception { .... SupplyChain supply_chain_account0= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey(account1_privatekey)); SupplyChain supply_chain_account1= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey(account1_privatekey)); SupplyChain supply_chain_account2= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey(account2_privatekey)); SupplyChain supply_chain_account3= loadContract(CONTRACT_ADDRESS, web3j, getCredentialsFromPrivateKey(account3_privatekey)); supply_chain_account0.addRetailer(retailerID).send(); supply_chain_account0.addWholesaler(wholesalerID).send(); supply_chain_account0.addManufacturer(manufacturerID).send(); supply_chain_account1.produceItemByManufacturer(valueOf(1), "mullick", "milk", valueOf(1)).send(); supply_chain_account1.packageItemByManufacturer(valueOf(1)).send(); supply_chain_account1.sellItemByManufacturer(valueOf(1), valueOf(1)).send(); supply_chain_account2.purchaseItemByWholesaler(valueOf(1), "mr. mullick", valueOf(1)).send(); supply_chain_account1.shippedItemByManufacturer(valueOf(1)).send(); supply_chain_account2.receivedItemByWholesaler(valueOf(1)).send(); supply_chain_account2.sellItemByWholesaler(valueOf(1), valueOf(1)).send(); supply_chain_account3.purchaseItemByRetailer(valueOf(1),"mrs. mullick", valueOf(1)).send(); supply_chain_account2.shippedItemByWholesaler(valueOf(1)).send(); supply_chain_account3.receivedItemByRetailer(valueOf(1)).send(); .... }
現在它根本不執行並拋出錯誤。非常感謝 :D