Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement CryptoKitties #7

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
639 changes: 638 additions & 1 deletion src/contracts/assets/abi/cryptokitties.abi.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/contracts/contract/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const FILENAME = ["coingecko", "marble-nft", "my-crypto-heroes-hero", "my-crypto-heroes-extension", "su-squares"];
export const FILENAME = [
"coingecko",
"marble-nft",
"my-crypto-heroes-hero",
"my-crypto-heroes-extension",
"su-squares",
"cryptokitties"
];

/** @typedef {{name: string, description: string, official_site: string, abi: string, image: {thumb: string, small: string, large: string}, contract: {contract_address: string, standard: string, image_address?: string}}} ContractJson */

Expand Down
9 changes: 9 additions & 0 deletions src/helper/abi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const balanceOfDestructure = (abi, balanceOf, input) => {
const balItem = abi.find(item => item.name === balanceOf);
if (balItem.outputs.length === 1) {
const name = balItem.outputs[0].name;
return parseInt(name !== "" ? input[name] : input);
} else {
return parseInt(input);
}
};
3 changes: 3 additions & 0 deletions src/helper/class/Web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export class Web3Class {
web3;
/** @type {Object} */
token = {};
abi = {};

constructor() {
this.web3 = window.web3;
}
Expand Down Expand Up @@ -36,6 +38,7 @@ export class Web3Class {
async setContract({ abiAddress, address, acc }) {
const res = await fetch(`abi/${abiAddress}`);
const abiJson = await res.json();
this.abi = { ...this.abi, [address]: abiJson };
this.token = new this.web3.eth.Contract(abiJson, address, {
from: acc,
gasPrice: "20000000000"
Expand Down
15 changes: 13 additions & 2 deletions src/store/contract/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ContractActionName, ContractMutationName } from "./names";
import { errorNotification } from "src/helper/notifications";
import { range } from "src/helper/utils";
import { web3Instance } from "src/boot/web3";
import { balanceOfDestructure } from "src/helper/abi";

/** @typedef {import("./state").default} ContractState */

Expand Down Expand Up @@ -78,7 +79,12 @@ const actions = {
for (let contract of Object.keys(state.contractDetails)) {
const { address, name, abi } = state.contractDetails[contract];
await web3Instance.setContract({ abiAddress: abi, address, acc });
const bal = await web3Instance.getBalance(acc);
const tempBal = await web3Instance.getBalance(acc);
const bal = balanceOfDestructure(
web3Instance.abi[address],
"balanceOf",
tempBal
);
commit(ContractMutationName.setContractsBalance, { name, bal });
}
},
Expand Down Expand Up @@ -115,7 +121,12 @@ const actions = {
}
const { address, abi } = state.contractDetails[name];
await web3Instance.setContract({ abiAddress: abi, address, acc });
const bal = await web3Instance.getBalance(acc);
const tempBal = await web3Instance.getBalance(acc);
const bal = balanceOfDestructure(
web3Instance.abi[address],
"balanceOf",
tempBal
);
commit(ContractMutationName.setContractsBalance, { name, bal });
if (bal === 0) {
return;
Expand Down
16 changes: 16 additions & 0 deletions test/jest/__tests__/helper/abi.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import openZeppelinAbi from "src/contracts/assets/abi/openzeppelin.abi.json";
import kittiesAbi from "src/contracts/assets/abi/cryptokitties.abi.json";
import { balanceOfDestructure } from "src/helper/abi";

describe("abi helper function", () => {
it("get proper balance of", () => {
const openZeppelinInput = 1;
expect(
balanceOfDestructure(openZeppelinAbi, "balanceOf", openZeppelinInput)
).toEqual(1);
const kittiesInput = { "0": "0", count: "0" };
expect(balanceOfDestructure(kittiesAbi, "balanceOf", kittiesInput)).toEqual(
0
);
});
});