|
| 1 | +const axios = require('axios') |
| 2 | +axios.defaults.timeout = 30000 |
| 3 | + |
| 4 | +const Web3 = require('web3') |
| 5 | +const web3 = new Web3() |
| 6 | +const BN = require('bn.js') |
| 7 | + |
| 8 | +async function getLegacyDAOParameters (daoId, subgraphEndpoint) { |
| 9 | + let voteParasQuery = ` |
| 10 | + boostedVotePeriodLimit |
| 11 | + daoBountyConst |
| 12 | + minimumDaoBounty |
| 13 | + queuedVotePeriodLimit |
| 14 | + queuedVoteRequiredPercentage |
| 15 | + preBoostedVotePeriodLimit |
| 16 | + proposingRepReward |
| 17 | + quietEndingPeriod |
| 18 | + thresholdConst |
| 19 | + voteOnBehalf |
| 20 | + votersReputationLossRatio |
| 21 | + activationTime |
| 22 | + ` |
| 23 | + const query = `{ |
| 24 | + dao(id: "${daoId}") { |
| 25 | + id |
| 26 | + name |
| 27 | + nativeToken { |
| 28 | + id |
| 29 | + name |
| 30 | + symbol |
| 31 | + } |
| 32 | + reputationHolders(first: 1000) { |
| 33 | + balance |
| 34 | + address |
| 35 | + } |
| 36 | + reputationHoldersCount |
| 37 | + schemes { |
| 38 | + isRegistered |
| 39 | + name |
| 40 | + alias |
| 41 | + canDelegateCall |
| 42 | + canRegisterSchemes |
| 43 | + canUpgradeController |
| 44 | + canManageGlobalConstraints |
| 45 | + genericSchemeParams { |
| 46 | + voteParams { |
| 47 | + ${voteParasQuery} |
| 48 | + } |
| 49 | + contractToCall |
| 50 | + } |
| 51 | + uGenericSchemeParams { |
| 52 | + voteParams { |
| 53 | + ${voteParasQuery} |
| 54 | + } |
| 55 | + contractToCall |
| 56 | + } |
| 57 | + schemeRegistrarParams { |
| 58 | + voteRegisterParams { |
| 59 | + ${voteParasQuery} |
| 60 | + } |
| 61 | + voteRemoveParams { |
| 62 | + ${voteParasQuery} |
| 63 | + } |
| 64 | + } |
| 65 | + contributionRewardParams { |
| 66 | + voteParams { |
| 67 | + ${voteParasQuery} |
| 68 | + } |
| 69 | + } |
| 70 | + contributionRewardExtParams { |
| 71 | + voteParams { |
| 72 | + ${voteParasQuery} |
| 73 | + } |
| 74 | + rewarder |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + }` |
| 79 | + try { |
| 80 | + let { data } = (await axios.post(subgraphEndpoint, { query })).data |
| 81 | + let { dao } = data |
| 82 | + let migrationParams = { |
| 83 | + founders: [], |
| 84 | + Schemes: [], |
| 85 | + VotingMachinesParams: [] |
| 86 | + } |
| 87 | + migrationParams.orgName = dao.name |
| 88 | + migrationParams.tokenName = dao.nativeToken.name |
| 89 | + migrationParams.tokenSymbol = dao.nativeToken.symbol |
| 90 | + // TODO: is tokenCap needed? |
| 91 | + migrationParams.tokenCap = 0 |
| 92 | + // TODO: Legacy DAOs had no metadata, we should maybe ask for user input for this |
| 93 | + migrationParams.metaData = '' |
| 94 | + if (dao.reputationHoldersCount > 1000) { |
| 95 | + // TODO: Might need to handle edge case of more than 1000 reputation holders, let's check if any such exist |
| 96 | + console.log('Error: Too many rep holders') |
| 97 | + } |
| 98 | + for (let reputationHolder of dao.reputationHolders) { |
| 99 | + // TODO: DO we need DAOToken too? |
| 100 | + migrationParams.founders.push({ |
| 101 | + tokens: 0, |
| 102 | + reputation: web3.utils.fromWei(reputationHolder.balance), |
| 103 | + address: reputationHolder.address |
| 104 | + }) |
| 105 | + } |
| 106 | + // TODO: Maybe handle more than 1000 token holders. |
| 107 | + const tokenHoldersQuery = `{ |
| 108 | + tokenHolders(where: {contract: "${dao.nativeToken.id}"}, first: 1000) { |
| 109 | + contract |
| 110 | + address |
| 111 | + balance |
| 112 | + } |
| 113 | + }` |
| 114 | + let { tokenHolders } = (await axios.post(subgraphEndpoint, { query: tokenHoldersQuery })).data.data |
| 115 | + for (let tokenHolder of tokenHolders) { |
| 116 | + let existingFounder = false |
| 117 | + for (let i in migrationParams.founders) { |
| 118 | + if (tokenHolder.address === migrationParams.founders[i].address) { |
| 119 | + existingFounder = true |
| 120 | + migrationParams.founders[i].tokens = web3.utils.fromWei(tokenHolder.balance) |
| 121 | + break |
| 122 | + } |
| 123 | + } |
| 124 | + if (!existingFounder) { |
| 125 | + migrationParams.founders.push({ |
| 126 | + tokens: web3.utils.fromWei(tokenHolder.balance), |
| 127 | + reputation: 0, |
| 128 | + address: tokenHolder.address |
| 129 | + }) |
| 130 | + } |
| 131 | + } |
| 132 | + let i = 0 |
| 133 | + for (let scheme of dao.schemes) { |
| 134 | + if (!scheme.isRegistered) { |
| 135 | + continue |
| 136 | + } |
| 137 | + let permissions = '0x00000000' |
| 138 | + if (scheme.canRegisterSchemes) { |
| 139 | + permissions = '0x0000001F' |
| 140 | + } else if (scheme.canDelegateCall) { |
| 141 | + permissions = '0x00000010' |
| 142 | + } else if (scheme.canUpgradeController) { |
| 143 | + permissions = '0x0000000A' |
| 144 | + } else if (scheme.canManageGlobalConstraints) { |
| 145 | + permissions = '0x00000004' |
| 146 | + } |
| 147 | + |
| 148 | + let schemeName = scheme.name |
| 149 | + let schemeParamsName = '' |
| 150 | + let params |
| 151 | + let voteParamsName = 'voteParams' |
| 152 | + if (scheme.genericSchemeParams !== null) { |
| 153 | + schemeParamsName = 'genericSchemeParams' |
| 154 | + params = [ |
| 155 | + 'GenesisProtocolAddress', |
| 156 | + { voteParams: i }, |
| 157 | + scheme[schemeParamsName].contractToCall |
| 158 | + ] |
| 159 | + } else if (scheme.uGenericSchemeParams !== null) { |
| 160 | + schemeName = 'GenericScheme' |
| 161 | + schemeParamsName = 'uGenericSchemeParams' |
| 162 | + params = [ |
| 163 | + 'GenesisProtocolAddress', |
| 164 | + { voteParams: i }, |
| 165 | + scheme[schemeParamsName].contractToCall |
| 166 | + ] |
| 167 | + } else if (scheme.schemeRegistrarParams !== null) { |
| 168 | + schemeName = 'SchemeFactory' |
| 169 | + schemeParamsName = 'schemeRegistrarParams' |
| 170 | + voteParamsName = 'voteRegisterParams' |
| 171 | + params = [ |
| 172 | + 'GenesisProtocolAddress', |
| 173 | + { voteParams: i }, |
| 174 | + { packageContract: 'DAOFactoryInstance' } |
| 175 | + ] |
| 176 | + } else if (scheme.contributionRewardParams !== null) { |
| 177 | + schemeParamsName = 'contributionRewardParams' |
| 178 | + params = [ |
| 179 | + 'GenesisProtocolAddress', |
| 180 | + { voteParams: i } |
| 181 | + ] |
| 182 | + } else if (scheme.contributionRewardExtParams !== null) { |
| 183 | + schemeParamsName = 'contributionRewardExtParams' |
| 184 | + params = [ |
| 185 | + 'GenesisProtocolAddress', |
| 186 | + { voteParams: i }, |
| 187 | + { packageContract: 'DAOFactoryInstance' }, |
| 188 | + 'PackageVersion', |
| 189 | + 'Competition' |
| 190 | + ] |
| 191 | + } |
| 192 | + |
| 193 | + if (schemeParamsName === '') { |
| 194 | + console.log('Cannot migrate this scheme ' + scheme.name) |
| 195 | + continue |
| 196 | + } |
| 197 | + i++ |
| 198 | + migrationParams.VotingMachinesParams.push({ |
| 199 | + boostedVotePeriodLimit: scheme[schemeParamsName][voteParamsName].boostedVotePeriodLimit, |
| 200 | + daoBountyConst: scheme[schemeParamsName][voteParamsName].daoBountyConst, |
| 201 | + minimumDaoBounty: web3.utils.fromWei(scheme[schemeParamsName][voteParamsName].minimumDaoBounty), |
| 202 | + queuedVotePeriodLimit: scheme[schemeParamsName][voteParamsName].queuedVotePeriodLimit, |
| 203 | + queuedVoteRequiredPercentage: scheme[schemeParamsName][voteParamsName].queuedVoteRequiredPercentage, |
| 204 | + preBoostedVotePeriodLimit: scheme[schemeParamsName][voteParamsName].preBoostedVotePeriodLimit, |
| 205 | + proposingRepReward: web3.utils.fromWei(scheme[schemeParamsName][voteParamsName].proposingRepReward), |
| 206 | + quietEndingPeriod: scheme[schemeParamsName][voteParamsName].quietEndingPeriod, |
| 207 | + thresholdConst: realMathToNumber(new BN(scheme[schemeParamsName][voteParamsName].thresholdConst)), |
| 208 | + voteOnBehalf: scheme[schemeParamsName][voteParamsName].voteOnBehalf, |
| 209 | + votersReputationLossRatio: scheme[schemeParamsName][voteParamsName].votersReputationLossRatio, |
| 210 | + activationTime: scheme[schemeParamsName][voteParamsName].activationTime |
| 211 | + }) |
| 212 | + migrationParams.Schemes.push({ |
| 213 | + name: schemeName, |
| 214 | + alias: scheme.alias, |
| 215 | + permissions, |
| 216 | + params |
| 217 | + }) |
| 218 | + } |
| 219 | + console.log(JSON.stringify(migrationParams, null, 2)) |
| 220 | + return migrationParams |
| 221 | + } catch (e) { |
| 222 | + console.log(e) |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +function realMathToNumber (t) { |
| 227 | + const REAL_FBITS = 40 |
| 228 | + const fraction = t.maskn(REAL_FBITS).toNumber() / Math.pow(2, REAL_FBITS) |
| 229 | + return Math.round((t.shrn(REAL_FBITS).toNumber() + fraction) * 1000) |
| 230 | +} |
| 231 | + |
| 232 | +if (require.main === module) { |
| 233 | + let cliArgs = process.argv.slice(2) |
| 234 | + let daoId = cliArgs[0] |
| 235 | + let subgraphEndpoint = cliArgs[1] |
| 236 | + getLegacyDAOParameters(daoId, subgraphEndpoint) |
| 237 | +} else { |
| 238 | + module.exports = { |
| 239 | + getLegacyDAOParameters |
| 240 | + } |
| 241 | +} |
0 commit comments