├── .gitignore ├── Readme.md ├── constants.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Web3 Functions 2 | 3 | # Install 4 | `npm i web3-functions`. 5 | 6 | # Get the balance of Wallet 7 | 8 | const getBalance = async (address) => { 9 | const res = await web3.eth.getBalance(address); 10 | return res; 11 | } 12 | 13 | # For checking Web3 Provider is available or not 14 | 15 | const isConnected = async () => { 16 | if (!web3) return false; 17 | return !!await web3.eth.getNodeInfo(); 18 | } 19 | 20 | # Set Web3 Provider 21 | 22 | const setProvider = async () => { 23 | const prov = new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || ''); 24 | if (!web3) { 25 | web3 = new Web3(prov); 26 | } else { 27 | web3.setProvider(prov); 28 | } 29 | } 30 | 31 | # Get KeyStore file using Private Key 32 | 33 | const getKeystore = (privateKey, password) => { 34 | if (!web3) throw new Error('not inialized'); 35 | 36 | return web3.eth.accounts.encrypt(privateKey, password); 37 | 38 | }; 39 | 40 | # Get info on delegator 41 | 42 | const getDelegate = (from, delegateAddress, sfc) => { 43 | return new Promise(resolve => { 44 | sfc.methods.delegations(delegateAddress).call({ from }, function (error, result) { 45 | if (!error) resolve(result); 46 | console.log(error, 'errorerror getDelegate'); 47 | }); 48 | }); 49 | } 50 | 51 | # Validate KeyStore 52 | 53 | const validateKeystore = (keystore, password) => { 54 | if (!web3) throw new Error('not inialized'); 55 | 56 | return web3.eth.accounts.decrypt(keystore, password); 57 | 58 | }; 59 | 60 | # Get Private Key from KeyStore and Password 61 | 62 | const getPrivateKey = (keystore, password) => 63 | new Promise(resolve => 64 | keythereum.recover(password, keystore, dataRes => { 65 | resolve(dataRes instanceof Buffer ? EthUtil.bufferToHex(dataRes) : null); 66 | }) 67 | ); 68 | 69 | # Get estimationfee for transactions and Staking (in Web) 70 | 71 | const estimateFee = async ({ from, to, value, memo }) => { 72 | const gasPrice = await web3.eth.getGasPrice(); 73 | const gasLimit = await web3.eth.estimateGas({ 74 | from, 75 | to, 76 | value: Web3.utils.toHex(Web3.utils.toWei(value, 'ether')), 77 | data: Web3.utils.asciiToHex(memo) 78 | }); 79 | const fee = Web3.utils.fromWei( 80 | BigInt(gasPrice.toString()) 81 | .multiply(BigInt(gasLimit.toString())) 82 | .toString() 83 | ); 84 | return fee; 85 | } 86 | 87 | # Get delegation pending rewards after staking 88 | 89 | const getDelegationPendingRewards = async (from, delegateAddress) => { 90 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 91 | const sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 92 | const info = await Promise.all([ 93 | getCurrentEpoch(from, sfc), 94 | getDelegate(from, delegateAddress, sfc) || {} 95 | ]); 96 | const maxEpochs = Number(info[0]) - 1; 97 | const fromEpoch = info[1].paidUntilEpoch; 98 | return new Promise(resolve => { 99 | sfc.methods 100 | .calcDelegationRewards(delegateAddress, fromEpoch, maxEpochs) 101 | .call({ from }, function (error, result) { 102 | if (result) { 103 | resolve({ 104 | pendingRewards: parseFloat(result['0']) / Math.pow(10, 18), 105 | data: info[1] 106 | }); 107 | } else { 108 | resolve({ pendingRewards: 0, data: info[1] }); 109 | } 110 | }); 111 | }); 112 | } 113 | 114 | # Get epoch for unstake FTM 115 | 116 | const getCurrentEpoch = (from, sfc) => { 117 | return new Promise(resolve => { 118 | sfc.methods.currentEpoch().call({ from }, function (error, result) { 119 | if (!error) { 120 | resolve(result); 121 | } 122 | console.log(error, 'errorerror getCurrentEpoch'); 123 | }); 124 | }); 125 | } 126 | 127 | # Restore Wallet using Private key 128 | 129 | const restoreWallet = async (privateKey) => { 130 | const wallet = web3.eth.accounts.privateKeyToAccount(privateKey); 131 | return wallet; 132 | } 133 | 134 | # Get TransactionFee (Mobile) 135 | 136 | const getTransactionFee = async (gasLimit) => { 137 | const gasPrice = await web3.eth.getGasPrice(); 138 | // const gasLimit = 200000; 139 | const fee = Web3.utils.fromWei( 140 | BigInt(gasPrice.toString()) 141 | .multiply(BigInt(gasLimit.toString())) 142 | .toString() 143 | ); 144 | return fee; 145 | } 146 | 147 | # Delegate stake 148 | 149 | const delegateStake = ({ amount, publicKey, privateKey, validatorId, isWeb = false }) => { 150 | console.log(amount, publicKey, privateKey, validatorId, '**\*\***8amount, publicKey, privateKey, validatorId'); 151 | 152 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 153 | 154 | const web3Sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 155 | return transfer({ 156 | from: publicKey, 157 | to: '0xfc00face00000000000000000000000000000000', 158 | value: amount, 159 | memo: web3Sfc.methods.createDelegation(validatorId).encodeABI(), 160 | privateKey, 161 | gasLimit: 200000, 162 | web3Delegate: web3, 163 | isWeb 164 | }); 165 | 166 | } 167 | 168 | # Unstake your staked amount 169 | 170 | const delegateUnstake = async (publicKey, privateKey) => { 171 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 172 | const web3Sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 173 | return transfer({ 174 | from: publicKey, 175 | to: '0xfc00face00000000000000000000000000000000', 176 | value: '0', 177 | memo: web3Sfc.methods.prepareToWithdrawDelegation().encodeABI(), 178 | privateKey, 179 | gasLimit: 200000, 180 | web3Delegate: web3 181 | 182 | # Transfer FTM 183 | 184 | const transfer = async ({ 185 | from, 186 | to, 187 | value, 188 | memo = '', 189 | privateKey, 190 | gasLimit = 44000, 191 | web3Delegate = '' 192 | }) => { 193 | const useWeb3 = web3Delegate || web3; 194 | const nonce = await useWeb3.eth.getTransactionCount(from); 195 | const gasPrice = await useWeb3.eth.getGasPrice(); 196 | const rawTx = { 197 | from, 198 | to, 199 | value: Web3.utils.toHex(Web3.utils.toWei(value, 'ether')), 200 | gasLimit: Web3.utils.toHex(gasLimit), 201 | gasPrice: Web3.utils.toHex(gasPrice), 202 | nonce: Web3.utils.toHex(nonce), 203 | data: `0x${memo}` 204 | }; 205 | const privateKeyBuffer = EthUtil.toBuffer(privateKey); 206 | const tx = new Tx(rawTx); 207 | tx.sign(privateKeyBuffer); 208 | const serializedTx = tx.serialize(); 209 | const res = await useWeb3.eth.sendSignedTransaction(`0x${serializedTx.toString('hex')}`); 210 | return res; 211 | }}); 212 | } 213 | 214 | # Withdrawing FTM which you have unstaked 215 | 216 | const withdrawDelegateAmount = async (publicKey, privateKey) => { 217 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 218 | const web3Sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 219 | return transfer({ 220 | from: publicKey, 221 | to: '0xfc00face00000000000000000000000000000000', 222 | value: '0', 223 | memo: web3Sfc.methods.withdrawDelegation().encodeABI(), 224 | privateKey, 225 | gasLimit: 200000, 226 | web3Delegate: web3 227 | }); 228 | } 229 | 230 | # Get account information i.e transaction details 231 | 232 | const getAccount = async (address) => { 233 | return await fetch(`${REACT_APP_API_URL_FANTOM}api/v1/get-account?address=${address}`); 234 | } 235 | 236 | # Get Keys from Mnemonic 237 | 238 | const mnemonicToKeys = async (mnemonic) => { 239 | const seed = await Bip39.mnemonicToSeed(mnemonic); 240 | const root = Hdkey.fromMasterSeed(seed); 241 | 242 | const addrNode = root.derive("m/44'/60'/0'/0/0"); 243 | const pubKey = EthUtil.privateToPublic(addrNode._privateKey); 244 | const addr = EthUtil.publicToAddress(pubKey).toString('hex'); 245 | const publicAddress = EthUtil.toChecksumAddress(addr); 246 | const privateKey = EthUtil.bufferToHex(addrNode._privateKey); 247 | 248 | return { publicAddress, privateKey }; 249 | 250 | }; 251 | 252 | # Get Keys from Private Key 253 | 254 | const privateKeyToKeys = (privateKey) => { 255 | const privateKeyBuffer = EthUtil.toBuffer(privateKey); 256 | 257 | const pubKey = EthUtil.privateToPublic(privateKeyBuffer); 258 | const addr = EthUtil.publicToAddress(pubKey).toString('hex'); 259 | const publicAddress = EthUtil.toChecksumAddress(addr); 260 | 261 | return { publicAddress, privateKey }; 262 | 263 | }; 264 | -------------------------------------------------------------------------------- /constants.js: -------------------------------------------------------------------------------- 1 | module.exports.contractFunctions = JSON.parse( 2 | '[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"reward","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fromEpoch","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"untilEpoch","type":"uint256"}],"name":"ClaimedDelegationReward","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"reward","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fromEpoch","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"untilEpoch","type":"uint256"}],"name":"ClaimedValidatorReward","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"toStakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"CreatedDelegation","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":true,"internalType":"address","name":"stakerAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"CreatedStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"diff","type":"uint256"}],"name":"IncreasedStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"}],"name":"PreparedToWithdrawDelegation","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"}],"name":"PreparedToWithdrawStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"UpdatedBaseRewardPerSec","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"short","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"long","type":"uint256"}],"name":"UpdatedGasPowerAllocationRate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"penalty","type":"uint256"}],"name":"WithdrawnDelegation","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"penalty","type":"uint256"}],"name":"WithdrawnStake","type":"event"},{"constant":false,"inputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"name":"_updateBaseRewardPerSec","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"short","type":"uint256"},{"internalType":"uint256","name":"long","type":"uint256"}],"name":"_updateGasPowerAllocationRate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"epoch","type":"uint256"},{"internalType":"uint256","name":"delegatedAmount","type":"uint256"}],"name":"calcDelegationReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"delegator","type":"address"},{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"calcDelegationRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"calcTotalReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"calcValidatorReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"calcValidatorRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"claimDelegationRewards","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"claimValidatorRewards","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"contractCommission","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"to","type":"uint256"}],"name":"createDelegation","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"createStake","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"currentEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"currentSealedEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"delegationLockPeriodEpochs","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"delegationLockPeriodTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"delegations","outputs":[{"internalType":"uint256","name":"createdEpoch","type":"uint256"},{"internalType":"uint256","name":"createdTime","type":"uint256"},{"internalType":"uint256","name":"deactivatedEpoch","type":"uint256"},{"internalType":"uint256","name":"deactivatedTime","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"paidUntilEpoch","type":"uint256"},{"internalType":"uint256","name":"toStakerID","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"delegationsNum","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"delegationsTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"epochSnapshots","outputs":[{"internalType":"uint256","name":"endTime","type":"uint256"},{"internalType":"uint256","name":"duration","type":"uint256"},{"internalType":"uint256","name":"epochFee","type":"uint256"},{"internalType":"uint256","name":"totalBaseRewardWeight","type":"uint256"},{"internalType":"uint256","name":"totalTxRewardWeight","type":"uint256"},{"internalType":"uint256","name":"baseRewardPerSecond","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"e","type":"uint256"},{"internalType":"uint256","name":"v","type":"uint256"}],"name":"epochValidator","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"getStakerID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"increaseStake","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"maxDelegatedRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"minDelegation","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"minStake","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"minStakeIncrease","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[],"name":"prepareToWithdrawDelegation","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"prepareToWithdrawStake","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"stakeLockPeriodEpochs","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"stakeLockPeriodTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"stakeTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"stakers","outputs":[{"internalType":"uint256","name":"status","type":"uint256"},{"internalType":"uint256","name":"createdEpoch","type":"uint256"},{"internalType":"uint256","name":"createdTime","type":"uint256"},{"internalType":"uint256","name":"deactivatedEpoch","type":"uint256"},{"internalType":"uint256","name":"deactivatedTime","type":"uint256"},{"internalType":"uint256","name":"stakeAmount","type":"uint256"},{"internalType":"uint256","name":"paidUntilEpoch","type":"uint256"},{"internalType":"uint256","name":"delegatedMe","type":"uint256"},{"internalType":"address","name":"stakerAddress","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"stakersLastID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"stakersNum","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"validatorCommission","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[],"name":"withdrawDelegation","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"withdrawStake","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]' 3 | ); 4 | 5 | module.exports.abi = JSON.parse( 6 | '[{"constant":true,"inputs":[],"name":"minDelegation","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"bondedRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"stakersNum","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"slashedStakeTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"short","type":"uint256"},{"internalType":"uint256","name":"long","type":"uint256"}],"name":"updateGasPowerAllocationRate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"withdrawDelegation","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"name":"updateBaseRewardPerSec","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"prepareToWithdrawDelegation","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"delegationLockPeriodEpochs","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"epochSnapshots","outputs":[{"internalType":"uint256","name":"endTime","type":"uint256"},{"internalType":"uint256","name":"duration","type":"uint256"},{"internalType":"uint256","name":"epochFee","type":"uint256"},{"internalType":"uint256","name":"totalBaseRewardWeight","type":"uint256"},{"internalType":"uint256","name":"totalTxRewardWeight","type":"uint256"},{"internalType":"uint256","name":"baseRewardPerSecond","type":"uint256"},{"internalType":"uint256","name":"stakeTotalAmount","type":"uint256"},{"internalType":"uint256","name":"delegationsTotalAmount","type":"uint256"},{"internalType":"uint256","name":"totalSupply","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"maxDelegatedRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"contractCommission","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"delegationsTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes","name":"metadata","type":"bytes"}],"name":"updateStakerMetadata","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"calcTotalReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"minStake","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[],"name":"updateCapReachedDate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"unbondingUnlockPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"stakeTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"capReachedDate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"stakeLockPeriodTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"delegationsNum","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"unbondingStartDate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"stakeLockPeriodEpochs","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"getStakerID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"bondedTargetRewardUnlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"unbondingPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"currentEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"currentSealedEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"stakersLastID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"rewardsAllowed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"isOwner","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"calcValidatorRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"stakerMetadata","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"slashedDelegationsTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"validatorCommission","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[],"name":"maxStakerMetadataSize","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"delegator","type":"address"},{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"calcDelegationRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"e","type":"uint256"},{"internalType":"uint256","name":"v","type":"uint256"}],"name":"epochValidator","outputs":[{"internalType":"uint256","name":"stakeAmount","type":"uint256"},{"internalType":"uint256","name":"delegatedMe","type":"uint256"},{"internalType":"uint256","name":"baseRewardWeight","type":"uint256"},{"internalType":"uint256","name":"txRewardWeight","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"withdrawStake","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"delegations","outputs":[{"internalType":"uint256","name":"createdEpoch","type":"uint256"},{"internalType":"uint256","name":"createdTime","type":"uint256"},{"internalType":"uint256","name":"deactivatedEpoch","type":"uint256"},{"internalType":"uint256","name":"deactivatedTime","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"paidUntilEpoch","type":"uint256"},{"internalType":"uint256","name":"toStakerID","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"to","type":"uint256"}],"name":"createDelegation","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"prepareToWithdrawStake","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"minStakeIncrease","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes","name":"metadata","type":"bytes"}],"name":"createStake","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"increaseStake","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"epoch","type":"uint256"},{"internalType":"uint256","name":"delegatedAmount","type":"uint256"}],"name":"calcDelegationReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"delegationLockPeriodTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"claimValidatorRewards","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"stakerID","type":"uint256"},{"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"calcValidatorReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"_fromEpoch","type":"uint256"},{"internalType":"uint256","name":"maxEpochs","type":"uint256"}],"name":"claimDelegationRewards","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"stakers","outputs":[{"internalType":"uint256","name":"status","type":"uint256"},{"internalType":"uint256","name":"createdEpoch","type":"uint256"},{"internalType":"uint256","name":"createdTime","type":"uint256"},{"internalType":"uint256","name":"deactivatedEpoch","type":"uint256"},{"internalType":"uint256","name":"deactivatedTime","type":"uint256"},{"internalType":"uint256","name":"stakeAmount","type":"uint256"},{"internalType":"uint256","name":"paidUntilEpoch","type":"uint256"},{"internalType":"uint256","name":"delegatedMe","type":"uint256"},{"internalType":"address","name":"stakerAddress","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":true,"internalType":"address","name":"stakerAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"CreatedStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"}],"name":"UpdatedStakerMetadata","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"diff","type":"uint256"}],"name":"IncreasedStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"toStakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"CreatedDelegation","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"reward","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fromEpoch","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"untilEpoch","type":"uint256"}],"name":"ClaimedDelegationReward","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"reward","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fromEpoch","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"untilEpoch","type":"uint256"}],"name":"ClaimedValidatorReward","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"}],"name":"PreparedToWithdrawStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"penalty","type":"uint256"}],"name":"WithdrawnStake","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"}],"name":"PreparedToWithdrawDelegation","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"stakerID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"penalty","type":"uint256"}],"name":"WithdrawnDelegation","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_capReachedDate","type":"uint256"}],"name":"ChangedCapReachedDate","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"UpdatedBaseRewardPerSec","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"short","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"long","type":"uint256"}],"name":"UpdatedGasPowerAllocationRate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"}]' 7 | ); 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-underscore-dangle */ 2 | /* eslint-disable @typescript-eslint/no-explicit-any */ 3 | const Web3 = require('web3'); 4 | const Tx = require('ethereumjs-tx'); 5 | const EthUtil = require('ethereumjs-util'); 6 | const Bip39 = require('bip39'); 7 | const Hdkey = require('hdkey'); 8 | const keythereum = require('keythereum'); 9 | const BigInt = require('big-integer'); 10 | const { contractFunctions } = require('./constants'); 11 | 12 | const REACT_APP_API_URL_WEB3 = 'https://rpc.fantom.network/' 13 | const REACT_APP_API_URL_FANTOM = 'https://api.fantom.network/api/v1/' 14 | 15 | let web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 16 | 17 | const getBalance = async (address) => { 18 | const res = await web3.eth.getBalance(address); 19 | return res; 20 | } 21 | 22 | const isConnected = async () => { 23 | if (!web3) return false; 24 | return !!await web3.eth.getNodeInfo(); 25 | } 26 | 27 | const setProvider = async () => { 28 | const prov = new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || ''); 29 | if (!web3) { 30 | web3 = new Web3(prov); 31 | } else { 32 | web3.setProvider(prov); 33 | } 34 | } 35 | 36 | const getKeystore = (privateKey, password) => { 37 | if (!web3) throw new Error('not inialized'); 38 | 39 | return web3.eth.accounts.encrypt(privateKey, password); 40 | }; 41 | 42 | const getDelegate = (from, delegateAddress, sfc) => { 43 | return new Promise(resolve => { 44 | sfc.methods.delegations(delegateAddress).call({ from }, function (error, result) { 45 | if (!error) resolve(result); 46 | console.log(error, 'errorerror getDelegate'); 47 | }); 48 | }); 49 | } 50 | 51 | const validateKeystore = (keystore, password) => { 52 | if (!web3) throw new Error('not inialized'); 53 | 54 | return web3.eth.accounts.decrypt(keystore, password); 55 | }; 56 | 57 | const getPrivateKey = (keystore, password) => 58 | new Promise(resolve => 59 | keythereum.recover(password, keystore, dataRes => { 60 | resolve(dataRes instanceof Buffer ? EthUtil.bufferToHex(dataRes) : null); 61 | }) 62 | ); 63 | 64 | const getCurrentEpoch = (from, sfc) => { 65 | return new Promise(resolve => { 66 | sfc.methods.currentEpoch().call({ from }, function (error, result) { 67 | if (!error) { 68 | resolve(result); 69 | } 70 | console.log(error, 'errorerror getCurrentEpoch'); 71 | }); 72 | }); 73 | } 74 | 75 | / 76 | 77 | const estimateFee = async ({ from, to, value, memo }) => { 78 | 79 | 80 | const gasPrice = await web3.eth.getGasPrice(); 81 | const gasLimit = await web3.eth.estimateGas({ 82 | from, 83 | to, 84 | value: Web3.utils.toHex(Web3.utils.toWei(value, 'ether')), 85 | data: Web3.utils.asciiToHex(memo) 86 | }); 87 | 88 | const fee = Web3.utils.fromWei( 89 | BigInt(gasPrice.toString()) 90 | .multiply(BigInt(gasLimit.toString())) 91 | .toString() 92 | ); 93 | 94 | return fee; 95 | } 96 | 97 | const getDelegationPendingRewards = async (from, delegateAddress) => { 98 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 99 | const sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 100 | const info = await Promise.all([ 101 | getCurrentEpoch(from, sfc), 102 | getDelegate(from, delegateAddress, sfc) || {} 103 | ]); 104 | const maxEpochs = Number(info[0]) - 1; 105 | const fromEpoch = info[1].paidUntilEpoch; 106 | return new Promise(resolve => { 107 | sfc.methods 108 | .calcDelegationRewards(delegateAddress, fromEpoch, maxEpochs) 109 | .call({ from }, function (error, result) { 110 | if (result) { 111 | resolve({ 112 | pendingRewards: parseFloat(result['0']) / Math.pow(10, 18), 113 | data: info[1] 114 | }); 115 | } else { 116 | resolve({ pendingRewards: 0, data: info[1] }); 117 | } 118 | }); 119 | }); 120 | } 121 | 122 | const delegateStake = ({ amount, publicKey, privateKey, validatorId, isWeb = false }) => { 123 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 124 | const web3Sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 125 | return transfer({ 126 | from: publicKey, 127 | to: '0xfc00face00000000000000000000000000000000', 128 | value: amount, 129 | memo: web3Sfc.methods.createDelegation(validatorId).encodeABI(), 130 | privateKey, 131 | gasLimit: 200000, 132 | web3Delegate: web3, 133 | isWeb 134 | }); 135 | } 136 | 137 | const restoreWallet = async (privateKey) => { 138 | const wallet = web3.eth.accounts.privateKeyToAccount(privateKey); 139 | return wallet; 140 | } 141 | 142 | const getTransactionFee = async (gasLimit) => { 143 | const gasPrice = await web3.eth.getGasPrice(); 144 | const fee = Web3.utils.fromWei( 145 | BigInt(gasPrice.toString()) 146 | .multiply(BigInt(gasLimit.toString())) 147 | .toString() 148 | ); 149 | return fee; 150 | } 151 | 152 | const transfer = async ({ 153 | from, 154 | to, 155 | value, 156 | memo = '', 157 | privateKey, 158 | gasLimit = 44000, 159 | web3Delegate = '', 160 | isWeb 161 | }) => { 162 | const useWeb3 = web3Delegate || web3; 163 | const nonce = await useWeb3.eth.getTransactionCount(from); 164 | const gasPrice = await useWeb3.eth.getGasPrice(); 165 | 166 | const rawTx = { 167 | from, 168 | to, 169 | value: Web3.utils.toHex(Web3.utils.toWei(value, 'ether')), 170 | gasLimit: Web3.utils.toHex(gasLimit), 171 | gasPrice: Web3.utils.toHex(gasPrice), 172 | nonce: Web3.utils.toHex(nonce), 173 | data: memo 174 | }; 175 | 176 | 177 | 178 | const bufferData = EthUtil.addHexPrefix(privateKey) 179 | const privateKeyBuffer = EthUtil.toBuffer(privateKey); 180 | const tx = new Tx(rawTx); 181 | tx.sign(privateKeyBuffer); 182 | const serializedTx = tx.serialize(); 183 | const res = await useWeb3.eth.sendSignedTransaction(`0x${serializedTx.toString('hex')}`); 184 | if (isWeb) { 185 | localStorage.setItem('txHash', res.transactionHash); 186 | } 187 | 188 | return res; 189 | } 190 | 191 | const delegateUnstake = async (publicKey, privateKey, isWeb = false) => { 192 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 193 | const web3Sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 194 | return transfer({ 195 | from: publicKey, 196 | to: '0xfc00face00000000000000000000000000000000', 197 | value: '0', 198 | memo: web3Sfc.methods.prepareToWithdrawDelegation().encodeABI(), 199 | privateKey, 200 | gasLimit: 200000, 201 | web3Delegate: web3, 202 | isWeb 203 | }); 204 | } 205 | 206 | const withdrawDelegateAmount = async (publicKey, privateKey, isWeb = false) => { 207 | const web3 = new Web3(new Web3.providers.HttpProvider(REACT_APP_API_URL_WEB3 || '')); 208 | const web3Sfc = new web3.eth.Contract(contractFunctions, '0xfc00face00000000000000000000000000000000'); 209 | return transfer({ 210 | from: publicKey, 211 | to: '0xfc00face00000000000000000000000000000000', 212 | value: '0', 213 | memo: web3Sfc.methods.withdrawDelegation().encodeABI(), 214 | privateKey, 215 | gasLimit: 200000, 216 | web3Delegate: web3, 217 | isWeb, 218 | }); 219 | } 220 | 221 | const mnemonicToKeys = async (mnemonic) => { 222 | const seed = await Bip39.mnemonicToSeed(mnemonic); 223 | const root = Hdkey.fromMasterSeed(seed); 224 | const addrNode = root.derive("m/44'/60'/0'/0/0"); 225 | const pubKey = EthUtil.privateToPublic(addrNode._privateKey); 226 | const addr = EthUtil.publicToAddress(pubKey).toString('hex'); 227 | const publicAddress = EthUtil.toChecksumAddress(addr); 228 | const privateKey = EthUtil.bufferToHex(addrNode._privateKey); 229 | return { publicAddress, privateKey }; 230 | }; 231 | 232 | const privateKeyToKeys = (privateKey) => { 233 | const privateKeyBuffer = EthUtil.toBuffer(privateKey); 234 | const pubKey = EthUtil.privateToPublic(privateKeyBuffer); 235 | const addr = EthUtil.publicToAddress(pubKey).toString('hex'); 236 | const publicAddress = EthUtil.toChecksumAddress(addr); 237 | return { publicAddress, privateKey }; 238 | }; 239 | 240 | const getAccount = async (address) => { 241 | // eslint-disable-next-line no-return-await 242 | return await fetch(`${REACT_APP_API_URL_FANTOM}api/v1/get-account?address=${address}`); 243 | } 244 | 245 | const estimateFeeMobile = async (value) => { 246 | let fee; 247 | if (web3 && web3.eth) { 248 | const gasPrice = await web3.eth.getGasPrice(); 249 | const gasLimit = value; 250 | fee = Web3.utils.fromWei( 251 | BigInt(gasPrice.toString()) 252 | .multiply(BigInt(gasLimit.toString())) 253 | .toString() 254 | ); 255 | } 256 | return fee; 257 | } 258 | 259 | module.exports.estimateFeeMobile = estimateFeeMobile 260 | module.exports.getBalance = getBalance 261 | module.exports.getKeystore = getKeystore 262 | module.exports.privateKeyToKeys = privateKeyToKeys 263 | module.exports.delegateUnstake = delegateUnstake 264 | module.exports.isConnected = isConnected 265 | module.exports.setProvider = setProvider 266 | module.exports.getDelegate = getDelegate 267 | module.exports.validateKeystore = validateKeystore 268 | module.exports.getPrivateKey = getPrivateKey 269 | module.exports.getCurrentEpoch = getCurrentEpoch 270 | module.exports.estimateFee = estimateFee 271 | module.exports.getDelegationPendingRewards = getDelegationPendingRewards 272 | module.exports.delegateStake = delegateStake 273 | module.exports.restoreWallet = restoreWallet 274 | module.exports.getTransactionFee = getTransactionFee 275 | module.exports.transfer = transfer 276 | module.exports.delegateUnstake = delegateUnstake 277 | module.exports.withdrawDelegateAmount = withdrawDelegateAmount 278 | module.exports.mnemonicToKeys = mnemonicToKeys 279 | module.exports.getAccount = getAccount 280 | 281 | 282 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web3-functions", 3 | "version": "1.1.1", 4 | "description": "contains Fantom web3 functions", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "test" 8 | }, 9 | "type": "module", 10 | "repository": { 11 | "type": "git", 12 | "url": "null" 13 | }, 14 | "keywords": [ 15 | "web3" 16 | ], 17 | "author": "sunil", 18 | "license": "ISC", 19 | "dependencies": { 20 | "big-integer": "^1.6.48", 21 | "bip39": "^3.0.2", 22 | "dotenv": "^8.2.0", 23 | "ethereumjs-tx": "^1.3.7", 24 | "ethereumjs-util": "^6.0.0", 25 | "hdkey": "^1.1.1", 26 | "keythereum": "^1.0.4", 27 | "web3": "^1.2.2", 28 | "web3-core": "^1.2.2" 29 | } 30 | } 31 | --------------------------------------------------------------------------------