Skip to content

Commit

Permalink
Update script for Project
Browse files Browse the repository at this point in the history
  • Loading branch information
huyminh1115 committed Jan 18, 2024
1 parent 33e0fe1 commit fa4a3ad
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 5 deletions.
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BERKELEY_MINA=
BERKELEY_ARCHIVE=
BERKELEY_COMMITTEE_ADDRESS=
BERKELEY_DKG_ADDRESS=
BERKELEY_ROUND_1_ADDRESS=
BERKELEY_ROUND_2_ADDRESS=
BERKELEY_REQUEST_ADDRESS=
BERKELEY_RESPONSE_ADDRESS=
BERKELEY_PROJECT_ADDRESS=
BERKELEY_CAMPAIGN_ADDRESS=
BERKELEY_PARTICIPATION_ADDRESS=
BERKELEY_FUNDING_ADDRESS=
BERKELEY_TREASURY_ADDRESS=
30 changes: 29 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@
"feepayerKeyPath": "/home/huyminh/.cache/zkapp-cli/keys/myaccount.json",
"feepayerAlias": "myaccount",
"fee": "0.1"
},
"acc4": {
"url": "https://proxy.berkeley.minaexplorer.com/graphql",
"keyPath": "keys/acc4.json",
"feepayerKeyPath": "/home/huyminh/.cache/zkapp-cli/keys/myaccount.json",
"feepayerAlias": "myaccount",
"fee": "0.1"
},
"acc3": {
"url": "https://proxy.berkeley.minaexplorer.com/graphql",
"keyPath": "keys/acc3.json",
"feepayerKeyPath": "/home/huyminh/.cache/zkapp-cli/keys/myaccount.json",
"feepayerAlias": "myaccount",
"fee": "0.1"
},
"acc2": {
"url": "https://proxy.berkeley.minaexplorer.com/graphql",
"keyPath": "keys/acc2.json",
"feepayerKeyPath": "/home/huyminh/.cache/zkapp-cli/keys/myaccount.json",
"feepayerAlias": "myaccount",
"fee": "0.1"
},
"acc1": {
"url": "https://proxy.berkeley.minaexplorer.com/graphql",
"keyPath": "keys/acc1.json",
"feepayerKeyPath": "/home/huyminh/.cache/zkapp-cli/keys/myaccount.json",
"feepayerAlias": "myaccount",
"fee": "0.1"
}
}
}
}
16 changes: 14 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
"zkapp-cli": "^0.15.0"
},
"dependencies": {
"@auxo-dev/dkg": "0.2.12",
"@auxo-dev/auxo-libs": "0.3.5",
"@auxo-dev/dkg": "0.2.12",
"dotenv": "^16.3.1",
"o1js": "0.15.1"
}
}
}
87 changes: 87 additions & 0 deletions src/scripts/interactions/prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'dotenv/config.js';
import fs from 'fs';
import { Cache, Mina, PrivateKey, PublicKey, fetchAccount } from 'o1js';
import { Config, JSONKey, Key } from '../helper/config.js';

Check failure on line 4 in src/scripts/interactions/prepare.ts

View workflow job for this annotation

GitHub Actions / test

Module '"../helper/config.js"' has no exported member 'JSONKey'.
import { wait } from '../helper/deploy.js';

Check failure on line 5 in src/scripts/interactions/prepare.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find module '../helper/deploy.js' or its corresponding type declarations.
import { Contract, ZkAppEnum } from '../../constants.js';
import {
EMPTY_ADDRESS_MT,
AddressStorage,
} from '../../contracts/SharedStorage.js';

export async function prepare() {
// Cache folder
const cache = Cache.FileSystem('./caches');

// Network configuration
const network = Mina.Network({
mina: process.env.BERKELEY_MINA as string,
archive: process.env.BERKELEY_ARCHIVE as string,
});
Mina.setActiveInstance(network);
const FEE = 0.101 * 1e9;

// Accounts configuration
let configJson: Config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
let acc1: JSONKey = JSON.parse(
fs.readFileSync(configJson.deployAliases['acc1'].keyPath, 'utf8')
);
let acc2: JSONKey = JSON.parse(
fs.readFileSync(configJson.deployAliases['acc2'].keyPath, 'utf8')
);
let acc3: JSONKey = JSON.parse(
fs.readFileSync(configJson.deployAliases['acc3'].keyPath, 'utf8')
);
let acc4: JSONKey = JSON.parse(
fs.readFileSync(configJson.deployAliases['acc4'].keyPath, 'utf8')
);

let feePayerKey: Key;
feePayerKey = {
privateKey: PrivateKey.fromBase58(acc1.privateKey),
publicKey: PublicKey.fromBase58(acc1.publicKey),
};
let sender, feePayerNonce;
do {
console.log('Fetch nonce...');
sender = await fetchAccount({ publicKey: feePayerKey.publicKey });
feePayerNonce = Number(sender.account?.nonce);
if (!isNaN(feePayerNonce)) {
console.log('Nonce:', feePayerNonce);
break;
}
await wait(1000); // 1s
} while (true);

let addressMerkleTree = EMPTY_ADDRESS_MT();

// Read contract address from config
await Promise.all(
Object.keys(Contract)
.filter((item) => isNaN(Number(item)))
.map(async (e) => {
let config = configJson.deployAliases[e.toLowerCase()];
let keyBase58: { privateKey: string; publicKey: string } = JSON.parse(
fs.readFileSync(config.keyPath, 'utf8')
);
let key = {
privateKey: PrivateKey.fromBase58(keyBase58.privateKey),
publicKey: PublicKey.fromBase58(keyBase58.publicKey),
};
addressMerkleTree.setLeaf(
AddressStorage.calculateIndex(ZkAppEnum[e]).toBigInt(),
AddressStorage.calculateLeaf(key.publicKey)
);
})
);

return {
feePayer: {
key: feePayerKey,
nonce: feePayerNonce,
fee: FEE,
},
cache,
addressMerkleTree,
};
}
88 changes: 88 additions & 0 deletions src/scripts/interactions/project/createProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import fs from 'fs';
import {
Cache,
Field,
Mina,
PrivateKey,
Provable,
PublicKey,
Reducer,
fetchAccount,
} from 'o1js';
import { Config, JSONKey, Key } from '../../helper/config.js';

Check failure on line 12 in src/scripts/interactions/project/createProject.ts

View workflow job for this annotation

GitHub Actions / test

Module '"../../helper/config.js"' has no exported member 'JSONKey'.
import {
ContractList,
compile,
wait,
proveAndSend,
} from '../../helper/deploy.js';

Check failure on line 18 in src/scripts/interactions/project/createProject.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find module '../../helper/deploy.js' or its corresponding type declarations.
import { fetchActions, fetchZkAppState } from '../../helper/deploy.js';

Check failure on line 19 in src/scripts/interactions/project/createProject.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find module '../../helper/deploy.js' or its corresponding type declarations.
import {
ProjectContract,
ProjectAction,
CreateProject,
CreateProjectInput,
ProjectProof,
} from '../../../contracts/Project.js';
import {
MemberStorage,
InfoStorage,
MemberArray,
} from '../../../contracts/ProjectStorage.js';
import axios from 'axios';
import { IPFSHash } from '@auxo-dev/auxo-libs';
import { prepare } from '../prepare.js';

async function main() {
const { cache, feePayer } = await prepare();

// Compile programs
await compile(CreateProject, cache);
await compile(ProjectContract, cache);

const projectAddress =
'B62qoGy5GKNRaa2P8uh4ppdRqnCVjNyHqVSsaD4JMcE6FawLRKmmN4u';
const projectContract = new ProjectContract(
PublicKey.fromBase58(projectAddress)
);

// Do this and state value of contract is fetched in Mina
const rawState = (await fetchZkAppState(projectAddress)) || [];

let arrayPublicKey = [
'B62qjvrida5Kr4rj7f4gDZyG77TdFMp2ntZ9uf5Xzb7iPodykUgwYqm',
'B62qnhBkHqUeUTmYiAvvGdywce7j5PeTdU6t6mi7UAL8emD3mDPtQW2',
'B62qnk1is4cK94PCX1QTwPM1SxfeCF9CcN6Nr7Eww3JLDgvxfWdhR5S',
'B62qmtfTkHLzmvoKYcTLPeqvuVatnB6wtnXsP6jrEi6i2eUEjcxWauH',
].map((e) => PublicKey.fromBase58(e));
let memberArray = new MemberArray(arrayPublicKey);

let input = new CreateProjectInput({
members: memberArray,
ipfsHash: IPFSHash.fromString(
'QmNQLoDczHM3HXKodoYQnRszgd4JR4ZxzEKYe534eEBCc2'
),
payeeAccount: PublicKey.fromBase58(
'B62qnk1is4cK94PCX1QTwPM1SxfeCF9CcN6Nr7Eww3JLDgvxfWdhR5S'
),
});

let tx = await Mina.transaction(
{
sender: feePayer.key.publicKey,
fee: feePayer.fee,
nonce: feePayer.nonce++,
},
() => {
projectContract.createProject(input);
}
);
await proveAndSend(tx, feePayer.key, 'projectContract', 'createProject');
}

main()
.then()
.catch((err) => {
console.error(err);
process.exit(1);
});
Loading

0 comments on commit fa4a3ad

Please sign in to comment.