mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-08 00:44:06 +00:00
Implement typeConversion APIs bytesToHex, bigIntToString and stringToH160 (#19)
* Implement host api typeConversion.bytesToHex * Complete host api bigIntToString. * Create TypeId for assemblyscript loader. * Implement host apis bigInt fromString, plus and minus
This commit is contained in:
@@ -1,50 +1,52 @@
|
||||
import { Address, log } from '@graphprotocol/graph-ts';
|
||||
import { Address, log, BigInt, BigDecimal, ByteArray } from '@graphprotocol/graph-ts';
|
||||
|
||||
import {
|
||||
Example1
|
||||
Example1,
|
||||
Test
|
||||
} from '../generated/Example1/Example1';
|
||||
import { ExampleEntity } from '../generated/schema';
|
||||
|
||||
// export function handleTest (event: Test): void {
|
||||
// // Entities can be loaded from the store using a string ID; this ID
|
||||
// // needs to be unique across all entities of the same type
|
||||
// let entity = ExampleEntity.load(event.transaction.from.toHex());
|
||||
export function handleTest (event: Test): void {
|
||||
// Entities can be loaded from the store using a string ID; this ID
|
||||
// needs to be unique across all entities of the same type
|
||||
let entity = ExampleEntity.load(event.transaction.from.toHex());
|
||||
|
||||
// // Entities only exist after they have been saved to the store;
|
||||
// // `null` checks allow to create entities on demand
|
||||
// if (!entity) {
|
||||
// entity = new ExampleEntity(event.transaction.from.toHex());
|
||||
// Entities only exist after they have been saved to the store;
|
||||
// `null` checks allow to create entities on demand
|
||||
if (!entity) {
|
||||
entity = new ExampleEntity(event.transaction.from.toHex());
|
||||
|
||||
// // Entity fields can be set using simple assignments
|
||||
// entity.count = BigInt.fromI32(0);
|
||||
// }
|
||||
// Entity fields can be set using simple assignments
|
||||
entity.count = BigInt.fromI32(0);
|
||||
}
|
||||
|
||||
// // BigInt and BigDecimal math are supported
|
||||
// // entity.count = entity.count + BigInt.fromI32(1)
|
||||
// BigInt and BigDecimal math are supported
|
||||
// entity.count = entity.count + BigInt.fromI32(1)
|
||||
|
||||
// // Entity fields can be set based on event parameters
|
||||
// entity.param1 = event.params.param1;
|
||||
// entity.param2 = event.params.param2;
|
||||
// Entity fields can be set based on event parameters
|
||||
entity.param1 = event.params.param1;
|
||||
entity.param2 = event.params.param2;
|
||||
|
||||
// // Entities can be written to the store with `.save()`
|
||||
// entity.save();
|
||||
// Entities can be written to the store with `.save()`
|
||||
entity.save();
|
||||
|
||||
// // Note: If a handler doesn't require existing field values, it is faster
|
||||
// // _not_ to load the entity from the store. Instead, create it fresh with
|
||||
// // `new Entity(...)`, set the fields that should be updated and save the
|
||||
// // entity back to the store. Fields that were not set or unset remain
|
||||
// // unchanged, allowing for partial updates to be applied.
|
||||
// Note: If a handler doesn't require existing field values, it is faster
|
||||
// _not_ to load the entity from the store. Instead, create it fresh with
|
||||
// `new Entity(...)`, set the fields that should be updated and save the
|
||||
// entity back to the store. Fields that were not set or unset remain
|
||||
// unchanged, allowing for partial updates to be applied.
|
||||
|
||||
// // It is also possible to access smart contracts from mappings. For
|
||||
// // example, the contract that has emitted the event can be connected to
|
||||
// // with:
|
||||
// //
|
||||
// // let contract = Contract.bind(event.address)
|
||||
// //
|
||||
// // The following functions can then be called on this contract to access
|
||||
// // state variables and other data:
|
||||
// //
|
||||
// // - contract.getMethod(...)
|
||||
// }
|
||||
// It is also possible to access smart contracts from mappings. For
|
||||
// example, the contract that has emitted the event can be connected to
|
||||
// with:
|
||||
//
|
||||
// let contract = Contract.bind(event.address)
|
||||
//
|
||||
// The following functions can then be called on this contract to access
|
||||
// state variables and other data:
|
||||
//
|
||||
// - contract.getMethod(...)
|
||||
}
|
||||
|
||||
export function testEthCall (): void {
|
||||
log.debug('In test eth call', []);
|
||||
@@ -62,3 +64,87 @@ export function testEthCall (): void {
|
||||
log.debug('Contract eth call result', []);
|
||||
}
|
||||
}
|
||||
|
||||
export function testBytesToHex (): string {
|
||||
log.debug('In test bytesToHex', []);
|
||||
|
||||
const hexString = '0x231a';
|
||||
log.debug('Using hexString: {}', [hexString]);
|
||||
|
||||
const byteArray = ByteArray.fromHexString(hexString);
|
||||
const res = byteArray.toHexString();
|
||||
log.debug('typeConversion.bytesToHex result: {}', [res]);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function testBigIntToString (): string {
|
||||
log.debug('In test bigIntToString', []);
|
||||
|
||||
const bigInt = BigInt.fromString('1000000000000000000');
|
||||
const res = bigInt.toString();
|
||||
log.debug('typeConversion.bigIntToString from hex result: {}', [res]);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function testStringToH160 (): string {
|
||||
log.debug('In test stringToH160', []);
|
||||
|
||||
const addressString = '0xafad925b5eae1e370196cba39893e858ff7257d5';
|
||||
const address = Address.fromString(addressString);
|
||||
const res = address.toHexString();
|
||||
log.debug('typeConversion.stringToH160 result: {}', [res]);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function testBigDecimalDividedBy (): string {
|
||||
log.debug('In test bigDecimal.dividedBy', []);
|
||||
|
||||
const bigInt1 = BigInt.fromString('1000000000000000000');
|
||||
const bigInt2 = BigInt.fromString('100');
|
||||
|
||||
const bigDecimal1 = new BigDecimal(bigInt1);
|
||||
const bigDecimal2 = new BigDecimal(bigInt2);
|
||||
const res = bigDecimal1 / bigDecimal2;
|
||||
log.debug('bigDecimal.dividedBy result: {}', [res.toString()]);
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
export function testBigIntPlus (): string {
|
||||
log.debug('In test bigInt.plus', []);
|
||||
|
||||
const bigInt1 = BigInt.fromString('100');
|
||||
const bigInt2 = BigInt.fromString('100');
|
||||
|
||||
const res = bigInt1 + bigInt2;
|
||||
log.debug('bigInt.plus result: {}', [res.toString()]);
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
export function testBigIntMinus (): string {
|
||||
log.debug('In test bigInt.minus', []);
|
||||
|
||||
const bigInt1 = BigInt.fromString('200');
|
||||
const bigInt2 = BigInt.fromString('100');
|
||||
|
||||
const res = bigInt1 - bigInt2;
|
||||
log.debug('bigInt.minus result: {}', [res.toString()]);
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
export function testBigIntFromString (): string {
|
||||
log.debug('In test bigInt.fromString', []);
|
||||
|
||||
const string = '123';
|
||||
const bigInt = BigInt.fromString(string);
|
||||
const res = bigInt.toString();
|
||||
log.debug('bigInt.FromString result: {}', [res]);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// TODO: Export it automatically using graph-cli.
|
||||
export { BigDecimal, BigInt };
|
||||
|
||||
Reference in New Issue
Block a user