|
| 1 | +import assert from 'assert'; |
| 2 | +import { BigNumber } from '@ethersproject/bignumber'; |
| 3 | +import { Abi } from './types'; |
| 4 | +import { getSelectorFromName } from './utils'; |
| 5 | +import { addTransaction } from './starknet'; |
| 6 | + |
| 7 | +type Args = { [inputName: string]: string | string[] }; |
| 8 | +type Calldata = string[]; |
| 9 | + |
| 10 | +const parseFelt = (candidate: string): BigNumber => { |
| 11 | + try { |
| 12 | + return BigNumber.from(candidate); |
| 13 | + } catch (e) { |
| 14 | + throw Error('Couldnt parse felt'); |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +const isFelt = (candidate: string): boolean => { |
| 19 | + try { |
| 20 | + parseFelt(candidate); |
| 21 | + return true; |
| 22 | + } catch (e) { |
| 23 | + return false; |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +export class Contract { |
| 28 | + connectedTo: string | null = null; |
| 29 | + |
| 30 | + abi: Abi[]; |
| 31 | + |
| 32 | + /** |
| 33 | + * Contract class to handle contract methods |
| 34 | + * |
| 35 | + * @param abi - Abi of the contract object |
| 36 | + * @param address (optional) - address to connect to |
| 37 | + */ |
| 38 | + constructor(abi: Abi[], address: string | null = null) { |
| 39 | + this.connectedTo = address; |
| 40 | + this.abi = abi; |
| 41 | + } |
| 42 | + |
| 43 | + public connect(address: string): Contract { |
| 44 | + this.connectedTo = address; |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + private static compileCalldata(args: Args): Calldata { |
| 49 | + return Object.values(args).flatMap((value) => { |
| 50 | + if (Array.isArray(value)) |
| 51 | + return [ |
| 52 | + BigNumber.from(value.length).toString(), |
| 53 | + ...value.map((x) => BigNumber.from(x).toString()), |
| 54 | + ]; |
| 55 | + return BigNumber.from(value).toString(); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + public invoke(method: string, args: Args = {}) { |
| 60 | + // ensure contract is connected |
| 61 | + assert(this.connectedTo !== null, 'contract isnt connected to an address'); |
| 62 | + |
| 63 | + // ensure provided method exists |
| 64 | + const invokeableFunctionNames = this.abi |
| 65 | + .filter((abi) => { |
| 66 | + const isView = abi.stateMutability === 'view'; |
| 67 | + const isFunction = abi.type === 'function'; |
| 68 | + return isFunction && !isView; |
| 69 | + }) |
| 70 | + .map((abi) => abi.name); |
| 71 | + assert(invokeableFunctionNames.includes(method), 'invokeable method not found in abi'); |
| 72 | + |
| 73 | + // ensure args match abi type |
| 74 | + const methodAbi = this.abi.find((abi) => abi.name === method)!; |
| 75 | + methodAbi.inputs.forEach((input) => { |
| 76 | + assert(args[input.name] !== undefined, `no arg for "${input.name}" provided`); |
| 77 | + if (input.type === 'felt') { |
| 78 | + assert(typeof args[input.name] === 'string', `arg ${input.name} should be a felt (string)`); |
| 79 | + assert( |
| 80 | + isFelt(args[input.name] as string), |
| 81 | + `arg ${input.name} should be decimal or hexadecimal` |
| 82 | + ); |
| 83 | + } else { |
| 84 | + assert(Array.isArray(args[input.name]), `arg ${input.name} should be a felt* (string[])`); |
| 85 | + (args[input.name] as string[]).forEach((felt, i) => { |
| 86 | + assert( |
| 87 | + typeof felt === 'string', |
| 88 | + `arg ${input.name}[${i}] should be a felt (string) as part of a felt* (string[])` |
| 89 | + ); |
| 90 | + assert( |
| 91 | + isFelt(felt), |
| 92 | + `arg ${input.name}[${i}] should be decimal or hexadecimal as part of a felt* (string[])` |
| 93 | + ); |
| 94 | + }); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + // compile calldata |
| 99 | + const entrypointSelector = getSelectorFromName(method); |
| 100 | + const calldata = Contract.compileCalldata(args); |
| 101 | + |
| 102 | + return addTransaction({ |
| 103 | + type: 'INVOKE_FUNCTION', |
| 104 | + contract_address: this.connectedTo, |
| 105 | + calldata, |
| 106 | + entry_point_selector: entrypointSelector, |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
0 commit comments