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

Updated cast function #232

Draft
wants to merge 2 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
33 changes: 19 additions & 14 deletions src/dsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionConfig, 'from' | 'to' | 'value' | 'gas' | 'gasPrice' | 'maxFeePerGas' | 'maxPriorityFeePerGas' | 'nonce'>
Expand Down Expand Up @@ -390,20 +390,20 @@ 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(),
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,
Expand Down Expand Up @@ -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 }) : 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 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
.cast(encodedSpells.targets, encodedSpells.spells, params.origin || Addresses.genesis)
.encodeABI()

return data
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/wrap-if-spells.ts
Original file line number Diff line number Diff line change
@@ -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
}