Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Update AssemblyScript to 0.19.10 #185

Merged
merged 14 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- "10"
- "12"
- "14"
- "16"
160 changes: 71 additions & 89 deletions chain/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Address, BigInt, Bytes, Wrapped } from '..'
import '../common/eager_offset'
import { Bytes, Wrapped } from '../common/collections'
import { Address, BigInt } from '../common/numbers'

/** Host Ethereum interface */
export declare namespace ethereum {
Expand Down Expand Up @@ -33,8 +35,22 @@ export namespace ethereum {
* A dynamically typed value used when accessing Ethereum data.
*/
export class Value {
kind: ValueKind
data: ValuePayload
constructor(
public kind: ValueKind,
public data: ValuePayload,
) {}

@operator('<')
lt(other: Value): boolean {
abort("Less than operator isn't supported in Value")
return false
}

@operator('>')
gt(other: Value): boolean {
abort("Greater than operator isn't supported in Value")
return false
}

toAddress(): Address {
assert(this.kind == ValueKind.ADDRESS, 'Ethereum value is not an address')
Expand Down Expand Up @@ -97,7 +113,7 @@ export namespace ethereum {
let valueArray = this.toArray()
let out = new Array<T>(valueArray.length)
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toTuple() as T
out[i] = <T>valueArray[i].toTuple()
}
return out
}
Expand Down Expand Up @@ -182,81 +198,47 @@ export namespace ethereum {

static fromAddress(address: Address): Value {
assert(address.length == 20, 'Address must contain exactly 20 bytes')

let token = new Value()
token.kind = ValueKind.ADDRESS
token.data = address as u64
return token
return new Value(ValueKind.ADDRESS, changetype<u32>(address))
}

static fromBoolean(b: boolean): Value {
let token = new Value()
token.kind = ValueKind.BOOL
token.data = b ? 1 : 0
return token
return new Value(ValueKind.BOOL, b ? 1 : 0)
}

static fromBytes(bytes: Bytes): Value {
let token = new Value()
token.kind = ValueKind.BYTES
token.data = bytes as u64
return token
return new Value(ValueKind.BYTES, changetype<u32>(bytes))
}

static fromFixedBytes(bytes: Bytes): Value {
let token = new Value()
token.kind = ValueKind.FIXED_BYTES
token.data = bytes as u64
return token
return new Value(ValueKind.FIXED_BYTES, changetype<u32>(bytes))
}

static fromI32(i: i32): Value {
let token = new Value()
token.kind = ValueKind.INT
token.data = BigInt.fromI32(i) as u64
return token
return new Value(ValueKind.INT, changetype<u32>(BigInt.fromI32(i)))
}

static fromSignedBigInt(i: BigInt): Value {
let token = new Value()
token.kind = ValueKind.INT
token.data = i as u64
return token
return new Value(ValueKind.INT, changetype<u32>(i))
}

static fromUnsignedBigInt(i: BigInt): Value {
let token = new Value()
token.kind = ValueKind.UINT
token.data = i as u64
return token
return new Value(ValueKind.UINT, changetype<u32>(i))
}

static fromString(s: string): Value {
let token = new Value()
token.kind = ValueKind.STRING
token.data = s as u64
return token
return new Value(ValueKind.STRING, changetype<u32>(s))
}

static fromArray(values: Array<Value>): Value {
let token = new Value()
token.kind = ValueKind.ARRAY
token.data = values as u64
return token
return new Value(ValueKind.ARRAY, changetype<u32>(values))
}

static fromFixedSizedArray(values: Array<Value>): Value {
let token = new Value()
token.kind = ValueKind.FIXED_ARRAY
token.data = values as u64
return token
return new Value(ValueKind.FIXED_ARRAY, changetype<u32>(values))
}

static fromTuple(values: Tuple): Value {
let token = new Value()
token.kind = ValueKind.TUPLE
token.data = values as u64
return token
return new Value(ValueKind.TUPLE, changetype<u32>(values))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ended up a bit nicer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah haha 😄

}

static fromTupleArray(values: Array<Tuple>): Value {
Expand Down Expand Up @@ -345,67 +327,67 @@ export namespace ethereum {
* An Ethereum block.
*/
export class Block {
hash: Bytes
parentHash: Bytes
unclesHash: Bytes
author: Address
stateRoot: Bytes
transactionsRoot: Bytes
receiptsRoot: Bytes
number: BigInt
gasUsed: BigInt
gasLimit: BigInt
timestamp: BigInt
difficulty: BigInt
totalDifficulty: BigInt
size: BigInt | null
hash!: Bytes
parentHash!: Bytes
unclesHash!: Bytes
author!: Address
stateRoot!: Bytes
transactionsRoot!: Bytes
receiptsRoot!: Bytes
number!: BigInt
gasUsed!: BigInt
gasLimit!: BigInt
timestamp!: BigInt
difficulty!: BigInt
totalDifficulty!: BigInt
size!: BigInt | null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm what's the meaning of ! in a field that is | null? I see this happens a few times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because of this.

Basically when the properties of an exported class don't have a constructor, it requires to either create one, or to add the !. I think its related to how you intend on setting them.

I prefered this way instead of the constructors because as far as I know only graph-node will populate these and pass to the subgraph developer.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that annotating with ! will yield a runtime check in Wasm, while assigning in the constructor will not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the warning! I guess I'll change it then 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcodeIO I was discussing here with @leoyvens and we're a bit confused.

This check you're talking about, is it like an assertion to see if the property is null or not?

Copy link

@dcodeIO dcodeIO Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A runtime check is inserted whenever a property annotated with ! is accessed (this is essentially the developer saying "I promise this is always set"), guaranteeing that the value is in fact always set. On the contrary, if the constructor would always assign a non-nullable value, or if the type of the field would be nullable (or of a type that can trivially be zero), such a runtime check would not be necessary, as the condition can be evaluated statically, respectively if nullable or similar, cannot by definition lead to type confusion. What this ensures, basically, is that there'll never be a null value with a non-nullable type like BigInt.

}

/**
* An Ethereum transaction.
*/
export class Transaction {
hash: Bytes
index: BigInt
from: Address
to: Address | null
value: BigInt
gasLimit: BigInt
gasPrice: BigInt
input: Bytes
hash!: Bytes
index!: BigInt
from!: Address
to!: Address | null
value!: BigInt
gasLimit!: BigInt
gasPrice!: BigInt
input!: Bytes
}

/**
* Common representation for Ethereum smart contract calls.
*/
export class Call {
to: Address
from: Address
block: Block
transaction: Transaction
inputValues: Array<EventParam>
outputValues: Array<EventParam>
to!: Address
from!: Address
block!: Block
transaction!: Transaction
inputValues!: Array<EventParam>
outputValues!: Array<EventParam>
}

/**
* Common representation for Ethereum smart contract events.
*/
export class Event {
address: Address
logIndex: BigInt
transactionLogIndex: BigInt
logType: string | null
block: Block
transaction: Transaction
parameters: Array<EventParam>
address!: Address
logIndex!: BigInt
transactionLogIndex!: BigInt
logType!: string | null
block!: Block
transaction!: Transaction
parameters!: Array<EventParam>
}

/**
* A dynamically-typed Ethereum event parameter.
*/
export class EventParam {
name: string
value: Value
name!: string
value!: Value
}

export class SmartContractCall {
Expand Down Expand Up @@ -452,7 +434,7 @@ export namespace ethereum {
name +
'` to handle this in the mapping.',
)
return result as Array<Value>
return changetype<Array<Value>>(result)
}

tryCall(
Expand All @@ -465,7 +447,7 @@ export namespace ethereum {
if (result == null) {
return new CallResult()
} else {
return CallResult.fromValue(result as Array<Value>)
return CallResult.fromValue(changetype<Array<Value>>(result))
}
}
}
Expand Down Expand Up @@ -494,7 +476,7 @@ export namespace ethereum {
'accessed value of a reverted call, ' +
'please check the `reverted` field before accessing the `value` field',
)
return (this._value as Wrapped<T>).inner
return changetype<Wrapped<T>>(this._value).inner
}
}
}
Loading