-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a4d23c
commit 51dacf5
Showing
7 changed files
with
183 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,90 @@ | ||
# governance | ||
# Collective Governance TypeScript API | ||
|
||
### Installation | ||
|
||
### npm | ||
|
||
``` | ||
$ npm install @momentranks/governance | ||
``` | ||
|
||
#### Yarn | ||
|
||
``` | ||
$ yarn add @momentranks/governance | ||
``` | ||
|
||
#### Usage | ||
|
||
Connect an existing Governance contract from the builder address and build transaction id | ||
|
||
``` | ||
import { | ||
Wallet, | ||
EthWallet, | ||
Storage, | ||
Governance, | ||
Meta, | ||
GovernanceBuilder, | ||
CollectiveGovernance, | ||
MetaStorage, | ||
CollectiveStorage, | ||
} from '@momentranks/governance'; | ||
import Web3 from 'web3'; | ||
export interface Collective { | ||
governance: Governance; | ||
storage: Storage; | ||
meta: Meta; | ||
web3: Web3; | ||
wallet: Wallet; | ||
} | ||
export async function connect(): Promise<Collective> { | ||
try { | ||
const rpcUrl = 'wss://localhost:8545'; | ||
const privateKey = 'XXXXXXXXXXXX'; | ||
const abiPath = 'node_modules/@momentranks/governance/abi'; | ||
const builderAddress = '0xd64f3Db037B263D54561a2cc9885Db370B51E354'; | ||
const maximumGas = 10000000; | ||
const buildTransaction = '0x0f7f3e13055547b8b6ac5b28285abc960266c6297094ab451ca9de318cbf5906'; | ||
const web3 = new Web3(rpcUrl); | ||
const wallet = new EthWallet(privateKey, web3); | ||
wallet.connect(); | ||
const builder = new GovernanceBuilder(abiPath, builderAddress, web3, wallet, maximumGas); | ||
const contractAddress = await builder.discoverContractAddress(buildTransaction); | ||
const governance = new CollectiveGovernance(abiPath, contractAddress.governanceAddress, web3, wallet, maximumGas); | ||
const name = await governance.name(); | ||
const version = await governance.version(); | ||
const metaAddress = contractAddress.metaAddress; | ||
const meta = new MetaStorage(abiPath, metaAddress, web3); | ||
const metaName = await meta.name(); | ||
const metaVersion = await meta.version(); | ||
if (version !== metaVersion) { | ||
throw new Error('MetaStorage version mismatch'); | ||
} | ||
const community = await meta.community(); | ||
console.log(`Community: ${community}`); | ||
const communityUrl = await meta.url(); | ||
console.log(`Community Url: ${communityUrl}`); | ||
const description = await meta.description(); | ||
console.log(`Description: ${description}`); | ||
const storageAddress = contractAddress.storageAddress; | ||
const storage = new CollectiveStorage(abiPath, storageAddress, web3); | ||
const storageVersion = await storage.version(); | ||
if (version != storageVersion) { | ||
throw new Error('Storage version mismatch'); | ||
} | ||
return { governance, storage, meta, web3, wallet }; | ||
} catch (error) { | ||
throw new Error('Run failed'); | ||
} | ||
} | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { parseIntOrThrow } from '../version'; | ||
|
||
describe('parseIntOrThrow', () => { | ||
it('must parse a proper number', () => { | ||
expect(parseIntOrThrow('110571')).toBe(110571); | ||
}); | ||
it('must throw if not a number', () => { | ||
expect(() => parseIntOrThrow('')).toThrow(); | ||
}); | ||
}); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
/* | ||
* BSD 3-Clause License | ||
* | ||
* Copyright (c) 2022, collective | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
export function parseIntOrThrow(num: string): number { | ||
const n = parseInt(num); | ||
if (n) { | ||
return n; | ||
} | ||
throw new Error(`${num} is not a number`); | ||
} |
This file contains 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