-
Notifications
You must be signed in to change notification settings - Fork 2
/
7.1.computejob-publish.js
117 lines (104 loc) · 3.04 KB
/
7.1.computejob-publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const {
NftFactory,
Nft,
ProviderInstance,
Datatoken,
getHash,
Aquarius
} = require('@oceanprotocol/lib');
const { SHA256 } = require('crypto-js');
const Web3 = require('web3');
const { web3Provider, oceanConfig } = require('./config');
const { dataDDo, datasetUrl } = require('./sample_ddo/dataDDo');
const { algoDDo, algoAssetUrl } = require('./sample_ddo/algo');
const web3 = new Web3(web3Provider);
const aquarius = new Aquarius(oceanConfig.metadataCacheUri);
const nft = new Nft(web3);
const providerUrl = oceanConfig.providerUri;
const Factory = new NftFactory(oceanConfig.erc721FactoryAddress, web3);
const mintAndtransfer = async (
datatokenAddress,
publisherAccount,
publisherAddress,
consumerAddress
) => {
const datatoken = new Datatoken(web3);
await datatoken.mint(
datatokenAddress,
publisherAddress,
'100',
consumerAddress
);
};
const createDataNFTWithMetadata = async (ddo, files) => {
const accounts = await web3.eth.getAccounts();
const publisherAccount = accounts[0];
const nftParams = {
name: '72120Bundle',
symbol: '72Bundle',
templateIndex: 1,
tokenURI: 'https://example.com',
transferable: true,
owner: publisherAccount
};
const erc20Params = {
templateIndex: 1,
cap: '100000',
feeAmount: '0',
paymentCollector: '0x0000000000000000000000000000000000000000',
feeToken: '0x0000000000000000000000000000000000000000',
minter: publisherAccount,
mpFeeAddress: '0x0000000000000000000000000000000000000000'
};
const result = await Factory.createNftWithErc20(
publisherAccount,
nftParams,
erc20Params
);
const erc721Address = result.events.NFTCreated.returnValues[0];
const datatokenAddress = result.events.TokenCreated.returnValues[0];
// create the files encrypted string
let providerResponse = await ProviderInstance.encrypt(files, providerUrl);
ddo.services[0].files = await providerResponse;
ddo.services[0].datatokenAddress = datatokenAddress;
ddo.services[0].serviceEndpoint = providerUrl;
// update ddo and set the right did
ddo.nftAddress = erc721Address;
const chain = await web3.eth.getChainId();
ddo.id = `did:op:${SHA256(
web3.utils.toChecksumAddress(erc721Address) + chain.toString(10)
)}`;
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl);
const encryptedResponse = await providerResponse;
const metadataHash = getHash(JSON.stringify(ddo));
await nft.setMetadata(
erc721Address,
publisherAccount,
0,
providerUrl,
'',
'0x2',
encryptedResponse,
`0x${metadataHash}`
);
await mintAndtransfer(
datatokenAddress,
publisherAccount,
publisherAccount,
accounts[1]
);
return ddo.id;
};
(async () => {
try {
const dataDid = await createDataNFTWithMetadata(dataDDo, datasetUrl);
console.log(`Dataset did ${dataDid}`);
const algoDid = await createDataNFTWithMetadata(algoDDo, algoAssetUrl);
console.log(`Algo did ${algoDid}`);
process.exit();
} catch (e) {
console.log(e);
}
})().catch((error) => {
throw error;
});