-
Notifications
You must be signed in to change notification settings - Fork 6
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
Use createAsset function from SDK. #73
base: main
Are you sure you want to change the base?
Changes from 9 commits
55b9eaa
9eb253e
db00782
5e2aac6
7fee679
eabaa24
d1efb18
478df75
7681750
bca4cb1
cb94b1d
5a9de65
022e47e
a381328
a792f83
3182f0d
a2ec4d2
e451ffa
0ed52a7
3b24717
bfccdc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
import { SHA256 } from "crypto-js"; | ||
import { ethers, Signer } from "ethers"; | ||
import fetch from "cross-fetch"; | ||
import { promises as fs } from "fs"; | ||
import { createHash } from "crypto"; | ||
import * as path from "path"; | ||
import * as sapphire from '@oasisprotocol/sapphire-paratime'; | ||
import { | ||
AccesslistFactory, | ||
Aquarius, | ||
DatatokenCreateParams, | ||
Nft, | ||
NftCreateData, | ||
NftFactory, | ||
ProviderInstance, | ||
ZERO_ADDRESS, | ||
approveWei, | ||
|
@@ -22,14 +17,12 @@ | |
DDO, | ||
orderAsset, | ||
getEventFromTx, | ||
DispenserCreationParams, | ||
FreCreationParams, | ||
DownloadResponse, | ||
Asset, | ||
ProviderFees, | ||
ComputeAlgorithm, | ||
LoggerInstance, | ||
Datatoken4 | ||
createAsset | ||
} from "@oceanprotocol/lib"; | ||
import { hexlify } from "ethers/lib/utils"; | ||
|
||
|
@@ -67,176 +60,46 @@ | |
return { data, filename }; | ||
} | ||
|
||
export async function createAsset( | ||
export async function createAssetUtil( | ||
name: string, | ||
symbol: string, | ||
owner: Signer, | ||
assetUrl: any, | ||
ddo: any, | ||
providerUrl: string, | ||
config: Config, | ||
aquariusInstance: Aquarius, | ||
encryptDDO: boolean = true, | ||
templateIndex: number = 1, | ||
providerFeeToken: string = ZERO_ADDRESS, | ||
accessListFactory?: string, | ||
allowAccessList?: string, | ||
denyAccessList?: string, | ||
macOsProviderUrl?: string, | ||
encryptDDO: boolean = true, | ||
|
||
) { | ||
const { chainId } = await owner.provider.getNetwork(); | ||
const nft = new Nft(owner, chainId); | ||
const nftFactory = new NftFactory(config.nftFactoryAddress, owner); | ||
|
||
let wrappedSigner | ||
let allowListAddress | ||
if(templateIndex === 4){ | ||
// Wrap the signer for Sapphire | ||
wrappedSigner = sapphire.wrap(owner); | ||
const wrappedSigner = sapphire.wrap(owner); | ||
|
||
// Create Access List Factory | ||
const accessListFactory = new AccesslistFactory(config.accessListFactory, wrappedSigner, chainId); | ||
const accessListFactoryObj = new AccesslistFactory(config.accessListFactory, wrappedSigner, chainId); | ||
|
||
// Create Allow List | ||
allowListAddress = await accessListFactory.deployAccessListContract( | ||
await accessListFactoryObj.deployAccessListContract( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we sure about these params? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I double-checked, I put them in place to match createAsset from ocean.js There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its not about the order of the params: Here, you're creating a new access list factory and a new access list deployed from that factory
But you're calling the function with other values, the factory addresses and the allow access list addresses do not match (you're creating new ones and passing something else) |
||
'AllowList', | ||
'ALLOW', | ||
['https://oceanprotocol.com/nft/'], | ||
false, | ||
await owner.getAddress(), | ||
[await owner.getAddress(), ZERO_ADDRESS] | ||
); | ||
) | ||
return await createAsset(name, symbol, wrappedSigner, assetUrl, templateIndex, ddo, encryptDDO, providerUrl || macOsProviderUrl, providerFeeToken, aquariusInstance, accessListFactory, allowAccessList, denyAccessList); | ||
} | ||
|
||
ddo.chainId = parseInt(chainId.toString(10)); | ||
const nftParamsAsset: NftCreateData = { | ||
name, | ||
symbol, | ||
templateIndex, | ||
tokenURI: "aaa", | ||
transferable: true, | ||
owner: await owner.getAddress(), | ||
}; | ||
const datatokenParams: DatatokenCreateParams = { | ||
templateIndex, | ||
cap: "100000", | ||
feeAmount: "0", | ||
paymentCollector: await owner.getAddress(), | ||
feeToken: config.oceanTokenAddress, | ||
minter: await owner.getAddress(), | ||
mpFeeAddress: ZERO_ADDRESS, | ||
}; | ||
|
||
let bundleNFT; | ||
if (!ddo.stats?.price?.value) { | ||
bundleNFT = await nftFactory.createNftWithDatatoken( | ||
nftParamsAsset, | ||
datatokenParams | ||
); | ||
} else if (ddo?.stats?.price?.value === "0") { | ||
const dispenserParams: DispenserCreationParams = { | ||
dispenserAddress: config.dispenserAddress, | ||
maxTokens: "1", | ||
maxBalance: "100000000", | ||
withMint: true, | ||
allowedSwapper: ZERO_ADDRESS, | ||
}; | ||
|
||
bundleNFT = await nftFactory.createNftWithDatatokenWithDispenser( | ||
nftParamsAsset, | ||
datatokenParams, | ||
dispenserParams | ||
); | ||
} else { | ||
const fixedPriceParams: FreCreationParams = { | ||
fixedRateAddress: config.fixedRateExchangeAddress, | ||
baseTokenAddress: config.oceanTokenAddress, | ||
owner: await owner.getAddress(), | ||
marketFeeCollector: await owner.getAddress(), | ||
baseTokenDecimals: 18, | ||
datatokenDecimals: 18, | ||
fixedRate: ddo.stats.price.value, | ||
marketFee: "0", | ||
allowedConsumer: await owner.getAddress(), | ||
withMint: true, | ||
}; | ||
|
||
bundleNFT = await nftFactory.createNftWithDatatokenWithFixedRate( | ||
nftParamsAsset, | ||
datatokenParams, | ||
fixedPriceParams | ||
); | ||
} | ||
|
||
const trxReceipt = await bundleNFT.wait(); | ||
// events have been emitted | ||
const nftCreatedEvent = getEventFromTx(trxReceipt, "NFTCreated"); | ||
const tokenCreatedEvent = getEventFromTx(trxReceipt, "TokenCreated"); | ||
|
||
const nftAddress = nftCreatedEvent.args.newTokenAddress; | ||
const datatokenAddressAsset = tokenCreatedEvent.args.newTokenAddress; | ||
// create the files encrypted string | ||
assetUrl.datatokenAddress = datatokenAddressAsset; | ||
assetUrl.nftAddress = nftAddress; | ||
ddo.services[0].files = templateIndex === 4 ? '' : await ProviderInstance.encrypt( | ||
assetUrl, | ||
chainId, | ||
macOsProviderUrl || providerUrl | ||
); | ||
ddo.services[0].datatokenAddress = datatokenAddressAsset; | ||
ddo.services[0].serviceEndpoint = providerUrl; | ||
|
||
ddo.nftAddress = nftAddress; | ||
ddo.id = | ||
"did:op:" + | ||
SHA256(ethers.utils.getAddress(nftAddress) + chainId.toString(10)); | ||
|
||
let metadata; | ||
let metadataHash; | ||
let flags; | ||
if (encryptDDO) { | ||
metadata = await ProviderInstance.encrypt( | ||
ddo, | ||
chainId, | ||
macOsProviderUrl || providerUrl | ||
); | ||
const validateResult = await aquariusInstance.validate(ddo); | ||
metadataHash = validateResult.hash; | ||
flags = 2 | ||
} else { | ||
const stringDDO = JSON.stringify(ddo); | ||
const bytes = Buffer.from(stringDDO); | ||
metadata = hexlify(bytes); | ||
metadataHash = "0x" + createHash("sha256").update(metadata).digest("hex"); | ||
flags = 0 | ||
} | ||
|
||
await nft.setMetadata( | ||
nftAddress, | ||
await owner.getAddress(), | ||
0, | ||
providerUrl, | ||
"", | ||
ethers.utils.hexlify(flags), | ||
metadata, | ||
metadataHash | ||
); | ||
|
||
if(templateIndex === 4){ // Use Datatoken4 for file object | ||
const datatoken = new Datatoken4( | ||
wrappedSigner, | ||
ethers.utils.toUtf8Bytes(JSON.stringify(assetUrl.files)), | ||
chainId, | ||
config | ||
); | ||
|
||
// Set file object | ||
await datatoken.setFileObject(datatokenAddressAsset, await wrappedSigner.getAddress()); | ||
|
||
// Set allow list for the datatoken | ||
await datatoken.setAllowListContract( | ||
datatokenAddressAsset, | ||
allowListAddress, | ||
await wrappedSigner.getAddress() | ||
);} | ||
|
||
return ddo.id; | ||
const did = await createAsset(name, symbol, owner, assetUrl, templateIndex, ddo, encryptDDO, providerUrl || macOsProviderUrl, providerFeeToken, aquariusInstance); | ||
console.log('did: ', did) | ||
return did | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,7 +5,7 @@ import { | |||
Aquarius, | ||||
DDO | ||||
} from '@oceanprotocol/lib'; | ||||
import { createAsset, updateAssetMetadata } from './helpers'; | ||||
import { createAssetUtil, updateAssetMetadata } from './helpers'; | ||||
|
||||
export interface PublishAssetParams { | ||||
title: string; | ||||
|
@@ -44,13 +44,7 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c | |||
license: 'MIT', | ||||
tags: params.tags | ||||
}, | ||||
stats: { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we turned back the
we now have a FixedPrice datatoken instead of Dispenser one These changes + the default template of 1 ... breaks download of the asset (on SDK OrderUtils) at least for oasis shappire Line 70 in ce5ea7f
and then compare with the SDK one: https://github.com/oceanprotocol/ocean.js/blob/4437713b2243a87ae1a01730196abc27c872ac24/src/utils/Assets.ts#L46 To make sure we're passing all the relevant/needed parameters specially the templateIDorAddress and the stats fields are very important
|
||||
allocated: 0, | ||||
orders: 0, | ||||
price: { | ||||
value: params.isCharged ? params.price : "0" | ||||
} | ||||
}, | ||||
|
||||
services: [ | ||||
{ | ||||
id: 'access', | ||||
|
@@ -62,15 +56,6 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c | |||
timeout: Number(params.timeout), | ||||
}, | ||||
], | ||||
nft: { | ||||
address: "", | ||||
name: "Ocean Data NFT", | ||||
symbol: "OCEAN-NFT", | ||||
state: 5, | ||||
tokenURI: "", | ||||
owner: "", | ||||
created: "" | ||||
} | ||||
}; | ||||
|
||||
// Asset URL setup based on storage type | ||||
|
@@ -80,18 +65,17 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c | |||
files: [{ type: params.storageType, url: params.assetLocation, method: 'GET' }], | ||||
}; | ||||
|
||||
// Other networks | ||||
const did = await createAsset( | ||||
params.title, | ||||
'OCEAN-NFT', | ||||
signer, | ||||
assetUrl, | ||||
metadata, | ||||
params.providerUrl, | ||||
config, | ||||
aquarius, | ||||
params.template || 1 | ||||
); | ||||
// Other networks | ||||
const did = await createAssetUtil( | ||||
params.title, | ||||
'OCEAN-NFT', | ||||
signer, | ||||
assetUrl, | ||||
metadata, | ||||
params.providerUrl, | ||||
config, | ||||
aquarius | ||||
); | ||||
|
||||
|
||||
console.log(`Asset successfully published with DID: ${did}`); | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be the template id or the template address...
template index is something else (and must be calculated on SDK side)
might work, but is not exactly the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the name is misleading, it should be template id, correct, I'll update this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this still needs to be updated