Skip to content

Commit

Permalink
Fund pool
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl committed Feb 22, 2022
1 parent 916d8f1 commit 47867d7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
30 changes: 4 additions & 26 deletions src/fundingPools/createFundingPool.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
const fundingPools = require('./fundingPools.json')
const web3Utils = require('web3-utils')
const Contract = require('web3-eth-contract')
const abi = require('./abi.json')
const Web3Eth = require('web3-eth')
//0x013b75E935Ab46e38A4e39C44269186C7c520deb

const Web3 = require('web3')

function isValidAddress (_string) {
return web3Utils.isAddress(_string)
}
const { initContract, isValidAddress } = require('./utils')

const createFundingPool = async (_name, _validator, _chain, _token, _privateKey = null) => {
if (!isValidAddress(_validator)) {
throw new Error('Invalid Validator Address')
}
try {
const endpoint = fundingPools[_chain].RPC
const web3 = new Web3(endpoint)
const contractAddr = fundingPools[_chain].tokens[_token]
const accounts = await web3.eth.getAccounts()
console.log(accounts)
if (accounts.length < 1 && _privateKey == null) {
throw new Error('Must have an Eth account')
} else if (_privateKey != null) {
web3.eth.accounts.wallet.add(_privateKey)

if (!isValidAddress(_validator)) {
throw new Error(`${_validator} is not a valid address`)
}

//console.log(web3.eth.accounts.wallet.create(1))
//console.log(web3.eth.accounts.wallet)
const contract = new web3.eth.Contract(abi.abi, contractAddr)
const contract = await initContract(_chain, _token, _privateKey)
const gasPrice = await web3.eth.getGasPrice()
const gasEstimate = await contract.methods.createMoat(_name, _validator).estimateGas({gasPrice: gasPrice})
const response = await contract.methods.createMoat(_name, _validator).send({
Expand Down
25 changes: 23 additions & 2 deletions src/fundingPools/fundPool.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const fundingPools = require('./fundingPools.json')
const abi = require('./abi.json')
const Web3 = require('web3')
const { initContract } = require('./utils')

const fundPool = async (_name, _chain, _token, _amt, _privateKey = null) => {
try {
const contract = await initContract(_chain, _token, _privateKey)

const fundPool = async (_name, _chain, _token, _privateKey = null) => {
const gasPrice = await web3.eth.getGasPrice()
const gasEstimate = await contract.methods.fundMoat(_name, _amt).estimateGas({gasPrice: gasPrice})
const response = await contract.methods.fundMoat(_name, _amt).send({
gasPrice: gasPrice,
gas: gasEstimate * 1.2,
from: _addr
})

}
return response

} catch(e) {
console.log(e)
return 'failure'
}
}

module.exports = {fundPool}
25 changes: 25 additions & 0 deletions src/fundingPools/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Web3 = require('web3')
const fundingPools = require('./fundingPools.json')
const abi = require('./abi.json')

const initContract = async (_chain, _token, _privateKey = null) => {
const endpoint = fundingPools[_chain].RPC
const web3 = new Web3(endpoint)
const contractAddr = fundingPools[_chain].tokens[_token]
const accounts = await web3.eth.getAccounts()

if (accounts.length < 1 && _privateKey == null) {
throw new Error('Must have an Eth account')
} else if (_privateKey != null) {
web3.eth.accounts.wallet.add(_privateKey)
}
const contract = new web3.eth.Contract(abi.abi, contractAddr)

return contract
}

const isValidAddress = (_addr) => {
return Web3.utils.isAddress(_addr)
}

module.exports = {initContract, isValidAddress}
1 change: 0 additions & 1 deletion t.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const KwilDB = require('./index.js')
const fs = require('fs')
const {createFundingPool} = require('./src/fundingPools/createFundingPool.js')


let devKey
let photo
if (fs.existsSync('./devKey.js')) {
Expand Down

0 comments on commit 47867d7

Please sign in to comment.