common.js (2250B)
1 // This file contains browser interface code that is not explicitly linked to a specific implementation. 2 3 4 /** 5 * The session object keeps the current known state of backends and settings during user interface execution. 6 * 7 * These are the only global properties. 8 */ 9 var session = { 10 account: undefined, 11 contractAddress: undefined, 12 contract: undefined, 13 name: undefined, 14 symbol: undefined, 15 //declarationHash: undefined, 16 contentGatewayUrl: undefined, 17 contentGateway: undefined, 18 supply: 0, 19 }; 20 21 22 /** 23 * Entry point. 24 * 25 * Passes UI entry point function to be executed after successful initialization. 26 */ 27 window.addEventListener('load', async () => { 28 const provider = window.craftnft.loadProvider(); 29 const conn = window.craftnft.loadConn(provider); 30 31 provider.on('accountsChanged', function(e) { 32 console.debug('accountschanged', e); 33 }); 34 35 36 let config = undefined; 37 let rs = await fetch('settings.json'); 38 if (rs.ok) { 39 config = await rs.json(); 40 } 41 42 let abi = undefined; 43 let bin = undefined; 44 rs = await fetch('contract/CraftNFT.json'); 45 if (rs.ok) { 46 abi = await rs.json(); 47 } 48 rs = await fetch('contract/CraftNFT.bin'); 49 if (rs.ok) { 50 bin = await rs.text(); 51 } 52 config.abi = abi 53 config.bin = bin 54 55 // This is handler code for the Allocate event 56 // Unfortunately, at least with openethereum 3.3.3, it does not post the log object, but the block header instead. 57 let ev = conn.eth.subscribe('logs', { 58 address: config.contract, 59 topics: ['0xb85ba54b9f6f8241b7e1499e3cfc186ac0b1c8b7100f599bf6ca6844f896c342'], 60 }, (e, r) => { 61 console.debug('results of subscribe', e, r); 62 }); 63 64 provider.on('message', (m) => { 65 console.debug('metamask log message for Allocate', m); 66 }); 67 68 69 // run() is defined in the implementation file. 70 window.craftnft.startSession(conn, config, session, run); 71 }); 72 73 74 /** 75 * Emitted when the user requests a token allocation with the UI. 76 */ 77 window.addEventListener('tokenRequest', (e) => { 78 window.craftnft.allocateToken(session, e.detail.digest, e.detail.tokenData.amount); 79 }); 80 81 82 /** 83 * Emitted when the user requests a token minting with the UI. 84 */ 85 window.addEventListener('tokenMint', (e) => { 86 window.craftnft.mintToken(session, e.detail.digest, e.detail.batch, e.detail.recipient, e.detail.index); 87 });