Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple-storage with deploy access list #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions simple-storage/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Sample environment file - change all values as needed

# Privkey for each deployment address
CYPRUS1PK="0x0000000000000000000000000000000000000000000000000000000000000000"
CYPRUS2PK="0x0000000000000000000000000000000000000000000000000000000000000000"
CYPRUS3PK="0x0000000000000000000000000000000000000000000000000000000000000000"
PAXOS1PK="0x0000000000000000000000000000000000000000000000000000000000000000"
PAXOS2PK="0x0000000000000000000000000000000000000000000000000000000000000000"
PAXOS3PK="0x0000000000000000000000000000000000000000000000000000000000000000"
HYDRA1PK="0x0000000000000000000000000000000000000000000000000000000000000000"
HYDRA2PK="0x0000000000000000000000000000000000000000000000000000000000000000"
HYDRA3PK="0x0000000000000000000000000000000000000000000000000000000000000000"

# RPC endpoints
CYPRUS1URL="https://rpc.cyprus1.colosseum.quaiscan.io"
CYPRUS2URL="https://rpc.cyprus2.colosseum.quaiscan.io"
CYPRUS3URL="https://rpc.cyprus3.colosseum.quaiscan.io"
PAXOS1URL="https://rpc.paxos1.colosseum.quaiscan.io"
PAXOS2URL="https://rpc.paxos2.colosseum.quaiscan.io"
PAXOS3URL="https://rpc.paxos3.colosseum.quaiscan.io"
HYDRA1URL="https://rpc.hydra1.colosseum.quaiscan.io"
HYDRA2URL="https://rpc.hydra2.colosseum.quaiscan.io"
HYDRA3URL="https://rpc.hydra3.colosseum.quaiscan.io"
122 changes: 122 additions & 0 deletions simple-storage/1.deploy-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@

/* Example contract:
* // SPDX-License-Identifier: MIT
* pragma solidity ^0.8.0;
*
* contract SimpleStorage {
* uint256 private storedValue;
*
* event ValueSet(uint256 value, address indexed sender);
*
* function set(uint256 value) public {
* storedValue = value;
* emit ValueSet(value, msg.sender);
* }
*
* function get() public view returns (uint256) {
* uint256 value = storedValue;
* return value;
* }
* }
*
*/

const quais = require('quais');
const fs = require('fs');
require('dotenv').config();

// Prerequisites: compile the smart contract and generate artifacts, i.e. run:
// solc --bin --abi SimpleStorage.sol --output-dir artifacts

const abi = JSON.parse(fs.readFileSync('artifacts/SimpleStorage.abi', 'utf8'));
const bytecode = fs.readFileSync('artifacts/SimpleStorage.bin', 'utf8');

async function main() {
const RPC_URL = 'http://localhost';
const provider = new quais.JsonRpcProvider(RPC_URL);

const wallet = new quais.Wallet(process.env.CYPRUS1PK, provider);
console.log('Wallet address:', wallet.address);

// get address balance
let balance = await provider.getBalance(wallet.address);
console.log('Balance:', balance.toString());

// Deploy contract
const factory = new quais.ContractFactory(abi, bytecode, wallet);

let nonce = await provider.getTransactionCount(wallet.address, 'latest');

gasLimit = 8400000;
maxFeePerGas = 1000000000n;
maxPriorityFeePerGas = 1000000000n;

const deployParams = {
nonce,
gasLimit: gasLimit,
maxPriorityFeePerGas: maxFeePerGas,
maxFeePerGas: maxPriorityFeePerGas,
from: wallet.address,
};

const deployTransaction = await factory.getDeployTransaction(deployParams);

// Modify the deploy transaction
const modifiedDeployTransaction = {
...deployTransaction,
// Replace or add fields as needed
gasLimit: "0x" + (gasLimit).toString(16),
maxFeePerGas: "0x" + (maxFeePerGas).toString(16),
maxPriorityFeePerGas: "0x" + (maxPriorityFeePerGas).toString(16),
nonce: "0x" + nonce.toString(16)
};

const accessListResponse = await provider.send("quai_createAccessList", [modifiedDeployTransaction, "latest"], quais.Shard.Cyprus1);

// Format the accessList
const formattedAccessList = accessListResponse.accessList.map(item => ({
address: quais.getAddress(item.address), // Ensure the address is correctly formatted
storageKeys: item.storageKeys.map(key => quais.hexlify(key)) // Ensure storage keys are correctly formatted
}));

console.log('Formatted accessList:', formattedAccessList);

const accessListTransaction = {
...deployParams,
accessList: formattedAccessList
};

let contract = await factory.deploy(accessListTransaction);
const contractAddress = await contract.getAddress();
console.log('Contract address:', contractAddress);

// wait for contract to be deployed
const receipt = await contract.waitForDeployment();
console.log('receipt:', receipt);

// Subscribe to events
contract.on('ValueSet', (value, sender) => {
console.log(`\n\n'ValueSet' event detected => value: ${value.toString()}, sender: ${sender}\n\n`);
});

// Write to contract
contract = new quais.Contract(contractAddress, abi, wallet);

const tx = await contract.set(42);
console.log('Transaction:', tx);

await tx.wait();
console.log('Value set in contract');

// Read from contract
contract = new quais.Contract(contractAddress, abi, wallet);
const value = await contract.get();
console.log('Stored value in contract:', value.toString());
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
18 changes: 18 additions & 0 deletions simple-storage/SimpleStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
uint256 private storedValue;

event ValueSet(uint256 value, address indexed sender);

function set(uint256 value) public {
storedValue = value;
emit ValueSet(value, msg.sender);
}

function get() public view returns (uint256) {
uint256 value = storedValue;
return value;
}
}
1 change: 1 addition & 0 deletions simple-storage/artifacts/SimpleStorage.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"ValueSet","type":"event"},{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"}]
1 change: 1 addition & 0 deletions simple-storage/artifacts/SimpleStorage.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b506101cb806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806360fe47b11461003b5780636d4ce63c14610057575b600080fd5b61005560048036038101906100509190610117565b610075565b005b61005f6100cd565b60405161006c9190610153565b60405180910390f35b806000819055503373ffffffffffffffffffffffffffffffffffffffff167ff421846e535e82c1fbd6ac49ea86e258f1940223db04456b26480f6f2db9f6a7826040516100c29190610153565b60405180910390a250565b60008060005490508091505090565b600080fd5b6000819050919050565b6100f4816100e1565b81146100ff57600080fd5b50565b600081359050610111816100eb565b92915050565b60006020828403121561012d5761012c6100dc565b5b600061013b84828501610102565b91505092915050565b61014d816100e1565b82525050565b60006020820190506101686000830184610144565b9291505056fea264697066735822122065bd816f21f04b6741180b00e5641d792484be9f3c751aa97e25c8f3c470148264736f6c637829302e382e31392d646576656c6f702e323032332e31312e32302b636f6d6d69742e6338386634306664005a
15 changes: 15 additions & 0 deletions simple-storage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "contract-erc20",
"version": "0.0.1",
"description": "A basic example of deploying an ERC20 contract.",
"author": "Dominant Strategies Inc.",
"contributors": [
"Juuddi"
],
"dependencies": {
"@openzeppelin/contracts": "^5.0.1",
"dotenv": "^16.4.4",
"quais": "^1.0.0-alpha.6",
"quais-polling": "^1.0.4"
}
}