Skip to content

Commit

Permalink
Merge pull request #12 from sonicdex/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
memecakedev authored Jul 11, 2024
2 parents b801e78 + 1a90339 commit 6e5d974
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 46 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface TransactionItem {
export interface TransactionItem {
canisterId: string;
methodName: string;
args: any;
Expand All @@ -11,6 +11,7 @@ type BatchTransactType = {
[key: string]: TransactionItem[];
};
const Artemis = class Artemis {
constructor(connectObj?: { host: string; whitelist: string[] });
accountId: string;
principalId: string;
walletActive: string;
Expand All @@ -25,7 +26,7 @@ const Artemis = class Artemis {
isLoaded(): any;
getWalletBalance(returnType: string): number;
requestICPTransfer(transferRequest: any): any;
getCanisterActor(canisterId: string, idl: any, isAnon: boolean): any;
getCanisterActor(canisterId: string, idl: any, isAnon: boolean , isForced:boolean): any;
batchTransact(transactions: BatchTransactType): any;
trxArray:[TransactionItem[]]
}
Expand All @@ -39,7 +40,7 @@ const BatchTransact = class BatchTransact {
previousStep: string;
activeStep: string;
nextStep: string;
FailedStep: string;
failedSteps: string[];
transactionResults: {};
trxArray:[];
execute(): any;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "artemis-web3-adapter",
"version": "1.0.8",
"version": "1.1.5",
"description": "dfinity wallet adapter for connecting different wallets in IC",
"main": "index.js",
"scripts": {
Expand All @@ -19,7 +19,8 @@
"@fort-major/msq-client": "^0.3.4",
"@fort-major/msq-shared": "^0.3.3",
"buffer": "^6.0.3",
"buffer-crc32": "^1.0.0"
"buffer-crc32": "^1.0.0",
"crypto-js": "^4.2.0"
},
"devDependencies": {
"webpack": "^5.75.0",
Expand Down
27 changes: 17 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Artemis = class Artemis {
anoncanisterActors = [];
connectedWalletInfo = {};
wallets = walletList;
_connectObject={ whitelist: [NNS_CANISTER_ID], host: HOSTURL, }
_connectObject = { whitelist: [NNS_CANISTER_ID], host: HOSTURL, }
_cleanUpConnObj(connectObj) {
connectObj.whitelist.push(NNS_CANISTER_ID);
connectObj.whitelist = Array.from(new Set([...connectObj.whitelist]));
Expand Down Expand Up @@ -70,7 +70,7 @@ export const Artemis = class Artemis {
async getWalletBalance(returnType = "number") {
if (!this.accountId) return 0;
var actor = await this.getCanisterActor(NNS_CANISTER_ID, NNS_IDL, true);
const balance = await actor.icrc1_balance_of({ owner: Principal.from(this.principalId) , subaccount: []});
const balance = await actor.icrc1_balance_of({ owner: Principal.from(this.principalId), subaccount: [] });
if (returnType == 'number') { this.balance = (parseFloat(balance) / ICP_DECIMAL) }
else { this.balance = balance; }
return this.balance
Expand All @@ -84,18 +84,26 @@ export const Artemis = class Artemis {
reject(false)
})
};
async getCanisterActor(canisterId, idl, isAnon = false) {
async getCanisterActor(canisterId, idl, isAnon = false, isForced = false) {
let actor = false;
if (isAnon) {
if (this.anoncanisterActors[canisterId])
if (isForced) {
const pubAgent = new HttpAgent({ AnonymousIdentity, host: this._connectObject.host });
actor = await Actor.createActor(idl, { agent: pubAgent, canisterId: canisterId })
this.anoncanisterActors[canisterId] = actor;
} else if (this.anoncanisterActors[canisterId])
actor = this.anoncanisterActors[canisterId]
else {
const pubAgent = new HttpAgent({ AnonymousIdentity, host: HOSTURL });
const pubAgent = new HttpAgent({ AnonymousIdentity, host: this._connectObject.host });
actor = await Actor.createActor(idl, { agent: pubAgent, canisterId: canisterId })
this.anoncanisterActors[canisterId] = actor;
}
} else {
if (this.canisterActors[canisterId]) {
if (isForced) {
actor = await this.provider.createActor({ canisterId: canisterId, interfaceFactory: idl });
this.canisterActors[canisterId] = actor;
}
else if (this.canisterActors[canisterId]) {
actor = this.canisterActors[canisterId];
} else {
actor = await this.provider.createActor({ canisterId: canisterId, interfaceFactory: idl });
Expand Down Expand Up @@ -127,11 +135,10 @@ export const BatchTransact = BatchTransaction;

export const principalIdFromHex = getAccountIdentifier;

export const ArtemisAdapter = new Artemis({ whitelist: [NNS_CANISTER_ID], host: HOSTURL });
export const ArtemisAdapter = new Artemis({ whitelist: [NNS_CANISTER_ID], host: HOSTURL });

if (window) {
const artemis = new Artemis({ whitelist: [NNS_CANISTER_ID], host: HOSTURL });
window.artemis = artemis;
window.artemis = Artemis;
window.artemis.BatchTransact = BatchTransaction;
window.artemis.dfinity = { AnonymousIdentity, Principal }
window.artemis.dfinity = { AnonymousIdentity, Principal }
}
69 changes: 40 additions & 29 deletions src/libs/batchTransact.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export const BatchTransaction = class BatchTransaction {
stepsList = [];
completed = [];
activeStep = '';
FailedSteps = [];
failedSteps = [];
transactionResults = {};
trxArray = [];
_info= false;
_info = false;
_adapterObj = false;
constructor(transactionLlist = {}, _adapterObj) {
if (!_adapterObj || !_adapterObj.provider) return false;
Expand All @@ -32,34 +32,43 @@ export const BatchTransaction = class BatchTransaction {
var trxIndex = 0;
self.trxArray.forEach((subArray, i) => {
subArray.forEach((el, j) => {

self.trxArray[i][j].stepIndex = trxIndex;
self.trxArray[i][j].state = 'idle';
self.trxArray[i][j].onSuccess = async (data) => {
const stepIndex = self.trxArray[i][j].stepIndex;
self.trxArray[i][j].onSuccessMain = async (data, _this) => {
const stepIndex = _this.stepIndex;
const onSucessCall = el.onSuccess;
const onFailCall = el.onFail

if (data.err || data.Err || data.ERR) {
self.FailedSteps.push(self.stepsList[stepIndex]);
self.failedSteps.push(self.stepsList[stepIndex]);
self.transactionResults[self.stepsList[stepIndex]] = data;
self.state = 'error';
self.trxArray[i][j].state = 'error';
_this.state = 'error';
if (onFailCall) await onFailCall(data)
return false;
} else {
self.completed.push(self.stepsList[stepIndex]);
self.activeStep = self.stepsList[stepIndex + 1];
self.transactionResults[self.stepsList[stepIndex]] = data;
self.trxArray[i][j].state = 'done';
_this.state = 'done';
}
if (self.trxArray[i][j].updateNextStep && self.trxArray[(i + 1)]){
await self.trxArray[i][j].updateNextStep(data, self.trxArray[(i + 1)][0]);
}
if (_this.updateNextStep && self.trxArray[(i + 1)]) {
await _this.updateNextStep(data, self.trxArray[(i + 1)][0]);
}
if (onSucessCall) await onSucessCall(data)
};
self.trxArray[i][j].onFail = (err) => {
const stepIndex = self.trxArray[i][j].stepIndex;
console.error(`error in ${ self.stepsList[stepIndex]} ` , self.trxArray[i][j])

self.trxArray[i][j].onFailMain = async (err, _this) => {
const onFailCall = el.onFail
const stepIndex = _this.stepIndex;;
console.error(`error in ${self.stepsList[stepIndex]} `, self.trxArray[i][j])
console.error(err);
self.FailedSteps.push(self.stepsList[stepIndex]);
self.failedSteps.push(self.stepsList[stepIndex]);
self.activeStep = self.stepsList[stepIndex];
self.state = 'error';
self.trxArray[i][j].state = 'error';
_this.state = 'error';
if (onFailCall) await onFailCall(err)
return false;
}
trxIndex++;
Expand All @@ -71,7 +80,8 @@ export const BatchTransaction = class BatchTransaction {
if (this.state != "error") return false;
this.trxArray = this.trxArray.map(innerArray => innerArray.filter(item => item.state !== 'done'));
this.state = 'running';
this.FailedSteps = [];
this._info = '';
this.failedSteps = [];
var data = await this._processBatch();
return data;
}
Expand All @@ -80,46 +90,46 @@ export const BatchTransaction = class BatchTransaction {
if (this.state == 'running' || !this._adapterObj || Object.keys(this.transactionLlist).length == 0) return false;
if (this.state == 'done') return this.transactionResults;
this.state = 'running';
this.FailedSteps = [];
this.failedSteps = [];
this.trxArray = this._prepareTrxArry();
return await this._processBatch();
}
async _processBatch() {
if (!this.trxArray.length) return false;
var self = this;
self.activeStep = self.completed.length > 0 ? self.stepsList[self.completed.length] : self.stepsList[0];
if (['bitfinity','plug'].includes(this._adapterObj.walletActive)) {
if (['bitfinity'].includes(this._adapterObj.walletActive)) {
for (const trxStepItem of self.trxArray) {
if (self.state == 'error' || self.state == 'done') break;
if (trxStepItem.length)
var resp = await this._adapterObj.provider.batchTransactions(trxStepItem);
}
if (self.FailedSteps.length == 0) {
if (self.failedSteps.length == 0) {
self.state = 'done';
return self.transactionResults;
} else {
self.state = 'error';
return false;
}
} else if (['stoic', 'dfinity', 'astrox','metamask','nfid'].includes(this._adapterObj.walletActive)) {
} else if (['plug', 'stoic', 'dfinity', 'astrox', 'metamask', 'nfid'].includes(this._adapterObj.walletActive)) {
try {
for (const trxStepItem of self.trxArray) {
if (self.state == 'error' || self.state == 'done') break;
if (trxStepItem.length){
if (trxStepItem.length) {
for (const trxItem of trxStepItem) {
if (self.state == 'error' || self.state == 'done') break;
var actor = await self._adapterObj.getCanisterActor(trxItem.canisterId, trxItem.idl);
var actor = await self._adapterObj.getCanisterActor(trxItem.canisterId, trxItem.idl, false, true);
var resp = false;
if(trxItem.methodName){
if (trxItem.args) { resp = await actor[trxItem.methodName](...trxItem.args);}
else { resp = await actor[trxItem.methodName]();}
}else { await trxItem.onFail(resp);}
if (resp) { await trxItem.onSuccess(resp); }
else { await trxItem.onFail(resp); }
if (trxItem.methodName) {
if (trxItem.args) { resp = await actor[trxItem.methodName](...trxItem.args); }
else { resp = await actor[trxItem.methodName](); }
} else { await trxItem.onFailMain(resp); }
if (resp) { await trxItem.onSuccessMain(resp, trxItem); }
else { await trxItem.onFailMain(resp, trxItem); }
}
}
}
if (self.FailedSteps.length == 0) {
if (self.failedSteps.length == 0) {
self.state = 'done';
return self.transactionResults;
} else {
Expand All @@ -128,6 +138,7 @@ export const BatchTransaction = class BatchTransaction {
}
} catch (error) {
self.state = 'error';
console.error(error);
self._info = error;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallets/plug.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const plug = {
var publicKey = false, prinObj = false;
var isConnected = false;
const timeoutPromise = new Promise((resolve) => { setTimeout(() => { resolve(false); }, 3000) });
isConnected = await Promise.race([window.ic.plug.isConnected(), timeoutPromise]);
isConnected = await window.ic.plug.isConnected(); //Promise.race([window.ic.plug.isConnected(), timeoutPromise]);

try {
if (isConnected) {
Expand Down

0 comments on commit 6e5d974

Please sign in to comment.