Last active
April 24, 2023 10:04
-
-
Save AaronAbuUsama/33ccffb5d026d73f331adae2d160cada to your computer and use it in GitHub Desktop.
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 { | |
| ContractVotingSettings, | |
| ITokenVotingPluginInstall, | |
| votingSettingsToContract, | |
| } from "@aragon/sdk-client"; | |
| import { SupportedNetworks } from "../types"; | |
| import { BigNumber } from "ethers"; | |
| import { defaultAbiCoder } from "ethers/lib/utils"; | |
| import { activeContractsList } from "@aragon/osx-ethers"; | |
| export const encodeTokenVotingPlugin = ( | |
| pluginInitParams: ITokenVotingPluginInstall & { | |
| network: SupportedNetworks; | |
| } | |
| ) => { | |
| const { network, ...rest } = pluginInitParams; | |
| if (!Object.keys(CHAINS).includes(network)) { | |
| console.error(`Unsupported network ID: ${network}`); | |
| } | |
| const args = tokenVotingInitParamsToContract(rest); | |
| const hexBytes = defaultAbiCoder.encode( | |
| [ | |
| "tuple(uint8 votingMode, uint64 supportThreshold, uint64 minParticipation, uint64 minDuration, uint256 minProposerVotingPower) votingSettings", | |
| "tuple(address addr, string name, string symbol) tokenSettings", | |
| "tuple(address[] receivers, uint256[] amounts) mintSettings", | |
| ], | |
| args | |
| ); | |
| return { | |
| id: activeContractsList[network]["token-voting-repo"], | |
| data: hexToBytes(hexBytes), | |
| }; | |
| }; | |
| function tokenVotingInitParamsToContract( | |
| params: ITokenVotingPluginInstall | |
| ): any { | |
| let token: [string, string, string] = ["", "", ""]; | |
| let balances: [string[], BigNumber[]] = [[], []]; | |
| if (params.newToken) { | |
| token = [AddressZero, params.newToken.name, params.newToken.symbol]; | |
| balances = [ | |
| params.newToken.balances.map((balance) => balance.address), | |
| params.newToken.balances.map(({ balance }) => BigNumber.from(balance)), | |
| ]; | |
| } else if (params.useToken) { | |
| token = [params.useToken?.address, "", ""]; | |
| } | |
| return [ | |
| Object.values( | |
| votingSettingsToContract(params.votingSettings) | |
| ) as ContractVotingSettings, | |
| token, | |
| balances, | |
| ]; | |
| } | |
| export function bytesToHex(buff: Uint8Array, skip0x?: boolean): string { | |
| const bytes: string[] = []; | |
| for (let i = 0; i < buff.length; i++) { | |
| if (buff[i] >= 16) bytes.push(buff[i].toString(16)); | |
| else bytes.push("0" + buff[i].toString(16)); | |
| } | |
| if (skip0x) return bytes.join(""); | |
| return "0x" + bytes.join(""); | |
| } | |
| export function hexToBytes(hexString: string): Uint8Array { | |
| if (!hexString) return new Uint8Array(); | |
| else if (!/^(0x)?[0-9a-fA-F]*$/.test(hexString)) { | |
| throw new Error("Invalid hex string"); | |
| } else if (hexString.length % 2 !== 0) { | |
| throw new Error("The hex string has an odd length"); | |
| } | |
| hexString = strip0x(hexString); | |
| const bytes = []; | |
| for (let i = 0; i < hexString.length; i += 2) { | |
| bytes.push(parseInt(hexString.substring(i, i + 2), 16)); | |
| } | |
| return Uint8Array.from(bytes); | |
| } | |
| export function strip0x(value: string): string { | |
| return value.startsWith("0x") ? value.substring(2) : value; | |
| } | |
| export const CHAINS = { | |
| mainnet: 1, | |
| goerli: 5, | |
| polygon: 137, | |
| mumbai: 80001, | |
| } as const; | |
| export const AddressZero = "0x" + "0".repeat(40); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment