mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
interface changes, web3 migration
This commit is contained in:
parent
90a99e4dd3
commit
bd06445b82
54
src/Ocean.ts
54
src/Ocean.ts
@ -1,54 +0,0 @@
|
||||
import Keeper from "./keeper/Keeper"
|
||||
import Web3Helper from "./keeper/Web3Helper"
|
||||
import MetaData from "./metadata"
|
||||
import Config from "./models/Config"
|
||||
import Asset from "./ocean/Asset"
|
||||
import Order from "./ocean/Order"
|
||||
|
||||
export default class Ocean {
|
||||
|
||||
public static async getInstance(config) {
|
||||
const ocean = new Ocean(config)
|
||||
ocean.keeper = await Keeper.getInstance(config, ocean.helper)
|
||||
ocean.order = new Order(ocean.keeper)
|
||||
ocean.asset = new Asset(ocean.keeper)
|
||||
return ocean
|
||||
}
|
||||
|
||||
public order: Order
|
||||
public asset: Asset
|
||||
public helper: Web3Helper
|
||||
public metadata: MetaData
|
||||
|
||||
private keeper: Keeper
|
||||
private config: Config
|
||||
|
||||
private constructor(config: Config) {
|
||||
|
||||
this.config = config
|
||||
|
||||
this.helper = new Web3Helper(config)
|
||||
this.metadata = new MetaData(config)
|
||||
}
|
||||
|
||||
// Transactions with gas cost
|
||||
public async requestTokens(amount: number, receiver: string): Promise<boolean> {
|
||||
return this.keeper.market.requestTokens(amount, receiver)
|
||||
}
|
||||
|
||||
public async getAccounts() {
|
||||
const {token} = this.keeper
|
||||
const {helper} = this
|
||||
|
||||
return Promise.all((await helper.getAccounts()).map(async (account: string) => {
|
||||
// await ocean.market.requestTokens(account, 1000)
|
||||
return {
|
||||
name: account,
|
||||
balance: {
|
||||
eth: await token.getEthBalance(account),
|
||||
ocn: await token.getTokenBalance(account),
|
||||
},
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import BigNumber from "bignumber.js"
|
||||
import {Receipt} from "web3-utils"
|
||||
import Asset from "../models/Asset"
|
||||
import Config from "../models/Config"
|
||||
import ContractBaseWrapper from "./ContractWrapperBase"
|
||||
@ -13,26 +14,31 @@ export default class OceanAuth extends ContractBaseWrapper {
|
||||
}
|
||||
|
||||
public async getOrderStatus(orderId: string): Promise<number> {
|
||||
return this.contract.statusOfAccessRequest.call(orderId)
|
||||
return this.contract.statusOfAccessRequest(orderId)
|
||||
.call()
|
||||
.then((status: BigNumber) => status.toNumber())
|
||||
}
|
||||
|
||||
public async cancelAccessRequest(orderId: string, senderAddress: string) {
|
||||
return this.contract.cancelAccessRequest.send(orderId, {
|
||||
public async cancelAccessRequest(orderId: string, senderAddress: string): Promise<Receipt> {
|
||||
return this.contract.cancelAccessRequest(orderId)
|
||||
.send({
|
||||
from: senderAddress,
|
||||
})
|
||||
}
|
||||
|
||||
public async getEncryptedAccessToken(orderId: string, senderAddress: string) {
|
||||
return this.contract.getEncryptedAccessToken.send(orderId, {
|
||||
public async getEncryptedAccessToken(orderId: string, senderAddress: string): Promise<Receipt> {
|
||||
return this.contract.getEncryptedAccessToken(orderId)
|
||||
.send({
|
||||
from: senderAddress,
|
||||
})
|
||||
}
|
||||
|
||||
public async initiateAccessRequest(asset: Asset, publicKey: string, timeout, buyerAddress: string) {
|
||||
return this.contract.initiateAccessRequest.send(
|
||||
asset.assetId, asset.publisherId, publicKey, timeout, {
|
||||
from: buyerAddress, gas: this.config.defaultGas,
|
||||
public async initiateAccessRequest(asset: Asset, publicKey: string,
|
||||
timeout, buyerAddress: string): Promise<Receipt> {
|
||||
return this.contract.initiateAccessRequest(asset.assetId, asset.publisherId, publicKey, timeout)
|
||||
.send({
|
||||
from: buyerAddress,
|
||||
gas: this.config.defaultGas,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
import {Contract} from "web3-utils"
|
||||
import Contract from "web3-eth-contract"
|
||||
import Logger from "../utils/Logger"
|
||||
import Web3Helper from "./Web3Helper"
|
||||
|
||||
const contracts: Map<string, object> = new Map<string, object>()
|
||||
const contracts: Map<string, Contract> = new Map<string, Contract>()
|
||||
|
||||
export default class ContractHandler {
|
||||
|
||||
public static async get(what: string, web3Helper: Web3Helper) {
|
||||
public static async get(what: string, web3Helper: Web3Helper): Contract {
|
||||
return contracts.get(what) || await ContractHandler.load(what, web3Helper)
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ export default class ContractHandler {
|
||||
})
|
||||
}
|
||||
|
||||
private static async load(what: string, web3Helper: Web3Helper): Promise<object> {
|
||||
private static async load(what: string, web3Helper: Web3Helper): Promise<Contract> {
|
||||
const where = (await web3Helper.getNetworkName()).toLowerCase()
|
||||
Logger.log("Loading", what, "from", where)
|
||||
try {
|
||||
@ -79,7 +79,12 @@ export default class ContractHandler {
|
||||
|
||||
private static async deployContract(web3, name, from, params?): Promise<Contract> {
|
||||
|
||||
let contractInstance
|
||||
// dont redeploy if there is already something loaded
|
||||
if (contracts.has(name)) {
|
||||
return contracts.get(name)
|
||||
}
|
||||
|
||||
let contractInstance: Contract
|
||||
try {
|
||||
Logger.log("Deploying", name)
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
import Contract from "web3-eth-contract"
|
||||
import Config from "../models/Config"
|
||||
import Logger from "../utils/Logger"
|
||||
import ContractHandler from "./ContractHandler"
|
||||
@ -5,7 +6,7 @@ import Web3Helper from "./Web3Helper"
|
||||
|
||||
export default class ContractWrapperBase {
|
||||
|
||||
protected contract: any = null
|
||||
protected contract: Contract = null
|
||||
protected config: Config
|
||||
protected web3Helper: Web3Helper
|
||||
|
||||
@ -53,7 +54,7 @@ export default class ContractWrapperBase {
|
||||
}
|
||||
|
||||
public getAddress() {
|
||||
return this.contract.address
|
||||
return this.contract.options.address
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import BigNumber from "bignumber.js"
|
||||
import {Receipt} from "web3-utils"
|
||||
import Asset from "../models/Asset"
|
||||
import Config from "../models/Config"
|
||||
import Order from "../models/Order"
|
||||
@ -16,39 +18,41 @@ export default class OceanMarket extends ContractWrapperBase {
|
||||
|
||||
// call functions (costs no gas)
|
||||
public async isAssetActive(assetId: string): Promise<boolean> {
|
||||
return this.contract.methods.checkAsset(assetId).call
|
||||
return this.contract.methods.checkAsset(assetId).call()
|
||||
}
|
||||
|
||||
public async verifyOrderPayment(orderId: string): Promise<boolean> {
|
||||
return this.contract.methods.verifyPaymentReceived(orderId).call
|
||||
return this.contract.methods.verifyPaymentReceived(orderId).call()
|
||||
}
|
||||
|
||||
public async getAssetPrice(assetId: string): Promise<number> {
|
||||
return this.contract.methods.getAssetPrice(assetId).call().then((result) => result.toNumber())
|
||||
return this.contract.methods.getAssetPrice(assetId)
|
||||
.call()
|
||||
.then((price: string) => new BigNumber(price).toNumber())
|
||||
}
|
||||
|
||||
public async requestTokens(amount: number, receiverAddress: string): Promise<boolean> {
|
||||
return this.contract.methods.requestTokens(amount).send({
|
||||
public async requestTokens(amount: number, receiverAddress: string): Promise<Receipt> {
|
||||
return this.contract.methods.requestTokens(amount)
|
||||
.send({
|
||||
from: receiverAddress,
|
||||
})
|
||||
}
|
||||
|
||||
public async registerAsset(name: string, description: string,
|
||||
price: number, publisherAddress: string): Promise<string> {
|
||||
const assetId = await this.contract.methods.generateId(name + description).call()
|
||||
Logger.log("Registering: ", assetId)
|
||||
const result = await this.contract.methods.register(assetId, price).send({
|
||||
from: publisherAddress,
|
||||
gas: this.config.defaultGas,
|
||||
},
|
||||
)
|
||||
Logger.log("Registered: ", result)
|
||||
return assetId
|
||||
public async generateId(input: string): Promise<string> {
|
||||
return await this.contract.methods.generateId(input).call()
|
||||
}
|
||||
|
||||
public async payAsset(asset: Asset, order: Order, buyerAddress: string): Promise<boolean> {
|
||||
Logger.log("Sending payment")
|
||||
return this.contract.methods.sendPayment(order.id, asset.publisherId, asset.price, order.timeout).send({
|
||||
public async register(assetId: string, price: number, publisherAddress: string): Promise<Receipt> {
|
||||
return await this.contract.methods.register(assetId, price)
|
||||
.send({
|
||||
from: publisherAddress,
|
||||
gas: this.config.defaultGas,
|
||||
})
|
||||
}
|
||||
|
||||
public async payAsset(asset: Asset, order: Order, buyerAddress: string): Promise<Receipt> {
|
||||
return this.contract.methods.sendPayment(order.id, asset.publisherId, asset.price, order.timeout)
|
||||
.send({
|
||||
from: buyerAddress,
|
||||
gas: this.config.defaultGas,
|
||||
})
|
||||
|
@ -1,3 +1,5 @@
|
||||
import BigNumber from "bignumber.js"
|
||||
import {Receipt} from "web3-utils"
|
||||
import Config from "../models/Config"
|
||||
import ContractBaseWrapper from "./ContractWrapperBase"
|
||||
import Web3Helper from "./Web3Helper"
|
||||
@ -10,27 +12,17 @@ export default class OceanToken extends ContractBaseWrapper {
|
||||
return token
|
||||
}
|
||||
|
||||
public async getTokenBalance(accountAddress: string) {
|
||||
return this.contract.methods.balanceOf(accountAddress).call()
|
||||
}
|
||||
|
||||
public async getEthBalance(account: string): Promise<number> {
|
||||
return new Promise<number>((resolve, reject) => {
|
||||
// Logger.log("getting balance for", account);
|
||||
this.web3Helper.getWeb3().eth.getBalance(account, "latest", (err: any, balance: number) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
// Logger.log("balance", balance);
|
||||
resolve(balance)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
public async approve(marketAddress: string, price: number, buyerAddress: string) {
|
||||
return this.contract.methods.approve(marketAddress, price).send({
|
||||
public async approve(marketAddress: string, price: number, buyerAddress: string): Promise<Receipt> {
|
||||
return this.contract.methods.approve(marketAddress, price)
|
||||
.send({
|
||||
from: buyerAddress,
|
||||
gas: this.config.defaultGas,
|
||||
})
|
||||
}
|
||||
|
||||
public async balanceOf(address: string): Promise<number> {
|
||||
return this.contract.methods.balanceOf(address)
|
||||
.call()
|
||||
.then((balance: string) => new BigNumber(balance).toNumber())
|
||||
}
|
||||
}
|
||||
|
46
src/ocean/Account.ts
Normal file
46
src/ocean/Account.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import BigNumber from "bignumber.js"
|
||||
import Keeper from "../keeper/Keeper"
|
||||
import Web3Helper from "../keeper/Web3Helper"
|
||||
|
||||
export default class Account {
|
||||
|
||||
private keeper: Keeper
|
||||
|
||||
constructor(keeper: Keeper) {
|
||||
this.keeper = keeper
|
||||
}
|
||||
|
||||
public async getTokenBalance(accountAddress: string): Promise<number> {
|
||||
return this.keeper.token.balanceOf(accountAddress)
|
||||
}
|
||||
|
||||
public async getEthBalance(account: string): Promise<number> {
|
||||
const {web3Helper} = this.keeper
|
||||
// Logger.log("getting balance for", account);
|
||||
return web3Helper.getWeb3().eth.getBalance(account, "latest")
|
||||
.then((balance: string) => {
|
||||
// Logger.log("balance", balance);
|
||||
return new BigNumber(balance).toNumber()
|
||||
})
|
||||
}
|
||||
|
||||
public async list() {
|
||||
const {web3Helper} = this.keeper
|
||||
|
||||
return Promise.all((await web3Helper.getAccounts()).map(async (account: string) => {
|
||||
// await ocean.market.requestTokens(account, 1000)
|
||||
return {
|
||||
name: account,
|
||||
balance: {
|
||||
eth: await this.getEthBalance(account),
|
||||
ocn: await this.getTokenBalance(account),
|
||||
},
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
// Transactions with gas cost
|
||||
public async requestTokens(amount: number, receiver: string): Promise<boolean> {
|
||||
return this.keeper.market.requestTokens(amount, receiver)
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import Keeper from "../keeper/Keeper"
|
||||
import Logger from "../utils/Logger"
|
||||
|
||||
export default class Asset {
|
||||
|
||||
@ -8,9 +9,24 @@ export default class Asset {
|
||||
this.keeper = keeper
|
||||
}
|
||||
|
||||
public isAssetActive(assetId: string): Promise<boolean> {
|
||||
public async isAssetActive(assetId: string): Promise<boolean> {
|
||||
const {market} = this.keeper
|
||||
return market.isAssetActive(assetId)
|
||||
}
|
||||
|
||||
public async registerAsset(name: string, description: string,
|
||||
price: number, publisherAddress: string): Promise<string> {
|
||||
const {market} = this.keeper
|
||||
|
||||
// generate an id
|
||||
const assetId = await market.generateId(name + description)
|
||||
Logger.log("Registering: ", assetId)
|
||||
|
||||
// register asset in the market
|
||||
const result = await market.register(assetId, price, publisherAddress)
|
||||
Logger.log("Registered: ", assetId, "in block", result.blockNumber)
|
||||
|
||||
return assetId
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Config from "./models/Config"
|
||||
import Logger from "./utils/Logger"
|
||||
import Config from "../models/Config"
|
||||
import Logger from "../utils/Logger"
|
||||
|
||||
declare var fetch
|
||||
|
39
src/ocean/Ocean.ts
Normal file
39
src/ocean/Ocean.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import Keeper from "../keeper/Keeper"
|
||||
import Web3Helper from "../keeper/Web3Helper"
|
||||
import Config from "../models/Config"
|
||||
import Account from "./Account"
|
||||
import Asset from "./Asset"
|
||||
import MetaData from "./metadata"
|
||||
import Order from "./Order"
|
||||
import Tribe from "./Tribe"
|
||||
|
||||
export default class Ocean {
|
||||
|
||||
public static async getInstance(config) {
|
||||
const ocean = new Ocean(config)
|
||||
ocean.keeper = await Keeper.getInstance(config, ocean.helper)
|
||||
ocean.tribe = await Tribe.getInstance(ocean.helper)
|
||||
ocean.order = new Order(ocean.keeper)
|
||||
ocean.account = new Account(ocean.keeper)
|
||||
ocean.asset = new Asset(ocean.keeper)
|
||||
return ocean
|
||||
}
|
||||
|
||||
public account: Account
|
||||
public order: Order
|
||||
public tribe: Tribe
|
||||
public asset: Asset
|
||||
public helper: Web3Helper
|
||||
public metadata: MetaData
|
||||
|
||||
private keeper: Keeper
|
||||
private config: Config
|
||||
|
||||
private constructor(config: Config) {
|
||||
|
||||
this.config = config
|
||||
|
||||
this.helper = new Web3Helper(config)
|
||||
this.metadata = new MetaData(config)
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import Web3Helper from "./keeper/Web3Helper"
|
||||
import Web3Helper from "../keeper/Web3Helper"
|
||||
|
||||
export default class Tribe {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import Ocean from "./Ocean"
|
||||
import Ocean from "./ocean/Ocean"
|
||||
import Logger from "./utils/Logger"
|
||||
|
||||
export {
|
||||
|
Loading…
Reference in New Issue
Block a user