-
-
Save gangadharjannu/58be48c833183ec76de18710c281af5a to your computer and use it in GitHub Desktop.
compiling and sending ehter to a payable function from nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ethers, { | |
Contract, | |
providers as Providers, | |
utils as Utils, | |
Wallet, | |
ContractFactory | |
} from "ethers" | |
import path from "path" | |
import fs from "fs" | |
import solc from "solc" | |
const privateKey = "0x123123123" | |
export const setupContract = async () => { | |
// Connect to the network | |
const url = "http://localhost:8545" | |
const provider = new Providers.JsonRpcProvider(url) | |
const wallet = new Wallet(privateKey, provider) | |
console.log("wallet address ", wallet.address) | |
const input = { | |
"openzeppelin-solidity/contracts/access/Roles.sol": fs.readFileSync( | |
path.resolve( | |
__dirname, | |
"..", | |
"..", | |
"blockchain", | |
"node_modules", | |
"openzeppelin-solidity", | |
"contracts", | |
"access", | |
"Roles.sol" | |
), | |
"UTF-8" | |
), | |
"User.sol": fs.readFileSync( | |
path.resolve( | |
__dirname, | |
"..", | |
"..", | |
"blockchain", | |
"contracts", | |
"User.sol" | |
), | |
"UTF-8" | |
), | |
"SimpleAuth.sol": fs.readFileSync( | |
path.resolve( | |
__dirname, | |
"..", | |
"..", | |
"blockchain", | |
"contracts", | |
"SimpleAuth.sol" | |
), | |
"UTF-8" | |
) | |
} | |
const output = solc.compile({ sources: input }, 1) | |
console.log("output ", output) | |
const bytecode = output.contracts["SimpleAuth.sol:SimpleAuth"].bytecode | |
const abi = JSON.parse( | |
output.contracts["SimpleAuth.sol:SimpleAuth"].interface | |
) | |
console.log("abi: ", JSON.stringify(abi)) | |
const factory = new ContractFactory(abi, bytecode, wallet) | |
const wei = Utils.parseEther("0.1") | |
// const gasEstimate = web3.eth.estimateGas({data: bytecode}); | |
//needs to be deployed as the owner (private key for wallet) | |
const deployedContract = await factory.deploy(wei) | |
console.log( | |
"contract transaction hash" + deployedContract.deployTransaction.hash | |
) | |
// The contract is NOT deployed yet; we must wait until it is mined | |
await deployedContract.deployed() | |
console.log("contract fully deployed") | |
console.log("contract address" + deployedContract.address) | |
return { | |
address: deployedContract.address, | |
abi | |
} | |
} | |
export const web3UseContract = async (address, abi) => { | |
const wei = Utils.parseEther("0.1") | |
const url = "http://localhost:8545" | |
myContractInstance.depositFunds( | |
{ from: web3.eth.accounts[0], gas: 3000000, value: 100 }, | |
function(err, res) {} | |
) | |
} | |
export const useContract = async (address, abi) => { | |
const wei = Utils.parseEther("0.1") | |
const url = "http://localhost:8545" | |
const provider = new Providers.JsonRpcProvider(url) | |
// Load the wallet to deploy the contract with | |
const wallet = new Wallet(privateKey, provider) | |
var contract = new Contract( | |
address, | |
abi, | |
wallet | |
) | |
const price = await contract.retrievePrice() | |
console.log("price " + price) //logs the price set in the constructor when the contract was made (WORKS) | |
const testAddress = await contract.isUser( | |
"0xabcabc" | |
) | |
console.log("testAddress ", testAddress) //checks if the given address is a user on the contract (WORKS) | |
const gasPrice = await provider.getGasPrice() | |
console.log("gas price: ", gasPrice.toString()) //returns the price of gas from the network (WORKS) | |
try { | |
console.log("about to send transaction") | |
const addToUsers = await contract.requestAccess({ | |
//call function to request access, from the current wallet (REVERTS) | |
value: wei | |
}) | |
console.log("result of sending transaction ", addToUsers) | |
} catch (error) { | |
console.log("error.... ", error) //fires as the contract reverted the payment | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment