mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-11 10:14:07 +00:00
Example subgraph, ethCall test to drive implementation (#18)
* Setup example subgraph. * Implement eth_call in subgraph. * eth-call test stub Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Copyright 2021 Vulcanize, Inc.
|
||||
//
|
||||
|
||||
import path from 'path';
|
||||
|
||||
import { instantiate } from './index';
|
||||
|
||||
describe('eth-call wasm tests', () => {
|
||||
let exports: any;
|
||||
|
||||
it('should load the subgraph example wasm', async () => {
|
||||
const filePath = path.resolve(__dirname, '../test/subgraph/example1/build/Example1/Example1.wasm');
|
||||
const instance = await instantiate(filePath);
|
||||
exports = instance.exports;
|
||||
});
|
||||
|
||||
it('should execute exported function', () => {
|
||||
const { _start, testEthCall } = exports;
|
||||
|
||||
// Important to call _start for built subgraphs on instantiation!
|
||||
// TODO: Check api version https://github.com/graphprotocol/graph-node/blob/6098daa8955bdfac597cec87080af5449807e874/runtime/wasm/src/module/mod.rs#L533
|
||||
_start();
|
||||
|
||||
testEthCall();
|
||||
});
|
||||
});
|
||||
@@ -36,10 +36,10 @@ describe('wasm loader tests', () => {
|
||||
it('should instantiate a class in wasm from JS', async () => {
|
||||
const { Foo, FooID, __getString, __new, __pin, __unpin } = exports;
|
||||
|
||||
const newFooPtr = __pin(__new(FooID));
|
||||
const newFoo = Foo.wrap(newFooPtr);
|
||||
const newStrPtr = newFoo.getString();
|
||||
expect(__getString(newStrPtr)).to.equal('hello world!');
|
||||
__unpin(newFooPtr);
|
||||
const fooPtr = __pin(__new(FooID));
|
||||
const foo = Foo.wrap(fooPtr);
|
||||
const strPtr = foo.getString();
|
||||
expect(__getString(strPtr)).to.equal('hello world!');
|
||||
__unpin(fooPtr);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,82 +5,94 @@
|
||||
import fs from 'fs/promises';
|
||||
import loader from '@assemblyscript/loader';
|
||||
|
||||
const imports = {
|
||||
index: {
|
||||
'store.get': () => {
|
||||
console.log('store.get');
|
||||
},
|
||||
'store.set': () => {
|
||||
console.log('store.set');
|
||||
},
|
||||
|
||||
'typeConversion.stringToH160': () => {
|
||||
console.log('typeConversion.stringToH160');
|
||||
},
|
||||
'typeConversion.bytesToHex': () => {
|
||||
console.log('typeConversion.bytesToHex');
|
||||
},
|
||||
// 'typeConversion.bytesToString': () => {
|
||||
// console.log('typeConversion.bytesToString');
|
||||
// },
|
||||
'typeConversion.bigIntToString': () => {
|
||||
console.log('typeConversion.bigIntToString');
|
||||
},
|
||||
|
||||
// 'bigDecimal.fromString': () => {
|
||||
// console.log('bigDecimal.fromString');
|
||||
// },
|
||||
// 'bigDecimal.times': () => {
|
||||
// console.log('bigDecimal.times');
|
||||
// },
|
||||
'bigDecimal.dividedBy': () => {
|
||||
console.log('bigDecimal.dividedBy');
|
||||
},
|
||||
// 'bigDecimal.plus': () => {
|
||||
// console.log('bigDecimal.plus');
|
||||
// },
|
||||
// 'bigDecimal.minus': () => {
|
||||
// console.log('bigDecimal.minus');
|
||||
// },
|
||||
|
||||
'bigInt.plus': () => {
|
||||
console.log('bigInt.plus');
|
||||
},
|
||||
'bigInt.minus': () => {
|
||||
console.log('bigInt.minus');
|
||||
},
|
||||
'bigInt.times': () => {
|
||||
console.log('bigInt.times');
|
||||
},
|
||||
'bigInt.dividedBy': () => {
|
||||
console.log('bigInt.dividedBy');
|
||||
},
|
||||
// 'bigInt.mod': () => {
|
||||
// console.log('bigInt.mod');
|
||||
// },
|
||||
'bigInt.fromString': () => {
|
||||
console.log('bigInt.fromString');
|
||||
},
|
||||
|
||||
'log.log': () => {
|
||||
console.log('log.log');
|
||||
},
|
||||
|
||||
// 'dataSource.create': () => {
|
||||
// console.log('dataSource.create');
|
||||
// },
|
||||
'dataSource.address': () => {
|
||||
console.log('dataSource.address');
|
||||
}
|
||||
},
|
||||
ethereum: {
|
||||
'ethereum.call': () => {
|
||||
console.log('ethereum.call');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const instantiate = async (filePath: string): Promise<loader.ResultObject & { exports: any }> => {
|
||||
const buffer = await fs.readFile(filePath);
|
||||
return loader.instantiate(buffer, imports);
|
||||
|
||||
const imports = {
|
||||
index: {
|
||||
'store.get': () => {
|
||||
console.log('store.get');
|
||||
},
|
||||
'store.set': () => {
|
||||
console.log('store.set');
|
||||
},
|
||||
|
||||
'typeConversion.stringToH160': () => {
|
||||
console.log('typeConversion.stringToH160');
|
||||
},
|
||||
'typeConversion.bytesToHex': () => {
|
||||
console.log('typeConversion.bytesToHex');
|
||||
},
|
||||
// 'typeConversion.bytesToString': () => {
|
||||
// console.log('typeConversion.bytesToString');
|
||||
// },
|
||||
'typeConversion.bigIntToString': () => {
|
||||
console.log('typeConversion.bigIntToString');
|
||||
},
|
||||
|
||||
// 'bigDecimal.fromString': () => {
|
||||
// console.log('bigDecimal.fromString');
|
||||
// },
|
||||
// 'bigDecimal.times': () => {
|
||||
// console.log('bigDecimal.times');
|
||||
// },
|
||||
'bigDecimal.dividedBy': () => {
|
||||
console.log('bigDecimal.dividedBy');
|
||||
},
|
||||
// 'bigDecimal.plus': () => {
|
||||
// console.log('bigDecimal.plus');
|
||||
// },
|
||||
// 'bigDecimal.minus': () => {
|
||||
// console.log('bigDecimal.minus');
|
||||
// },
|
||||
|
||||
'bigInt.plus': () => {
|
||||
console.log('bigInt.plus');
|
||||
},
|
||||
'bigInt.minus': () => {
|
||||
console.log('bigInt.minus');
|
||||
},
|
||||
'bigInt.times': () => {
|
||||
console.log('bigInt.times');
|
||||
},
|
||||
'bigInt.dividedBy': () => {
|
||||
console.log('bigInt.dividedBy');
|
||||
},
|
||||
// 'bigInt.mod': () => {
|
||||
// console.log('bigInt.mod');
|
||||
// },
|
||||
'bigInt.fromString': () => {
|
||||
console.log('bigInt.fromString');
|
||||
},
|
||||
|
||||
'log.log': (_: number, msg: number) => {
|
||||
console.log('console.log', __getString(msg));
|
||||
},
|
||||
|
||||
// 'dataSource.create': () => {
|
||||
// console.log('dataSource.create');
|
||||
// },
|
||||
'dataSource.address': () => {
|
||||
console.log('dataSource.address');
|
||||
}
|
||||
},
|
||||
ethereum: {
|
||||
'ethereum.call': () => {
|
||||
console.log('ethereum.call');
|
||||
return null;
|
||||
}
|
||||
},
|
||||
conversion: {
|
||||
'typeConversion.stringToH160': () => {
|
||||
console.log('typeConversion.stringToH160');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const instance = await loader.instantiate(buffer, imports);
|
||||
|
||||
const exports = instance.exports;
|
||||
const { __getString } = exports;
|
||||
|
||||
return instance;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user