1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00
This commit is contained in:
Bogdan Fazakas 2023-03-28 09:14:38 +03:00
parent 996d967e65
commit 6585c02f52
13 changed files with 45 additions and 301 deletions

View File

@ -30,8 +30,11 @@ export class Datatoken extends SmartContract {
/**
* Instantiate ERC20 Datatokens
* @param {AbiItem | AbiItem[]} datatokensAbi
* @param {Web3} web3
* @param {Signer} signer
* @param {string | number} network optional network name or chain id
* @param {Config} config optional config object
* @param {AbiItem[]} abi optional abi
* @param {AbiItem[]} abiEnterprise optional enterprise abi
*/
constructor(
signer: Signer,

View File

@ -1,9 +1,4 @@
// import Web3 from 'web3'
// import { Contract } from 'web3-eth-contract'
// import { AbiItem } from 'web3-utils'
import { Signer, Contract, ContractFunction } from 'ethers'
import { Interface } from 'ethers/lib/utils'
import { AbiItem } from '../@types'
import { Config } from '../config'
import { SmartContract } from './SmartContract'

View File

@ -72,9 +72,9 @@ export function getEventFromTx(txReceipt, eventName) {
/**
* Send the transation on chain
* @param {string} from account that calls the function
* @param {any} estGas estimated gas for the transaction
* @param {Web3} web3 web3 objcet
* @param {BigNumber} estGas estimated gas for the transaction
* @param {Signer} signer signer object
* @param {number} gasFeeMultiplier number represinting the multiplier we apply to gas fees
* @param {Function} functionToSend function that we need to send
* @param {...any[]} args arguments of the function
* @return {Promise<any>} transaction receipt

View File

@ -1,8 +1,8 @@
import sha256 from 'crypto-js/sha256'
import Web3 from 'web3'
import { ethers } from 'ethers'
export function generateDid(nftAddress: string, chainId: number): string {
nftAddress = Web3.utils.toChecksumAddress(nftAddress)
nftAddress = ethers.utils.getAddress(nftAddress)
const checksum = sha256(nftAddress + chainId.toString(10))
return `did:op:${checksum.toString()}`
}

View File

@ -1,13 +1,19 @@
import Web3 from 'web3'
import { ethers, Signer, providers } from 'ethers'
export async function signHash(web3: Web3, message: string, address: string) {
let signedMessage = await web3.eth.sign(message, address)
export async function signHash(signer: Signer, message: string) {
/* Since ganache has no support yet for personal_sign, we must use the legacy implementation
const signedMessage = await user2.signMessage(message)
*/
const messageHashBytes = ethers.utils.arrayify(message)
let signedMessage = await (signer as providers.JsonRpcSigner)._legacySignMessage(
messageHashBytes
)
signedMessage = signedMessage.substr(2) // remove 0x
const r = '0x' + signedMessage.slice(0, 64)
const s = '0x' + signedMessage.slice(64, 128)
let v = '0x' + signedMessage.slice(128, 130)
// make sure we obey 27 and 28 standards
if (v === '0x00') v = '0x1b'
if (v === '0x01') v = '0x1c'
return { v, r, s }
}

View File

@ -3,7 +3,6 @@ import { ethers, Signer, BigNumber } from 'ethers'
import { amountToUnits, unitsToAmount, minAbi, sendTx, LoggerInstance } from '.'
import { Config } from '../config'
import { ReceiptOrEstimate, ReceiptOrDecimal } from '../@types'
// import { AbiItem } from 'web3-utils'
/**
* Approve spender to spent amount tokens

View File

@ -1,4 +1,3 @@
// import { AbiItem } from 'web3-utils/types'
export const minAbi = [
{
constant: true,

View File

@ -1,217 +0,0 @@
import Web3 from 'web3'
import { AbiItem } from 'web3-utils/types'
import OPFCommunityFeeCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json'
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
import Router from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json'
import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json'
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
// delete this once removed from contracts
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
import { getAddresses, GAS_PRICE } from './config'
const estimateGasAndDeployContract = async (
web3: Web3,
abi: AbiItem | AbiItem[],
bytecode: string,
argumentsArray: any[],
owner: string
) => {
const contract = new web3.eth.Contract(abi)
// get est gascost
const estimatedGas = await contract
.deploy({
data: bytecode,
arguments: argumentsArray
})
.estimateGas(function (err, estimatedGas) {
if (err) console.log('DeployContracts: ' + err)
return estimatedGas
})
// deploy the contract and get its address
return await contract
.deploy({
data: bytecode,
arguments: argumentsArray
})
.send({
from: owner,
gas: estimatedGas + 1,
gasPrice: GAS_PRICE
})
.then(function (contract) {
return contract.options.address
})
}
export interface Addresses {
opfCommunityFeeCollectorAddress: string
datatokenTemplateAddress: string
nftTemplateAddress: string
oceanAddress: string
routerAddress: string
sideStakingAddress: string
fixedRateAddress: string
dispenserAddress: string
nftFactoryAddress: string
daiAddress: string
usdcAddress: string
poolTemplateAddress: string
}
export const deployContracts = async (web3: Web3, owner: string): Promise<Addresses> => {
const addresses: Addresses = {} as Addresses
const configAddresses = getAddresses()
// deploy OPF free collector
addresses.opfCommunityFeeCollectorAddress =
configAddresses?.OPFCommunityFeeCollector ||
(await estimateGasAndDeployContract(
web3,
OPFCommunityFeeCollector.abi as AbiItem[],
OPFCommunityFeeCollector.bytecode,
[owner, owner],
owner
))
// deploy Datatoken template
addresses.datatokenTemplateAddress =
configAddresses?.ERC20Template['1'] ||
(await estimateGasAndDeployContract(
web3,
ERC20Template.abi as AbiItem[],
ERC20Template.bytecode,
[],
owner
))
// deploy NFT template
addresses.nftTemplateAddress =
configAddresses?.ERC721Template?.['1'] ||
(await estimateGasAndDeployContract(
web3,
ERC721Template.abi as AbiItem[],
ERC721Template.bytecode,
[],
owner
))
// deploy OCEAN mock tocken
addresses.oceanAddress =
configAddresses?.Ocean ||
(await estimateGasAndDeployContract(
web3,
MockERC20.abi as AbiItem[],
MockERC20.bytecode,
['OCEAN', 'OCEAN', 18],
owner
))
// deploy pool template to be removed once the pools are removed from contracts
addresses.poolTemplateAddress =
configAddresses?.poolTemplate ||
(await estimateGasAndDeployContract(
web3,
PoolTemplate.abi as AbiItem[],
PoolTemplate.bytecode,
[],
owner
))
// deploy router
addresses.routerAddress =
configAddresses?.Router ||
(await estimateGasAndDeployContract(
web3,
Router.abi as AbiItem[],
Router.bytecode,
[
owner,
addresses.oceanAddress,
addresses.poolTemplateAddress,
addresses.opfCommunityFeeCollectorAddress,
[]
],
owner
))
// deploy fixed rate
addresses.fixedRateAddress =
configAddresses?.FixedPrice ||
(await estimateGasAndDeployContract(
web3,
FixedRate.abi as AbiItem[],
FixedRate.bytecode,
[addresses.routerAddress],
owner
))
// deploy dispenser
addresses.dispenserAddress =
configAddresses?.Dispenser ||
(await estimateGasAndDeployContract(
web3,
Dispenser.abi as AbiItem[],
Dispenser.bytecode,
[addresses.routerAddress],
owner
))
// deploy NFT factory
addresses.nftFactoryAddress =
configAddresses?.ERC721Factory ||
(await estimateGasAndDeployContract(
web3,
ERC721Factory.abi as AbiItem[],
ERC721Factory.bytecode,
[
addresses.nftTemplateAddress,
addresses.datatokenTemplateAddress,
addresses.routerAddress
],
owner
))
// deploy USDC mock tocken
addresses.usdcAddress =
configAddresses?.MockUSDC ||
(await estimateGasAndDeployContract(
web3,
MockERC20.abi as AbiItem[],
MockERC20.bytecode,
['USDC', 'USDC', 6],
owner
))
// deploy DAI mock tocken
addresses.daiAddress =
configAddresses?.MockDAI ||
(await estimateGasAndDeployContract(
web3,
MockERC20.abi as AbiItem[],
MockERC20.bytecode,
['DAI', 'DAI', 18],
owner
))
if (!configAddresses?.Router) {
const RouterContract = new web3.eth.Contract(
Router.abi as AbiItem[],
addresses.routerAddress
)
await RouterContract.methods
.addFactory(addresses.nftFactoryAddress)
.send({ from: owner })
await RouterContract.methods
.addFixedRateContract(addresses.fixedRateAddress)
.send({ from: owner })
await RouterContract.methods
.addDispenserContract(addresses.dispenserAddress)
.send({ from: owner })
}
return addresses
}

View File

@ -1,5 +1,4 @@
import Web3 from 'web3'
import { ethers, Wallet, providers, Signer } from 'ethers'
import { providers, Signer } from 'ethers'
import fs from 'fs'
import { homedir } from 'os'
import { ConfigHelper, configHelperNetworks } from '../src/config'
@ -7,11 +6,24 @@ import { LoggerInstance, LogLevel } from '../src/utils'
LoggerInstance.setLevel(LogLevel.Error)
export interface Addresses {
opfCommunityFeeCollectorAddress: string
datatokenTemplateAddress: string
nftTemplateAddress: string
oceanAddress: string
routerAddress: string
sideStakingAddress: string
fixedRateAddress: string
dispenserAddress: string
nftFactoryAddress: string
daiAddress: string
usdcAddress: string
poolTemplateAddress: string
}
export const GAS_PRICE = '3000000000'
// by default, we connect with development network
export const web3 = new Web3(process.env.NODE_URI || configHelperNetworks[1].nodeUri)
export const provider = new providers.JsonRpcProvider(
process.env.NODE_URI || configHelperNetworks[1].nodeUri
)

View File

@ -23,8 +23,8 @@
/// 3. [Install dependencies](#3-install-dependancies)
/// 4. [Import dependencies and add variables and constants](#4-import-dependencies-and-add-variables-and-constants)
/// 5. [Load the configuration, initialize accounts and deploy contracts](#5-load-the-configuration-initialize-accounts-and-deploy-contracts)
/// 6. [Publish Data NFT and a Datatoken with a fixed rate exchange](#6-publish-data-nft-and-a-datatoken-with-a-fixed-rate-exchange
/// 7. [Consume a fixed rate asset data asset, and downloads it'](#7-consume-a-fixed-rate-asset-data-asset)
/// 6. [Publish Data NFT and a Datatoken with a fixed rate exchange](#6-publish-data-nft-and-a-datatoken-with-a-fixed-rate-exchange)
/// 7. [Consume a fixed rate asset data asset'](#7-consume-a-fixed-rate-asset-data-asset)
/// 8. [Publish Data NFT and a Datatoken with a dispenser](#8-publish-data-nft-and-a-datatoken-with-a-dispenserr)
/// 9. [Consume a dispenser data asset](#9-consume-a-dispenser-data-asset)
/// 10. [Using ERC725 Key-Value Store](#10-using-erc725-key-value-store)

View File

@ -526,17 +526,7 @@ describe('Datatoken', () => {
]
)
// const { v, r, s } = await signHash(web3, message,user3)
const messageHashBytes = ethers.utils.arrayify(message)
let signedMessage = await (user3 as providers.JsonRpcSigner)._legacySignMessage(
messageHashBytes
)
signedMessage = signedMessage.substr(2) // remove 0x
const r = '0x' + signedMessage.slice(0, 64)
const s = '0x' + signedMessage.slice(64, 128)
let v = '0x' + signedMessage.slice(128, 130)
if (v === '0x00') v = '0x1b'
if (v === '0x01') v = '0x1c'
const { v, r, s } = await signHash(user3, message)
const providerFees: ProviderFees = {
providerFeeAddress: await user3.getAddress(),
@ -586,16 +576,7 @@ describe('Datatoken', () => {
]
)
const messageHashBytes = ethers.utils.arrayify(message)
let signedMessage = await (user3 as providers.JsonRpcSigner)._legacySignMessage(
messageHashBytes
)
signedMessage = signedMessage.substr(2) // remove 0x
const r = '0x' + signedMessage.slice(0, 64)
const s = '0x' + signedMessage.slice(64, 128)
let v = '0x' + signedMessage.slice(128, 130)
if (v === '0x00') v = '0x1b'
if (v === '0x01') v = '0x1c'
const { v, r, s } = await signHash(user3, message)
const providerFees: ProviderFees = {
providerFeeAddress: await user3.getAddress(),
@ -641,16 +622,8 @@ describe('Datatoken', () => {
providerValidUntil
]
)
const messageHashBytes = ethers.utils.arrayify(message)
let signedMessage = await (user3 as providers.JsonRpcSigner)._legacySignMessage(
messageHashBytes
)
signedMessage = signedMessage.substr(2) // remove 0x
const r = '0x' + signedMessage.slice(0, 64)
const s = '0x' + signedMessage.slice(64, 128)
let v = '0x' + signedMessage.slice(128, 130)
if (v === '0x00') v = '0x1b'
if (v === '0x01') v = '0x1c'
const { v, r, s } = await signHash(user3, message)
const providerFees: ProviderFees = {
providerFeeAddress: await user3.getAddress(),
@ -698,16 +671,8 @@ describe('Datatoken', () => {
providerValidUntil
]
)
const messageHashBytes = ethers.utils.arrayify(message)
let signedMessage = await (user3 as providers.JsonRpcSigner)._legacySignMessage(
messageHashBytes
)
signedMessage = signedMessage.substr(2) // remove 0x
const r = '0x' + signedMessage.slice(0, 64)
const s = '0x' + signedMessage.slice(64, 128)
let v = '0x' + signedMessage.slice(128, 130)
if (v === '0x00') v = '0x1b'
if (v === '0x01') v = '0x1c'
const { v, r, s } = await signHash(user3, message)
const providerFees: ProviderFees = {
providerFeeAddress: await user1.getAddress(),

View File

@ -1,5 +1,4 @@
import { assert } from 'chai'
import { deployContracts, Addresses } from '../TestContractHandler'
import sha256 from 'crypto-js/sha256'
import { ethers, Signer } from 'ethers'
import { getAddresses, getTestConfig, provider } from '../config'

View File

@ -253,25 +253,8 @@ describe('Nft Factory test', () => {
providerValidUntil
]
)
/* Since ganache has no support yet for personal_sign, we must use the legacy implementation
const signedMessage = await user2.signMessage(message)
*/
// const signedMessage = await (user2 as providers.JsonRpcSigner)._legacySignMessage(
// message
// )
// console.log(signedMessage)
// const { v, r, s } = await signHash(web3, message, consumeFeeAddress)
const messageHashBytes = ethers.utils.arrayify(message)
let signedMessage = await (
consumeFeeAddress as providers.JsonRpcSigner
)._legacySignMessage(messageHashBytes)
signedMessage = signedMessage.substr(2) // remove 0x
const r = '0x' + signedMessage.slice(0, 64)
const s = '0x' + signedMessage.slice(64, 128)
let v = '0x' + signedMessage.slice(128, 130)
if (v === '0x00') v = '0x1b'
if (v === '0x01') v = '0x1c'
const { v, r, s } = await signHash(consumeFeeAddress, message)
const providerFees: ProviderFees = {
providerFeeAddress: await consumeFeeAddress.getAddress(),