-
Notifications
You must be signed in to change notification settings - Fork 2
/
snapshot.ts
142 lines (120 loc) · 4.91 KB
/
snapshot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import CeramicClient from '@ceramicnetwork/http-client';
import { IDX } from '@ceramicstudio/idx'
import { Ed25519Provider } from 'key-did-provider-ed25519'
import * as dotenv from 'dotenv';
import { BigNumber } from "ethers";
import * as fromString from 'uint8arrays/from-string';
import { ConvictionState, Proposal, UserConviction } from './proposal';
import { GraphQLClient, gql } from 'graphql-request'
import { definitions } from './config.json'
interface AccountInfo {
id: string;
balance: number;
token: string;
did?: string;
}
// const CERAMIC_URL = 'http://localhost:7007';
const CERAMIC_URL = 'https://ceramic-clay.3boxlabs.com';
//TODO: Get DID from wallet address, memberAccount1, memberAccount2
async function main() {
dotenv.config();
const dnycvContractAddress = process.env.DNYCV_CONTRACT_ADDRESS || '';
const seed = fromString(process.env.SEED, 'base16');
// ceramic
let ceramic = new CeramicClient(CERAMIC_URL);
await ceramic.setDIDProvider(new Ed25519Provider(seed));
const idx = new IDX({ ceramic: ceramic, aliases: definitions });
// get account balances and total from contract
const endpoint = 'https://api.thegraph.com/subgraphs/name/dynamiculture/dnycv';
const graphQLClient = new GraphQLClient(endpoint);
const query = gql`
{
accountTokenBalances(orderBy: balance) {
id
token
balance
}
}
`;
let totalSupply = 0;
const tokenBits = BigNumber.from(10).pow(18);
const accounts: Array<AccountInfo> = (await graphQLClient.request(query)).accountTokenBalances;
for (const account of accounts) {
const amount = BigNumber.from(account.balance).div(tokenBits);
console.log(`balance for ${account.id} in ${account.token} : ${amount}`);
totalSupply += amount.toNumber();
// Get DID from wallet address
// currently failing:
// didResolutionMetadata: { error: 'unsupportedDidMethod' }
// Resolve a DID document
// const doc = await didResolver.resolve(`did:ethr:${account.id}`)
if (process.env.MEMBER1_ACCOUNT == account.id) {
account.did = process.env.MEMBER1_DID;
} else if (process.env.MEMBER2_ACCOUNT == account.id) {
account.did = process.env.MEMBER2_DID;
}
};
let proposalconvictions = [];
let participants = [];
for (const account of accounts) {
if (account.did) {
const memberConvictionDoc: UserConviction = await idx.get("convictions", account.did);
console.log('memberConvictionDoc:');
console.log(memberConvictionDoc);
const memberBalance = BigNumber.from(account.balance).div(tokenBits);
participants.push({ account: account.id, balance: memberBalance.toNumber(), convictions: 'commitid' });
if (memberConvictionDoc) {
for (let conviction of memberConvictionDoc.convictions) {
let FOUND = false;
let proposalallocation = conviction.allocation * memberBalance.toNumber();
for (let proposalconviction of proposalconvictions) {
if (conviction.proposal == proposalconviction.proposal) {
proposalconviction.totalConviction += proposalallocation;
FOUND = true;
}
}
if (!FOUND) {
proposalconvictions.push({ proposal: conviction.proposal, totalConviction: proposalallocation, triggered: false })
}
}
}
}
}
//TODO: set triggered based on threshold
console.log(`proposal convictions: ${proposalconvictions}`);
const convictionsState = {
context: 'eip155:1/erc20:' + dnycvContractAddress,
supply: totalSupply,
participants: [
// e.g.:
// {
// "account": process.env.MEMBER1_ACCOUNT,
// "balance": memberBal1,
// "convictions": memberCommitID1
// },
],
proposals: [
// e.g.:
// {
// proposal: "kjzl6cwe1jw148f6l3w9bdm3t9cmavjikasq1akxun9l0rsb29spklonfyrp3lf",
// totalConviction: 234,
// triggered: false
// },
]
}
convictionsState.participants = participants;
convictionsState.proposals = proposalconvictions;
const convictionsStateDoc = await ceramic.loadDocument(process.env.CONVICTIONSTATEDOCID);
console.log('convictions state before:');
console.log(convictionsStateDoc.content);
await convictionsStateDoc.change({ content: convictionsState });
console.log('convictions state after:');
console.log(convictionsStateDoc.content);
process.exit(0)
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});