1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

fixed some typing issues

This commit is contained in:
Pedro Gutiérrez 2019-03-01 13:24:04 +01:00 committed by Pedro Gutiérrez
parent 5ec624b2a5
commit c9b383223b
13 changed files with 33 additions and 41 deletions

View File

@ -10,8 +10,8 @@ if (process.env.SEED_WORDS) {
seedphrase, seedphrase,
configJson.nodeUri, configJson.nodeUri,
0, 0,
2, 5,
) )
} }
export const config = configJson as Config export const config: Config = configJson as any

View File

@ -7,5 +7,5 @@
"threshold": 0, "threshold": 0,
"password": "secret", "password": "secret",
"address": "0xa99d43d86a0758d5632313b8fa3972b6088a21bb", "address": "0xa99d43d86a0758d5632313b8fa3972b6088a21bb",
"verbose": false "verbose": "error"
} }

View File

@ -69,8 +69,6 @@
"save-file": "^2.3.1", "save-file": "^2.3.1",
"uuid": "^3.3.2", "uuid": "^3.3.2",
"web3": "1.0.0-beta.37", "web3": "1.0.0-beta.37",
"web3-eth-contract": "1.0.0-beta.37",
"web3-utils": "1.0.0-beta.37",
"whatwg-url": "^7.0.0" "whatwg-url": "^7.0.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,11 +1,11 @@
import Contract from "web3-eth-contract" import { Contract } from "web3-eth-contract"
import Logger from "../utils/Logger" import Logger from "../utils/Logger"
import Keeper from "./Keeper" import Keeper from "./Keeper"
import Web3Provider from "./Web3Provider" import Web3Provider from "./Web3Provider"
export default class ContractHandler { export default class ContractHandler {
public static async get(what: string): Contract { public static async get(what: string): Promise<Contract> {
const where = (await (await Keeper.getInstance()).getNetworkName()).toLowerCase() const where = (await (await Keeper.getInstance()).getNetworkName()).toLowerCase()
try { try {
return ContractHandler.contracts.get(what) || await ContractHandler.load(what, where) return ContractHandler.contracts.get(what) || await ContractHandler.load(what, where)

View File

@ -1,6 +1,5 @@
import Event from "web3" import { Contract } from "web3-eth-contract"
import Contract from "web3-eth-contract" import { TransactionReceipt } from "web3-core"
import {Receipt} from "web3-utils"
import Logger from "../../utils/Logger" import Logger from "../../utils/Logger"
import ContractHandler from "../ContractHandler" import ContractHandler from "../ContractHandler"
import Web3Provider from "../Web3Provider" import Web3Provider from "../Web3Provider"
@ -16,7 +15,7 @@ export default abstract class ContractBase {
this.contractName = contractName this.contractName = contractName
} }
public async getEventData(eventName: any, options: any): Promise<Event[]> { public async getEventData(eventName: any, options: any) {
if (!this.contract.events[eventName]) { if (!this.contract.events[eventName]) {
throw new Error(`Event "${eventName}" not found on contract "${this.contractName}"`) throw new Error(`Event "${eventName}" not found on contract "${this.contractName}"`)
} }
@ -41,14 +40,14 @@ export default abstract class ContractBase {
this.contract = await ContractHandler.get(this.contractName) this.contract = await ContractHandler.get(this.contractName)
} }
protected async sendFrom(name: string, args: any[], from?: string): Promise<Receipt> { protected async sendFrom(name: string, args: any[], from?: string): Promise<TransactionReceipt> {
if (!from) { if (!from) {
from = (await Web3Provider.getWeb3().eth.getAccounts())[0] from = (await Web3Provider.getWeb3().eth.getAccounts())[0]
} }
return this.send(name, from, args) return this.send(name, from, args)
} }
protected async send(name: string, from: string, args: any[]): Promise<Receipt> { protected async send(name: string, from: string, args: any[]): Promise<TransactionReceipt> {
if (!this.contract.methods[name]) { if (!this.contract.methods[name]) {
throw new Error(`Method "${name}" is not part of contract "${this.contractName}"`) throw new Error(`Method "${name}" is not part of contract "${this.contractName}"`)
} }
@ -95,12 +94,10 @@ export default abstract class ContractBase {
} }
} }
private searchMethod(methodName): any { private searchMethod(methodName: string) {
const foundMethod = this.contract.options.jsonInterface.find((method) => { const foundMethod = this.contract.options.jsonInterface
if (method.name === methodName) { .map(method => ({...method, signature: (method as any).signature}))
return method .find((method: any) => method.name === methodName)
}
})
if (!foundMethod) { if (!foundMethod) {
throw new Error(`Method "${methodName}" is not part of contract "${this.contractName}"`) throw new Error(`Method "${methodName}" is not part of contract "${this.contractName}"`)
} }

View File

@ -1,4 +1,3 @@
import {Receipt} from "web3-utils"
import Web3Provider from "../Web3Provider" import Web3Provider from "../Web3Provider"
import ContractBase from "./ContractBase" import ContractBase from "./ContractBase"
@ -10,7 +9,7 @@ export default class DIDRegistry extends ContractBase {
return didRegistry return didRegistry
} }
public async registerAttribute(did: string, checksum: string, value: string, ownerAddress: string): Promise<Receipt> { public async registerAttribute(did: string, checksum: string, value: string, ownerAddress: string) {
return this.send("registerAttribute", ownerAddress, ["0x" + did, Web3Provider.getWeb3().utils.fromAscii(checksum), value]) return this.send("registerAttribute", ownerAddress, ["0x" + did, Web3Provider.getWeb3().utils.fromAscii(checksum), value])
} }

View File

@ -1,4 +1,3 @@
import {Receipt} from "web3-utils"
import ContractBase from "./ContractBase" import ContractBase from "./ContractBase"
export default class Dispenser extends ContractBase { export default class Dispenser extends ContractBase {
@ -9,7 +8,7 @@ export default class Dispenser extends ContractBase {
return market return market
} }
public async requestTokens(amount: number, receiverAddress: string): Promise<Receipt> { public async requestTokens(amount: number, receiverAddress: string) {
return this.send("requestTokens", receiverAddress, [amount]) return this.send("requestTokens", receiverAddress, [amount])
} }
} }

View File

@ -1,5 +1,4 @@
import BigNumber from "bignumber.js" import BigNumber from "bignumber.js"
import {Receipt} from "web3-utils"
import ContractBase from "./ContractBase" import ContractBase from "./ContractBase"
export default class OceanToken extends ContractBase { export default class OceanToken extends ContractBase {
@ -10,8 +9,8 @@ export default class OceanToken extends ContractBase {
return token return token
} }
public async approve(marketAddress: string, price: number, buyerAddress: string): Promise<Receipt> { public async approve(to: string, price: number, from?: string) {
return this.send("approve", buyerAddress, [marketAddress, price]) return this.sendFrom("approve", [to, price], from)
} }
public async balanceOf(address: string): Promise<number> { public async balanceOf(address: string): Promise<number> {
@ -19,7 +18,7 @@ export default class OceanToken extends ContractBase {
.then((balance: string) => new BigNumber(balance).toNumber()) .then((balance: string) => new BigNumber(balance).toNumber())
} }
public async transfer(to: string, amount: number, from: string): Promise<boolean> { public async transfer(to: string, amount: number, from: string) {
return this.send("transfer", from, [to, amount]) return this.send("transfer", from, [to, amount])
} }
} }

View File

@ -24,20 +24,20 @@ export abstract class Condition extends ContractBase {
return this.call("hashValues", args) return this.call("hashValues", args)
} }
fulfill(agreementId: string, ...args: any[]): Promise<ConditionState> fulfill(agreementId: string, ...args: any[])
fulfill(agreementId: string, args: any[], from?: string): Promise<ConditionState> { fulfill(agreementId: string, args: any[], from?: string) {
return this.sendFrom("fulfill", args, from) return this.sendFrom("fulfill", [agreementId, ...args], from)
} }
async generateIdHash(agreementId: string, ...values: any[]): Promise<string> { async generateIdHash(agreementId: string, ...values: any[]) {
return this.generateId(agreementId, await this.hashValues(...values)) return this.generateId(agreementId, await this.hashValues(...values))
} }
generateId(agreementId: string, valueHash: string): Promise<string> { generateId(agreementId: string, valueHash: string) {
return this.call("generateId", [agreementId, valueHash]) return this.call<string>("generateId", [agreementId, valueHash])
} }
abortByTimeOut(agreementId: string, from?: string): Promise<ConditionState> { abortByTimeOut(agreementId: string, from?: string) {
return this.sendFrom("requestTokens", [agreementId], from) return this.sendFrom("requestTokens", [agreementId], from)
} }
} }

View File

@ -32,9 +32,10 @@ export default class OceanTokens {
* @return {Promise<boolean>} Success, * @return {Promise<boolean>} Success,
*/ */
public async transfer(to: string, amount: number, from: Account): Promise<boolean> { public async transfer(to: string, amount: number, from: Account): Promise<boolean> {
return (await Keeper.getInstance()) (await Keeper.getInstance())
.token .token
.transfer(to, amount, from.getId()) .transfer(to, amount, from.getId())
return true
} }
/** /**

View File

@ -1,4 +1,3 @@
import ConfigProvider from "../../ConfigProvider"
import { Condition } from "../../ddo/Condition" import { Condition } from "../../ddo/Condition"
import { DDO } from "../../ddo/DDO" import { DDO } from "../../ddo/DDO"
import { ServiceAccess } from "../../ddo/Service" import { ServiceAccess } from "../../ddo/Service"
@ -93,7 +92,7 @@ export default class ServiceAgreement extends OceanBase {
let serviceAgreementHashSignature: string let serviceAgreementHashSignature: string
const web3 = Web3Provider.getWeb3() const web3 = Web3Provider.getWeb3()
if (web3.currentProvider.isMetaMask) { if ((web3 as any).currentProvider.isMetaMask) {
// password is injected by metamask, dont try to set it! // password is injected by metamask, dont try to set it!
serviceAgreementHashSignature = await web3.eth.personal.sign(serviceAgreementHash, consumer.getId(), null) serviceAgreementHashSignature = await web3.eth.personal.sign(serviceAgreementHash, consumer.getId(), null)
} else { } else {
@ -163,7 +162,7 @@ export default class ServiceAgreement extends OceanBase {
private static hashValuePairArray(valuePairs: ValuePair[]): string { private static hashValuePairArray(valuePairs: ValuePair[]): string {
let hash: string let hash: string
try { try {
hash = Web3Provider.getWeb3().utils.soliditySha3(...valuePairs).toString("hex") hash = (Web3Provider as any).getWeb3().utils.soliditySha3(...valuePairs).toString("hex")
} catch (err) { } catch (err) {
Logger.error(`Hashing of ${JSON.stringify(valuePairs, null, 2)} failed.`) Logger.error(`Hashing of ${JSON.stringify(valuePairs, null, 2)} failed.`)
throw err throw err
@ -192,7 +191,7 @@ export default class ServiceAgreement extends OceanBase {
{type: "bytes32", value: "0x" + serviceAgreementId} as ValuePair, {type: "bytes32", value: "0x" + serviceAgreementId} as ValuePair,
] ]
return Web3Provider.getWeb3().utils.soliditySha3(...args).toString("hex") return (Web3Provider as any).getWeb3().utils.soliditySha3(...args).toString("hex")
} }
private static getTimeoutValuesFromService(service: ServiceAccess): number[] { private static getTimeoutValuesFromService(service: ServiceAccess): number[] {

View File

@ -1,4 +1,3 @@
import ConfigProvider from "../../ConfigProvider"
import { Condition as DDOCondition, Dependency, Parameter } from "../../ddo/Condition" import { Condition as DDOCondition, Dependency, Parameter } from "../../ddo/Condition"
import { MetaData } from "../../ddo/MetaData" import { MetaData } from "../../ddo/MetaData"
import ContractReflector from "../../keeper/ContractReflector" import ContractReflector from "../../keeper/ContractReflector"
@ -21,7 +20,7 @@ export default class ServiceAgreementTemplate extends OceanBase {
{type: "address", value: methodReflection.address} as ValuePair, {type: "address", value: methodReflection.address} as ValuePair,
{type: "bytes4", value: methodReflection.signature} as ValuePair, {type: "bytes4", value: methodReflection.signature} as ValuePair,
] ]
return Web3Provider.getWeb3().utils.soliditySha3(...values).toString("hex") return (Web3Provider as any).getWeb3().utils.soliditySha3(...values).toString("hex")
} }
public constructor(private template: TemplateBase) { public constructor(private template: TemplateBase) {

View File

@ -3,6 +3,7 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"moduleResolution": "node", "moduleResolution": "node",
"lib": [ "lib": [
"es2017",
"es6", "es6",
"es7" "es7"
], ],