swarm import using web3-bzz

pull/5370/head
aniket-engg 4 years ago committed by Aniket
parent 0a22add810
commit b5473db30d
  1. 12
      libs/remix-url-resolver/src/resolve.ts
  2. 37
      libs/remix-url-resolver/tests/test.ts

@ -1,4 +1,5 @@
import axios, { AxiosResponse } from 'axios'
var Bzz = require('web3-bzz');
export interface Imported {
content: string;
@ -86,8 +87,15 @@ export class RemixURLResolver {
}
}
handleSwarm(url: string, cleanUrl: string) {
return
async handleSwarm(url: string, cleanUrl: string): Promise<HandlerResponse> {
//eslint-disable-next-line no-useless-catch
try {
const bzz = new Bzz(Bzz.givenProvider || 'http://swarm-gateways.net');
const contentInBuffer = await bzz.download(url)
return { content: contentInBuffer.toString(), cleanUrl }
} catch(e) {
throw e
}
}
/**

@ -31,6 +31,12 @@ describe('testRunner', () => {
// handle: (match: Array<string>) => { return handleLocal(match[2], match[3]) }
// }
// ]
// browser/greeter.sol :
// bzz-raw://a728627437140f2b0b46c1bcfb0de2126d18b40e9b61c3e31bd96abebf714619
// browser/mortal.sol :
// bzz-raw://bbd213997d71870470ad72e9b4a74d7898d11bfd3bfc483220f9fd0721607bff
// metadata.json :
// bzz-raw://ce595759fa991e3b165b280c9376f073c1d9d5591edaadf05fe76ddef8909249
// urlResolver.resolve(fileName, localFSHandler)
// .then((sources: object) => {
// results = sources
@ -172,6 +178,37 @@ describe('testRunner', () => {
assert.deepEqual(results, expt)
})
})
// Test SWARM imports
describe('test getting SWARM imports', () => {
const urlResolver = new RemixURLResolver()
const fileName = 'bzz-raw://a728627437140f2b0b46c1bcfb0de2126d18b40e9b61c3e31bd96abebf714619'
let results: object = {}
before(done => {
urlResolver.resolve(fileName)
.then((sources: object) => {
results = sources
done()
})
.catch((e: Error) => {
throw e
})
})
it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3)
})
it('should return contract content from raw github url', () => {
const content = fs.readFileSync(__dirname + '/example_1/greeter.sol', { encoding: 'utf8'})
const expt: object = {
content: content,
cleanUrl: 'a728627437140f2b0b46c1bcfb0de2126d18b40e9b61c3e31bd96abebf714619',
type: 'swarm'
}
assert.deepEqual(results, expt)
})
})
})
})
})

Loading…
Cancel
Save