Skip to content

Commit

Permalink
Testing with new features to enable on-chain price
Browse files Browse the repository at this point in the history
  • Loading branch information
br0wnD3v committed Jul 3, 2024
1 parent dd2ceec commit e32c678
Show file tree
Hide file tree
Showing 14 changed files with 1,561 additions and 1,183 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ node_modules
build
coverage
.env
config.json
doot_cache

# Editor
.vscode
Expand Down
13 changes: 6 additions & 7 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"version": 1,
"deployAliases": {
"berkeley": {
"devnet": {
"networkId": "testnet",
"url": "https://api.minascan.io/node/berkeley/v1/graphql",
"keyPath": "keys/berkeley.json",
"feepayerKeyPath": "/home/botvenom/.cache/zkapp-cli/keys/oracle.json",
"feepayerAlias": "oracle",
"fee": "0.1",
"smartContract": "Doot"
"url": "https://api.minascan.io/node/devnet/v1/graphql",
"keyPath": "keys/devnet.json",
"feepayerKeyPath": "/home/botvenom/.cache/zkapp-cli/keys/deployer.json",
"feepayerAlias": "deployer",
"fee": "0.1"
}
}
}
13 changes: 13 additions & 0 deletions example_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"deployAliases": {
"devnet": {
"networkId": "testnet",
"url": "https://api.minascan.io/node/devnet/v1/graphql",
"keyPath": "keys/devnet.json",
"feepayerKeyPath": "[YOUR_PATH_TO_KEY]",
"feepayerAlias": "deployer",
"fee": "0.1"
}
}
}
1,644 changes: 771 additions & 873 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
"format": "prettier --write --ignore-unknown **/*",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:doot": "node --experimental-vm-modules node_modules/jest/bin/jest.js Doot.test.ts",
"test:registry": "node --experimental-vm-modules node_modules/jest/bin/jest.js Registry.test.ts",
"testw": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
"lint": "npx eslint src/* --fix"
},
Expand All @@ -34,11 +36,12 @@
"jest": "^27.3.1",
"prettier": "^2.3.2",
"ts-jest": "^27.0.7",
"typescript": "^4.7.2"
"typescript": "5.1"
},
"dependencies": {
"dotenv": "^16.3.1",
"o1js": "^0.16.0",
"o1js-pack": "^0.5.1"
}
"o1js": "^1.4.0",
"o1js-pack": "^0.7.0"
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
199 changes: 127 additions & 72 deletions src/Doot.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { Doot, IpfsCID } from './Doot';
import { Doot, IpfsCID, PricesArray, offchainState } from './Doot';
import {
PrivateKey,
PublicKey,
Field,
Mina,
AccountUpdate,
Poseidon,
AccountUpdate,
MerkleMap,
MerkleMapWitness,
CircuitString,
} from 'o1js';

async function frameKey(key: CircuitString) {
return Poseidon.hash(key.toFields());
}

describe('Doot.js', () => {
let oraclePK: PrivateKey,
oracle: PublicKey,
Expand All @@ -24,23 +19,27 @@ describe('Doot.js', () => {

beforeAll(async () => {
// setup local blockchain
let Local = Mina.LocalBlockchain();
let Local = await Mina.LocalBlockchain();
Mina.setActiveInstance(Local);
// Local.testAccounts is an array of 10 test accounts that have been pre-filled with Mina
oraclePK = Local.testAccounts[0].privateKey;
oraclePK = Local.testAccounts[0].key;
oracle = oraclePK.toPublicKey();

// zkapp account
zkAppPrivateKey = PrivateKey.random();
zkAppAddress = zkAppPrivateKey.toPublicKey();

dootZkApp = new Doot(zkAppAddress);
offchainState.setContractInstance(dootZkApp);

await offchainState.compile();
await Doot.compile();

dootZkApp = new Doot(zkAppAddress);
// dootZkApp = new Doot(zkAppAddress);
// deploy zkapp
let txn = await Mina.transaction(oracle, () => {
let txn = await Mina.transaction(oracle, async () => {
AccountUpdate.fundNewAccount(oracle);
dootZkApp.deploy({ zkappKey: zkAppPrivateKey });
await dootZkApp.deploy();
});
await txn.prove();
await txn.sign([oraclePK, zkAppPrivateKey]).send();
Expand Down Expand Up @@ -71,7 +70,7 @@ describe('Doot.js', () => {
});

it(`Should set inital secret to 0`, async () => {
const onChainSecret = dootZkApp.secretToken.get();
const onChainSecret = dootZkApp.secret.get();
const expected = Field.from(0);

expect(onChainSecret).toEqual(expected);
Expand All @@ -80,25 +79,98 @@ describe('Doot.js', () => {

describe('Add/Update', () => {
const map: MerkleMap = new MerkleMap();

const secret: Field = Field.random();

let minaKey: Field;
let bitcoinKey: Field;
let chainlinkKey: Field;
let solanaKey: Field;
let ethereumKey: Field;
let cardanoKey: Field;
let avalancheKey: Field;
let rippleKey: Field;
let dogeKey: Field;
let polygonKey: Field;

let minaPrice: Field;
let bitcoinPrice: Field;
let ethereumPrice: Field;
let solanaPrice: Field;
let chainlinkPrice: Field;
let cardanoPrice: Field;
let avalanchePrice: Field;
let ripplePrice: Field;
let polygonPrice: Field;
let dogePrice: Field;

let prices: PricesArray;

beforeAll(async () => {
minaKey = await frameKey(CircuitString.fromString('Mina'));
minaPrice = Field.from(7500000000);
minaKey = CircuitString.fromString('Mina').hash();
bitcoinKey = CircuitString.fromString('Bitcoin').hash();
chainlinkKey = CircuitString.fromString('Chainlink').hash();
solanaKey = CircuitString.fromString('Solana').hash();
ethereumKey = CircuitString.fromString('Ethereum').hash();
cardanoKey = CircuitString.fromString('Cardano').hash();
avalancheKey = CircuitString.fromString('Avalanche').hash();
rippleKey = CircuitString.fromString('Ripple').hash();
dogeKey = CircuitString.fromString('Dogecoin').hash();
polygonKey = CircuitString.fromString('Polygon').hash();

minaPrice = Field.from(5248770935);
bitcoinPrice = Field.from(615439169547040);
ethereumPrice = Field.from(34421115510507);
solanaPrice = Field.from(1481398311039);
chainlinkPrice = Field.from(143095980879);
cardanoPrice = Field.from(3907233838);
avalanchePrice = Field.from(278604715977);
ripplePrice = Field.from(4749419511);
polygonPrice = Field.from(5645415935);
dogePrice = Field.from(1261024335);
});

it('Should add an asset called Mina and update the (BASE) commitment + IPFS hash + secret and fail the next time irrespective of the inputs.', async () => {
map.set(minaKey, minaPrice);
map.set(bitcoinKey, bitcoinPrice);
map.set(chainlinkKey, chainlinkPrice);
map.set(solanaKey, solanaPrice);
map.set(ethereumKey, ethereumPrice);
map.set(cardanoKey, cardanoPrice);
map.set(avalancheKey, avalanchePrice);
map.set(rippleKey, ripplePrice);
map.set(dogeKey, dogePrice);
map.set(polygonKey, polygonPrice);

prices = new PricesArray({
array: [
minaPrice,
bitcoinPrice,
ethereumPrice,
solanaPrice,
ripplePrice,
cardanoPrice,
avalanchePrice,
polygonPrice,
chainlinkPrice,
dogePrice,
],
});

console.log(typeof prices);

const updatedIPFS = IpfsCID.fromString(
'QmcNLBRwSQZDcdRe9uKUXZoLgnvTgAxx47Wfhod4JjYHTi'
'QmQy34PrqnoCBZySFAkRsC9q5BSFESGUxX6X8CQtrNhtrB'
);
const updatedCommitment = map.getRoot();

let txn = await Mina.transaction(oracle, () => {
dootZkApp.initBase(updatedCommitment, updatedIPFS, secret);
let txn = await Mina.transaction(oracle, async () => {
await dootZkApp.initBase(
updatedCommitment,
updatedIPFS,
prices,
secret
);
});
await txn.prove();
await txn.sign([oraclePK]).send();
Expand All @@ -107,23 +179,27 @@ describe('Doot.js', () => {
const onChainIpfsCid = IpfsCID.fromCharacters(
IpfsCID.unpack(onChainIpfsCID.packed)
);
const expectedIpfsCid = 'QmcNLBRwSQZDcdRe9uKUXZoLgnvTgAxx47Wfhod4JjYHTi';
const expectedIpfsCid = 'QmQy34PrqnoCBZySFAkRsC9q5BSFESGUxX6X8CQtrNhtrB';

const onChainCommitment = dootZkApp.commitment.get();
const onChainSecret = dootZkApp.secretToken.get();
const onChainSecret = dootZkApp.secret.get();

expect(onChainIpfsCid.toString()).toEqual(expectedIpfsCid);
expect(onChainCommitment).toEqual(updatedCommitment);
expect(onChainSecret).toEqual(Poseidon.hash([secret]));

try {
txn = await Mina.transaction(oracle, () => {
dootZkApp.initBase(updatedCommitment, updatedIPFS, secret);
txn = await Mina.transaction(oracle, async () => {
await dootZkApp.initBase(
updatedCommitment,
updatedIPFS,
prices,
secret
);
});
await txn.prove();
const signed = txn.sign([oraclePK]);
await txn.sign([oraclePK]).send();

await signed.send();
throw new Error('Expected_transaction_to_fail');
} catch (err: any) {
expect(err.message).toContain(
Expand All @@ -132,67 +208,46 @@ describe('Doot.js', () => {
}
});
it('Should update an existing asset called Mina including the base root + IPFS hash only if the secret is known.', async () => {
let updatedIPFS: IpfsCID;
let updatedPrice: Field;
let updatedCommitment: Field;
let txn: Mina.Transaction;

let minaWitness: MerkleMapWitness = map.getWitness(minaKey);

updatedPrice = Field.from(12300000000);
updatedIPFS = IpfsCID.fromString(
'QmcNLBRwSQZDcdRe9uKUXZoLgnvTgAxx47Wfhod4JjY009'
const updatedIPFS = IpfsCID.fromString(
'QmQy34PrqnoCBZySFAkRsC9q5BSFESGUxX6X8CQtr11110'
);
const updatedPrice = Field.from(6048770935);
map.set(minaKey, updatedPrice);

txn = await Mina.transaction(oracle, () => {
dootZkApp.updateIndividual(
minaWitness,
minaKey,
minaPrice,
prices = new PricesArray({
array: [
updatedPrice,
updatedIPFS,
secret
);
bitcoinPrice,
ethereumPrice,
solanaPrice,
ripplePrice,
cardanoPrice,
avalanchePrice,
polygonPrice,
chainlinkPrice,
dogePrice,
],
});
await txn.prove();
await txn.sign([oraclePK]).send();

const onChainIpfsCID = dootZkApp.ipfsCID.get();
const onChainIpfsCid = IpfsCID.fromCharacters(
IpfsCID.unpack(onChainIpfsCID.packed)
);
const expectedIpfsCid = 'QmcNLBRwSQZDcdRe9uKUXZoLgnvTgAxx47Wfhod4JjY009';

map.set(minaKey, updatedPrice);
updatedCommitment = map.getRoot();
const updatedCommitment = map.getRoot();
const onChainCommitment = dootZkApp.commitment.get();

expect(onChainIpfsCid.toString()).toEqual(expectedIpfsCid);
expect(onChainCommitment).toEqual(updatedCommitment);
expect(onChainCommitment != updatedCommitment);

// ----------------

let updatedUpdatedPrice = Field.from(15300000000);
updatedIPFS = IpfsCID.fromString(
'QmcNLBRwSQZDcdRe9uKUXZoLgnvTgAxx47Wfhod4JjYpqI'
);

try {
txn = await Mina.transaction(oracle, () => {
dootZkApp.updateIndividual(
minaWitness,
minaKey,
updatedPrice,
updatedUpdatedPrice,
await Mina.transaction(oracle, async () => {
await dootZkApp.update(
updatedCommitment,
updatedIPFS,
Field.random()
prices,
secret
);
});

await txn.prove();
const signed = txn.sign([oraclePK]);
})
.prove()
.sign([oraclePK])
.send();

await signed.send();
throw new Error('Expected_transaction_to_fail');
} catch (err: any) {
expect(err.message).toContain(
Expand Down
Loading

0 comments on commit e32c678

Please sign in to comment.