Skip to content

Commit

Permalink
final changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenandrews committed Sep 5, 2024
1 parent e4775aa commit f799552
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,6 @@ export default function App() {
.connect('https://icp0.io/', identity)
.token(id)
.list(currentAccount, price);
console.log(r);
if (r) {
if (refresh) await refresh();
if (loader) loader(false);
Expand Down
28 changes: 14 additions & 14 deletions src/components/UserCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,20 @@ export default function UserCollection(props) {
data = data.map(a => ({...a, token: a.id}));
break;
case 'favorites':
var r = await extjs
.connect('https://icp0.io/', props.identity)
.canister('6z5wo-yqaaa-aaaah-qcsfa-cai')
.liked();
data = r.filter((a, i) => r.indexOf(a) == i);
data = data.map(a => ({
id: a,
token: a,
price: 0,
time: 0,
owner: false,
canister: extjs.decodeTokenId(a).canister,
listing: null,
}));
// var r = await extjs
// .connect('https://icp0.io/', props.identity)
// .canister('6z5wo-yqaaa-aaaah-qcsfa-cai')
// .liked();
// data = r.filter((a, i) => r.indexOf(a) == i);
// data = data.map(a => ({
// id: a,
// token: a,
// price: 0,
// time: 0,
// owner: false,
// canister: extjs.decodeTokenId(a).canister,
// listing: null,
// }));
break;
case 'offers-made':
var r = await extjs
Expand Down
42 changes: 41 additions & 1 deletion src/ic/extjs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global BigInt */
import {Actor, HttpAgent} from '@dfinity/agent';
import {Principal} from '@dfinity/principal';
import { IDL } from '@dfinity/candid';
import {
LEDGER_CANISTER_ID,
GOVERNANCE_CANISTER_ID,
Expand Down Expand Up @@ -150,7 +151,45 @@ for (const a in tokensToLoad) {
}
}
}
function isQueryCall(idlFactory, methodName) {
// Parse the IDL using the provided factory function
const idl = idlFactory({ IDL });

// Retrieve the method definition by its name
const method = idl._fields.find(([name]) => name === methodName);

if (!method) {
throw new Error(`Method ${methodName} not found in IDL`);
}

// Check if the method is a query or update
const methodType = method[1].annotations.includes('query') ? 'query' : 'update';

return methodType;
}

function createLoggingActor(actor, idl) {
return new Proxy(actor, {
get(target, propKey) {
// Get the original property (which could be a method or property)
const origMethod = target[propKey];

// Check if the property is a function (i.e., a method call)
if (typeof origMethod === 'function') {
return async function (...args) {
const iqc = isQueryCall(idl, propKey);//propKey.startsWith("query");
//console.log(`Standard Actor: Calling ${iqc} method: ${propKey}`, 'with arguments:', args);
const result = await origMethod.apply(target, args);
//console.log(`Result from ${iqc} method: ${propKey}`, result);
return result;
};
}

// If not a function, just return the original property
return origMethod;
}
});
}
class VirtualActor {
_canister = false;
_idl = false;
Expand Down Expand Up @@ -271,10 +310,11 @@ class ExtConnection {
if (this._agent == 'infinityWallet' || this._agent == 'plug') {
this._canisters[cid] = new VirtualActor(cid, idl, this._agent);
} else {
this._canisters[cid] = Actor.createActor(idl, {
let actor = Actor.createActor(idl, {
agent: this._agent,
canisterId: cid,
});
this._canisters[cid] = createLoggingActor(actor, idl);
}
}
return this._canisters[cid];
Expand Down
25 changes: 14 additions & 11 deletions src/ic/stoic-identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class StoicIdentity extends SignIdentity {
constructor(principal, pubkey, transportMethod) {
super();
this._transportMethod = 'popup';
this._accounts = [];
if (transportMethod) this._transportMethod = transportMethod;
this._principal = principal;
this._publicKey = pubkey;
Expand Down Expand Up @@ -66,14 +67,7 @@ export class StoicIdentity extends SignIdentity {
new PublicKey(result.key, result.type),
this._transportMethod
);
id.accounts()
.then((r) => {
resolve(id);
})
.catch((e) => {
console.log("Couldn't load accounts", e);
resolve(false);
});
resolve(id);
}
});
}
Expand All @@ -90,8 +84,17 @@ export class StoicIdentity extends SignIdentity {
return _stoicSign("sign", data, this.getPrincipal().toText(), this._transportMethod);
}

accounts() {
return _stoicSign("accounts", "accounts", this.getPrincipal().toText(), this._transportMethod);
accounts(force) {
return new Promise(async (resolve, reject) => {
if (!this._accounts.length || force) {
_stoicSign("accounts", "accounts", this.getPrincipal().toText(), this._transportMethod).then(accounts => {
this._accounts = accounts;
resolve(this._accounts);
}).catch(reject);
} else {
resolve(this._accounts);
}
});
}

transformRequest(request) {
Expand Down Expand Up @@ -248,7 +251,7 @@ function _postToPopup(data, resolve, reject) {
const popup = window.open(
`${_stoicOrigin}/?stoicTunnel&transport=popup&lid=`+thisIndex,
"stoic_"+thisIndex,
"width=500,height=600"
"width=500,height=250,top=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,status=no"
);
if (!popup) {
return reject("Failed to open popup window. It may have been blocked by the browser.");
Expand Down
9 changes: 5 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,11 @@ const clipboardCopy = text => {
return _liked;
},
EntrepotUpdateLiked = async identity => {
if (identity) {
const _api = extjs.connect('https://icp0.io/', identity);
_liked = await _api.canister('6z5wo-yqaaa-aaaah-qcsfa-cai').liked();
} else _liked = [];
// if (identity) {
// const _api = extjs.connect('https://icp0.io/', identity);
// _liked = await _api.canister('6z5wo-yqaaa-aaaah-qcsfa-cai').liked();
// } else _liked = [];
_liked = [];
},
EntrepotSaveLiked = async identity => {
if (identity) {
Expand Down
3 changes: 2 additions & 1 deletion src/volt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export async function loadVolt(identity) {
}
let newVoltPrincipal = false;
const voltFactoryAPI = extjs
.connect('https://icp0.io/', identity)
.connect('https://icp0.io/')
.canister('flvm3-zaaaa-aaaak-qazaq-cai');
console.log("GET OWNER CANISTER");
const volt = await voltFactoryAPI.getOwnerCanister(identity.getPrincipal());
if (volt.length) {
newVoltPrincipal = volt[0].toText();
Expand Down

0 comments on commit f799552

Please sign in to comment.