Skip to content

Commit 98a45f7

Browse files
committed
Simplify contracts-interface and refactor examples
1 parent 44bed31 commit 98a45f7

16 files changed

+1688
-2561
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"semi": ["error", "always"],
3333
"no-console": "error",
3434
"@typescript-eslint/ban-ts-comment": "off",
35-
"@typescript-eslint/explicit-module-boundary-types": "off"
35+
"@typescript-eslint/explicit-module-boundary-types": "off",
36+
"@typescript-eslint/no-var-requires": "off"
3637
},
3738
"settings": {
3839
"import/resolver": {

contracts/.eslintrc

-5
This file was deleted.

package.json

-5
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,5 @@
6666
"eslint"
6767
]
6868
},
69-
"optionalDependencies": {
70-
"chokidar": "^3.5.2",
71-
"fsevents": "^2.3.2",
72-
"watchpack-chokidar2": "^2.0.1"
73-
},
7469
"packageManager": "[email protected]"
7570
}

packages/contracts-interface/examples/.eslintrc

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,15 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="en">
33
<head>
4+
<meta charset="UTF-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
8+
/>
9+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
410
<title>Example for browser envionments</title>
511
</head>
6-
712
<body>
813
<p>If you open the console "command-J" you should see the resutls printed</p>
9-
10-
<script src="../build/index.js"></script>
11-
<script>
12-
(async () => {
13-
const { synthetix } = window;
14-
// this instance exposes props for the given network: synths, sources, targets, users, as well as helper function toBytes32 - as per synthetix: https://github.com/Synthetixio/synthetix/blob/develop/index.js#L199.
15-
const snxjs = synthetix({ network: 'mainnet' });
16-
console.log('snxjs', snxjs);
17-
18-
const { formatEther } = snxjs.utils;
19-
20-
const synths = snxjs.synths.map(({ name }) => name);
21-
const fromBlock = 10260987;
22-
const blockOptions = fromBlock ? { blockTag: Number(fromBlock) } : {};
23-
24-
let totalInUSD = 0;
25-
26-
const unformattedSnxPrice = await snxjs.contracts.ExchangeRates.rateForCurrency(
27-
snxjs.toBytes32('SNX'),
28-
blockOptions
29-
); // note blockOptions must be passed to `ethers.Contract` as the final parameter (and fails if no archive node)
30-
const snxPrice = formatEther(unformattedSnxPrice);
31-
console.log('snxPrice', snxPrice);
32-
33-
let results = await Promise.all(
34-
synths.map(async (synth) => {
35-
const unformattedTotalSupply = await snxjs.contracts[`Synth${synth}`].totalSupply(
36-
blockOptions
37-
);
38-
const totalSupply = formatEther(unformattedTotalSupply);
39-
40-
const rateForSynth = formatEther(
41-
await snxjs.contracts.ExchangeRates.rateForCurrency(
42-
snxjs.toBytes32(synth),
43-
blockOptions
44-
)
45-
);
46-
const totalSupplyInUSD = rateForSynth * totalSupply;
47-
totalInUSD += totalSupplyInUSD;
48-
const rateIsFrozen = await snxjs.contracts.ExchangeRates.rateIsFrozen(
49-
snxjs.toBytes32(synth),
50-
blockOptions
51-
);
52-
53-
return {
54-
synth,
55-
totalSupply,
56-
rateForSynth,
57-
totalSupplyInUSD,
58-
rateIsFrozen,
59-
};
60-
})
61-
);
62-
63-
console.log('totalInUSD', totalInUSD);
64-
console.log('results', results);
65-
})().catch((e) => {
66-
console.log('error in browser example:', e);
67-
});
68-
</script>
6914
</body>
7015
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* eslint-disable no-console */
2+
3+
const { synthetix } = require('../src');
4+
const ethers = require('ethers');
5+
6+
async function synths() {
7+
// this instance exposes props for the given network: synths, sources, targets, users, as well as helper function toBytes32 - as per synthetix: https://github.com/Synthetixio/synthetix/blob/develop/index.js#L199.
8+
const snxjs = synthetix({ network: 'mainnet' });
9+
10+
const { formatEther } = snxjs.utils;
11+
12+
const synths = snxjs.synths.map(({ name }) => name);
13+
const fromBlock = 10260987;
14+
const blockOptions = fromBlock ? { blockTag: Number(fromBlock) } : {};
15+
16+
let totalInUSD = 0;
17+
18+
const unformattedSnxPrice = await snxjs.contracts.ExchangeRates.rateForCurrency(
19+
snxjs.toBytes32('SNX'),
20+
blockOptions
21+
); // note blockOptions must be passed to `ethers.Contract` as the final parameter (and fails if no archive node)
22+
const snxPrice = formatEther(unformattedSnxPrice);
23+
console.log('snxPrice', snxPrice);
24+
return await Promise.all(
25+
synths.map(async (synth, index) => {
26+
const totalAmount = await snxjs.contracts[`Synth${synth}`].totalSupply(blockOptions);
27+
28+
const totalSupply = formatEther(totalAmount);
29+
console.log('synth', synth);
30+
console.log('totalSupply', totalSupply);
31+
const rateForSynth = formatEther(
32+
await snxjs.contracts.ExchangeRates.rateForCurrency(snxjs.toBytes32(synth), blockOptions)
33+
);
34+
const totalSupplyInUSD = rateForSynth * totalSupply;
35+
totalInUSD += totalSupplyInUSD;
36+
if (index === synths.length - 1) {
37+
console.log('totalInUSD', totalInUSD);
38+
}
39+
40+
// const rateIsFrozen = await snxjs.contracts.ExchangeRates.rateIsFrozen(
41+
// snxjs.toBytes32(synth),
42+
// blockOptions
43+
// );
44+
const rateIsFrozen = 'NOT SUPPORTED';
45+
46+
return { synth, totalAmount, totalSupply, rateForSynth, totalSupplyInUSD, rateIsFrozen };
47+
})
48+
);
49+
}
50+
51+
async function signer() {
52+
const provider = ethers.providers.getDefaultProvider('kovan');
53+
const signer = new ethers.Wallet(
54+
// just a dummy kovan wallet with a little keth from the faucet
55+
'0xa0d951c494421559c63089093b020cf2f7aac003c916967dc36e989bc695222e',
56+
provider
57+
);
58+
const snxjs = synthetix({ network: 'kovan', signer, provider });
59+
const { formatEther, parseEther, parseUnits } = snxjs.utils;
60+
61+
console.log(
62+
'Remaining ETH capacity in the EtherWrapper (kovan):',
63+
formatEther(await snxjs.contracts.EtherWrapper.capacity())
64+
);
65+
66+
console.log('Engaging the native ether wrapper to mint sETH');
67+
const response = await snxjs.contracts.NativeEtherWrapper.mint({
68+
value: parseEther('0.01'),
69+
gasPrice: parseUnits('2', 'gwei'),
70+
gasLimit: 500e3,
71+
});
72+
console.log('Submitted', response.hash);
73+
await response.wait();
74+
console.log('Mined', `https://kovan.etherscan.io/tx/${response.hash}`);
75+
}
76+
77+
async function run() {
78+
console.log(await synths());
79+
await signer();
80+
}
81+
82+
run();

packages/contracts-interface/examples/node-example.js

-45
This file was deleted.

packages/contracts-interface/examples/signer-example.js

-28
This file was deleted.

packages/contracts-interface/package.json

+20-20
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,23 @@
66
"version": "2.74.2",
77
"description": "A library for interacting with Synthetix smart contracts",
88
"source": "./src/index.ts",
9-
"main": "./build/node/src/index.js",
10-
"module": "./build/node/src/index.js",
11-
"browser": "./build/node/src/index.js",
12-
"types": "./build/node/src/index.d.ts",
9+
"main": "./build/src/index.js",
10+
"module": "./build/src/index.js",
11+
"browser": "./build/src/index.js",
12+
"types": "./build/src/index.d.ts",
1313
"files": [
1414
"build",
1515
"src"
1616
],
1717
"scripts": {
18-
"build": "yarn build-browser && yarn build-node",
19-
"build-node": "tsc -p tsconfig.node.json",
20-
"build-browser": "webpack-cli --mode=production --max-old-space-size=4096",
21-
"examples:node": "ts-node --project tsconfig.node.json ./examples/signer-example.js",
22-
"examples:browser": "yarn build-browser && http-server -o ./examples/browser-example.html -c-1",
18+
"build": "tsc",
19+
"examples": "ts-node ./examples/index.js",
20+
"start": "webpack-cli serve",
2321
"lint": "eslint './src/**/*.{js,ts,tsx}' && tsc",
2422
"lint:fix": "eslint --fix './src/**/*.{js,ts,tsx}'",
25-
"test": "jest --forceExit --coverage --no-cache",
26-
"watch": "yarn build-node && chokidar 'src/**/*.ts' -c 'yarn build-node'",
27-
"tsc": "tsc --noemit"
28-
},
29-
"repository": {
30-
"type": "git",
31-
"url": "https://github.com/Synthetixio/js-monorepo"
23+
"test": "jest --coverage --no-cache"
3224
},
25+
"repository": "Synthetixio/js-monorepo",
3326
"author": "Synthetix",
3427
"license": "MIT",
3528
"bugs": {
@@ -46,15 +39,22 @@
4639
"@babel/preset-env": "^7.13.10",
4740
"@babel/preset-typescript": "^7.13.0",
4841
"@types/node": "^17.0.43",
49-
"chokidar-cli": "^2.1.0",
5042
"eslint": "^7.26.0",
43+
"html-webpack-plugin": "^5.5.0",
5144
"jest": "^27.0.6",
52-
"ts-loader": "^8.0.2",
45+
"ts-loader": "^9.3.1",
5346
"ts-node": "^10.2.1",
5447
"typescript": "^4.4.2",
55-
"webpack": "^4.44.1",
56-
"webpack-cli": "^3.3.12"
48+
"webpack": "^5.73.0",
49+
"webpack-cli": "^4.10.0",
50+
"webpack-dev-server": "^4.9.3"
5751
},
52+
"browserslist": [
53+
"last 1 Chrome version",
54+
"last 1 Firefox version",
55+
"last 1 Edge version",
56+
"last 1 Opera version"
57+
],
5858
"jest": {
5959
"testMatch": [
6060
"<rootDir>/**/*.test.{js,jsx,ts,tsx}"

packages/contracts-interface/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"compilerOptions": {
44
"declaration": true,
55
"declarationMap": true,
6-
"declarationDir": "./build"
6+
"module": "commonjs",
7+
"outDir": "./build/src"
78
},
89
"include": ["src"]
910
}

packages/contracts-interface/tsconfig.node.json

-10
This file was deleted.

0 commit comments

Comments
 (0)