Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
New Deployment with Initial Yield
Browse files Browse the repository at this point in the history
  • Loading branch information
hayesgm committed Mar 17, 2021
1 parent c43f056 commit 1e3bc87
Show file tree
Hide file tree
Showing 11 changed files with 1,027 additions and 55 deletions.
42 changes: 38 additions & 4 deletions chains/build_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ const path = require('path');
const getopts = require('getopts');
const chalk = require('chalk');
const types = require('@polkadot/types');
const Contract = require('web3-eth-contract');

function getEthProvider(network) {
if (['ropsten'].includes(network)) {
return `https://${network}-eth.compound.finance`;
} else {
throw new Error(`Unknown or unsupported eth network: "${network}"`);
}
}

async function readFile(chain, file) {
return await fs.readFile(path.join(__dirname, chain, file), 'utf8');
Expand All @@ -24,6 +33,10 @@ async function fetchChainDeployment(network) {
return JSON.parse(await fs.readFile(path.join(__dirname, '..', 'ethereum','networks', `${network}.json`), 'utf8'));
}

async function fetchChainDeploymentABI(network) {
return JSON.parse(await fs.readFile(path.join(__dirname, '..', 'ethereum','networks', `${network}-abi.json`), 'utf8'));
}

function exec(target, args, opts = {}) {
return new Promise((resolve, reject) => {
let proc = child_process.spawn(target, args, opts);
Expand Down Expand Up @@ -82,11 +95,31 @@ async function setAuthorities(chainSpec, chainConfig, opts) {
}

async function setStarport(chainSpec, chainConfig, opts) {
let chainDeployment = await fetchChainDeployment(chainConfig.network);
let chainDeployment = await fetchChainDeployment(chainConfig.eth_network);
let starportAddress = must(chainDeployment.Contracts, 'Starport');
chainSpec.properties.eth_starport_address = starportAddress;
}

async function setInitialYield(chainSpec, chainConfig, opts) {
let chainDeployment = await fetchChainDeployment(chainConfig.eth_network);
let chainDeploymentABI = await fetchChainDeploymentABI(chainConfig.eth_network);
let cashTokenAddress = chainDeployment.Contracts.Cash;
if (!cashTokenAddress) {
throw new Error(`Missing cash token address for network ${chainConfig.eth_network}`);
}
let cashTokenABI = chainDeploymentABI.Cash;
if (!cashTokenABI) {
throw new Error(`Missing cash token ABI for network ${chainConfig.eth_network}`);
}
let cashToken = new Contract(cashTokenABI, cashTokenAddress);
cashToken.setProvider(getEthProvider(chainConfig.eth_network));
let yieldStart = await cashToken.methods.cashYieldStart().call();
let cashYield = (await cashToken.methods.cashYieldAndIndex().call()).yield;

chainSpec.genesis.runtime.palletCash.lastYieldTimestamp = Number(yieldStart) * 1000;
chainSpec.genesis.runtime.palletCash.cashYield = Number(cashYield);
}

function must(hash, key, validator = (x) => x !== undefined) {
if (hash.hasOwnProperty(key) || validator(undefined)) {
let val = hash[key];
Expand All @@ -104,20 +137,20 @@ function upper(obj) {
}

async function setAssetInfo(chainSpec, chainConfig, opts) {
let compoundConfig = await fetchCompoundConfig(chainConfig.network);
let compoundConfig = await fetchCompoundConfig(chainConfig.eth_network);
let contracts = upper(compoundConfig['Contracts']);
let tokenInfos = upper(compoundConfig['Tokens']);

let rateModels = chainConfig.rate_models;
let assets = Object.entries(chainConfig.tokens).map(([symbol, info]) => {
let tokenAddress = contracts[symbol];
if (!tokenAddress) {
throw new Error(`Missing contract address on ${chainConfig.network} for ${symbol} from compound-config`);
throw new Error(`Missing contract address on ${chainConfig.eth_network} for ${symbol} from compound-config`);
}

let tokenInfo = tokenInfos[symbol];
if (!tokenInfo) {
throw new Error(`Missing token info on ${chainConfig.network} for ${symbol} from compound-config`);
throw new Error(`Missing token info on ${chainConfig.eth_network} for ${symbol} from compound-config`);
}
let asset = `ETH:${tokenAddress}`;
let decimals = must(tokenInfo, 'decimals', (d) => typeof(d) === 'number');
Expand Down Expand Up @@ -167,6 +200,7 @@ async function buildSpec(opts) {
await setAssetInfo(chainSpec, chainConfig, opts);
await setReporters(chainSpec, chainConfig, opts);
await setStarport(chainSpec, chainConfig, opts);
await setInitialYield(chainSpec, chainConfig, opts);

await writeFile(chain, 'chain-spec.json', JSON.stringify(chainSpec, null, 2));

Expand Down
3 changes: 2 additions & 1 deletion chains/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@polkadot/types": "^4.1.1",
"chalk": "^4.1.0",
"compound-config": "https://github.com/compound-finance/compound-config.git",
"getopts": "^2.3.0"
"getopts": "^2.3.0",
"web3-eth-contract": "^1.3.4"
}
}
2 changes: 1 addition & 1 deletion chains/testnet/chain-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"network": "ropsten",
"eth_network": "ropsten",
"base_chain": "testnet",
"validators": [
{
Expand Down
56 changes: 28 additions & 28 deletions chains/testnet/chain-spec-raw.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions chains/testnet/chain-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"telemetryEndpoints": null,
"protocolId": "local",
"properties": {
"eth_starport_address": "0xe7A19d74645b6eC10cCD3333e394CE5071Fa5C98"
"eth_starport_address": "0xD905AbBA1C5Ea48c0598bE9F3f8ae31290B58613"
},
"consensusEngine": null,
"lightSyncState": null,
Expand All @@ -23,8 +23,8 @@
"authorities": []
},
"palletCash": {
"lastYieldTimestamp": 0,
"cashYield": 0,
"lastYieldTimestamp": 1615967424000,
"cashYield": 300,
"assets": [
{
"asset": "ETH:0xc0e2d7d9279846b80eacdea57220ab2333bc049d",
Expand Down
Loading

0 comments on commit 1e3bc87

Please sign in to comment.