-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: o-tsaruk <oleksandr.v.tsaruk@gmail.com>
- Loading branch information
Showing
5 changed files
with
119 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2023 @paritytech/contracts-ui authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import { FileState } from '../types'; | ||
|
||
export const fileToFileState = (file: File): Promise<FileState> => | ||
new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
|
||
reader.onabort = reject; | ||
reader.onerror = reject; | ||
|
||
reader.onload = ({ target }: ProgressEvent<FileReader>): void => { | ||
if (target && target.result) { | ||
const name = file.name; | ||
const data = new Uint8Array(target.result as ArrayBuffer); | ||
const size = data.length; | ||
|
||
resolve({ | ||
data, | ||
name, | ||
size, | ||
} as FileState); | ||
} | ||
}; | ||
|
||
reader.readAsArrayBuffer(file); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2023 @paritytech/contracts-ui authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import { Buffer } from 'buffer'; | ||
|
||
function getFromPatron(field: string, hash: string) { | ||
const options = { | ||
method: 'GET', | ||
headers: | ||
field === 'metadata' | ||
? { | ||
'Content-Type': 'application/json', | ||
} | ||
: { | ||
'Content-Type': 'text/plain; charset=UTF-8', | ||
}, | ||
mode: 'cors' as RequestMode, | ||
}; | ||
|
||
return fetch('https://api.patron.works/buildSessions/' + field + '/' + hash, options).then( | ||
response => { | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
return field === 'metadata' ? response.json() : response.arrayBuffer(); | ||
}, | ||
); | ||
} | ||
|
||
export function getContractFromPatron(codeHash: string): Promise<File> { | ||
const metadataPromise = getFromPatron('metadata', codeHash); | ||
const wasmPromise = getFromPatron('wasm', codeHash); | ||
return Promise.all([metadataPromise, wasmPromise]).then(([metadataResponse, wasmResponse]) => { | ||
const result = Buffer.from(wasmResponse as ArrayBuffer).toString('hex'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
metadataResponse.source.wasm = '0x' + result; | ||
|
||
const metadataString = JSON.stringify(metadataResponse); | ||
|
||
const blob = new Blob([metadataString], { type: 'application/json' }); | ||
const patronFile = new File([blob], 'patron-contract.json', { | ||
lastModified: new Date().getTime(), | ||
type: 'json', | ||
}); | ||
return patronFile; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters