Skip to content

Commit

Permalink
Fetching and upserting kitties retrieved from the CryptoKitties API
Browse files Browse the repository at this point in the history
  • Loading branch information
vince0656 committed Nov 28, 2019
1 parent 8fb930a commit f43517a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
37 changes: 37 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "vue-cli-service build"
},
"dependencies": {
"axios": "^0.19.0",
"core-js": "^3.3.2",
"dns": "^0.2.2",
"ethers": "^4.0.39",
Expand Down
22 changes: 19 additions & 3 deletions src/Firebase/CryptoKitties/KittiesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export default new class KittiesService {
return db.collection('kitties')
.doc('network')
.collection(network)
.where('owner', '==', address)
.where('owner.address', '==', address)
.get()
.then(snapshots => {
if (snapshots.empty) {
return [];
}
return snapshots.docs.map(doc => doc.id);
return snapshots.docs.map(doc => doc.data());
});
}

Expand All @@ -35,7 +35,7 @@ export default new class KittiesService {
return db.collection('kitties')
.doc('network')
.collection(network)
.where('owner', '==', address)
.where('owner.address', '==', address)
.get()
.then(snapshots => {
if (snapshots.empty) {
Expand All @@ -50,6 +50,22 @@ export default new class KittiesService {
});
}

async upsertKitties(network, kitties) {
//checkValidNetwork(network);

return Promise.all(kitties.map(kittie => {
// /kitties/network/{networkID}/{kittieId}/
return db
.collection('kitties')
.doc('network')
.collection(network)
.doc(kittie.id.toString())
.set(kittie, {
merge: true
});
}));
}

async matchKitties(network, studId, otherKittieId) {
// challenger data
const studMatchData = {
Expand Down
24 changes: 17 additions & 7 deletions src/components/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
<div v-if="kitties.user && kitties.user.length">
<small>Your kitties:</small>
<ul>
<li v-for="(kittie, idx) in kitties.user" :key="idx">
<small>{{kittie.kittieId}}</small>
<li v-for="(kittie, idx) in kitties.user" :key="idx" style="display: block;">
<input type="radio"/>
<small> {{kittie.id}}</small>
-
<small> {{kittie.name}}</small>
<small v-for="(swipeRight, idx) in kittie.swipeRights" :key="idx"> - {{swipeRight.status}} Swipe Right from {{swipeRight.stud}} <button @click="acceptRight(kittie.kittieId, swipeRight.stud)" v-if="swipeRight.status === 'PENDING'">Accept</button></small>
</li>
</ul>
Expand All @@ -22,15 +25,16 @@
<div v-if="kitties.other && kitties.other.length">
<small>Found kitties:</small>
<ul>
<li v-for="(kittie, idx) in kitties.other" :key="idx">
<small>{{kittie}}</small> - <button @click="poke(kittie)">Poke</button> - <button @click="swipeRight(kittie)">Swipe Right🔥</button>
<li v-for="(kittie, idx) in kitties.other" :key="idx" style="display: block;">
<small>{{kittie.id}}-{{kittie.name}}</small> - <button @click="poke(kittie)">Poke</button> - <button @click="swipeRight(kittie.id.toString())">Swipe Right🔥</button>
</li>
</ul>
</div>
</div>
</template>

<script>
import axios from 'axios';
import {ethers} from "ethers";
import Accounts from '@/Firebase/Accounts';
import {db, messaging} from '@/Firebase/index';
Expand Down Expand Up @@ -79,7 +83,7 @@
console.log(`poke [[${pokeId}]] to kittie ${kittieId}`);
},
swipeRight: async function (kittieId) {
const stud = this.kitties.user[0].kittieId;
const stud = this.kitties.user[0].id.toString();
const msg = `Hello treakle, want to breed with ${stud}?`;
await KittiesService.swipeRight('mainnet', kittieId, stud, msg, this.accounts.user);
Expand All @@ -90,10 +94,16 @@
console.log('done matching!');
},
find: async function() {
this.kitties.other = await KittiesService.getAllKittiesForAddress('mainnet', this.accounts.other);
this.kitties.other = await KittiesService.getAllKittiesForAddress('mainnet', this.accounts.other.toLowerCase());
console.log(this.kitties.other);
console.log(this.accounts.other);
},
getUserKitties: async function() {
this.kitties.user = await KittiesService.getAllKittiesWithSwipeRightsForAddress('mainnet', this.accounts.user);
const kitties = (await axios.get('https://api.cryptokitties.co/v2/kitties?offset=0&limit=12&owner_wallet_address=0x401cBf2194D35D078c0BcdAe4BeA42275483ab5F&parents=false&authenticated=true&include=sale,sire,other&orderBy=id&orderDirection=desc')).data.kitties;
console.log('kitties', kitties);
this.kitties.user = kitties;
KittiesService.upsertKitties('mainnet', kitties);
//this.kitties.user = await KittiesService.getAllKittiesWithSwipeRightsForAddress('mainnet', /*this.accounts.user*/'0x401cBf2194D35D078c0BcdAe4BeA42275483ab5F');
},
},
async mounted() {
Expand Down

0 comments on commit f43517a

Please sign in to comment.