From c8c9ea8c9c865f3d64fa0324062614a5428e6c65 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Thu, 22 Sep 2022 02:21:10 +0530 Subject: [PATCH 1/2] updated cast function to take `castData` --- src/dsa.ts | 31 ++++++++++++++++++------------- src/utils/wrap-if-spells.ts | 4 ++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/dsa.ts b/src/dsa.ts index 81862bc5..eeaa8375 100644 --- a/src/dsa.ts +++ b/src/dsa.ts @@ -52,7 +52,7 @@ export interface Instance { * @param {number|string} _d.nonce (optional) txn nonce (mostly for node implementation) */ type CastParams = { - spells: Spells + spells: Spells | string origin?: string } & TransactionCallbacks & Pick @@ -396,14 +396,14 @@ export class DSA { from: await this.internal.getAddress(), origin: this.origin, } - + const mergedParams = Object.assign(defaults, wrapIfSpells(params)) as CastParams - + + const data = this.getData(mergedParams) + if (!mergedParams.from) throw new Error(`Parameter 'from' is not defined.`) if (!mergedParams.to) throw new Error(`Parameter 'to' is not defined.`) - const data = await this.getData(mergedParams) - const transactionConfig = await this.internal.getTransactionConfig({ from: mergedParams.from, to: mergedParams.to, @@ -463,15 +463,20 @@ export class DSA { return transaction } - private async getData(params: { spells: Spells; origin?: string }) { - const encodedSpells = this.internal.encodeSpells(params) + private getData(params: { spells: Spells | string; origin?: string }) { - const contract = new this.web3.eth.Contract(Abi.core.versions[this.instance.version].account, this.instance.address) - const data = contract.methods - .cast(encodedSpells.targets, encodedSpells.spells, params.origin || Addresses.genesis) - .encodeABI() - - return data + if (typeof params.spells == "string") { + return params.spells + } else { + const encodedSpells = this.internal.encodeSpells(params) + + const contract = new this.web3.eth.Contract(Abi.core.versions[this.instance.version].account, this.instance.address) + const data = contract.methods + .cast(encodedSpells.targets, encodedSpells.spells, params.origin || Addresses.genesis) + .encodeABI() + + return data + } } } diff --git a/src/utils/wrap-if-spells.ts b/src/utils/wrap-if-spells.ts index 8f21b5d1..64520967 100644 --- a/src/utils/wrap-if-spells.ts +++ b/src/utils/wrap-if-spells.ts @@ -1,5 +1,5 @@ import { Spells } from '../spells' -export function wrapIfSpells(params: Spells | { spells: Spells }) { - return params instanceof Spells ? { spells: params } : params +export function wrapIfSpells(params: (Spells | string)| { spells: Spells | string }) { + return params instanceof Spells || typeof params === "string" ? { spells: params } : params } From 4463140daa7b86e4b590625ebfc304eb35966059 Mon Sep 17 00:00:00 2001 From: Georges KABBOUCHI Date: Thu, 22 Sep 2022 00:02:46 +0300 Subject: [PATCH 2/2] fix types --- src/dsa.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dsa.ts b/src/dsa.ts index eeaa8375..b16e0799 100644 --- a/src/dsa.ts +++ b/src/dsa.ts @@ -390,7 +390,7 @@ export class DSA { })() } - async cast(params: Spells | CastParams) { + async cast(params: string | Spells | CastParams) { const defaults = { to: this.instance.address, from: await this.internal.getAddress(), @@ -463,12 +463,12 @@ export class DSA { return transaction } - private getData(params: { spells: Spells | string; origin?: string }) { + private getData(params: { spells: Spells | string; origin?: string }) : string { - if (typeof params.spells == "string") { + if (typeof params.spells === "string") { return params.spells } else { - const encodedSpells = this.internal.encodeSpells(params) + const encodedSpells = this.internal.encodeSpells(params as { spells: Spells, origin?: string}) const contract = new this.web3.eth.Contract(Abi.core.versions[this.instance.version].account, this.instance.address) const data = contract.methods