1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Ethereum, in all its glory.
//!
//! TODO(nlordell): Type-safe `FixedBytes`, and `Array` values.
//! FIXME(nlordell): Make field access lazy - this avoids issues like with the
//! `TransactionReceipt` type, where some fields are `null`.

use crate::{
    address::Address,
    crypto::Hash,
    ffi::{
        boxed::{AscBox, AscRef},
        eth::{
            AscBlock, AscCall, AscEthereumSmartContractCall, AscEvent, AscEventParam, AscLog,
            AscTransaction, AscTransactionReceipt,
        },
        str::{AscStr, AscString},
        sys,
        types::AscBytes,
        value::{AscArray, AscEthereumValue, AscEthereumValueData},
    },
    num::BigInt,
};
use indexmap::IndexMap;

/// Execute an Ethereum call.
pub fn call(call: SmartContractCall) -> Option<Vec<Value>> {
    let call = call.to_raw();
    let result = unsafe {
        let result = sys::ethereum__call(call.as_ptr());
        if result.is_null() {
            return None;
        }
        &*result
    };

    Some(
        result
            .as_slice()
            .iter()
            .map(|value| Value::from_raw(value.as_asc_ref()))
            .collect(),
    )
}

/// ABI-encode and Ethereum value.
pub fn encode(value: &Value) -> Option<Vec<u8>> {
    let value = value.to_raw();
    let data = unsafe {
        let data = sys::ethereum__encode(value.as_ptr());
        if data.is_null() {
            return None;
        }
        &*data
    };
    Some(data.as_slice().to_owned())
}

/// ABI-decode bytes for the specified signature.
pub fn decode(signature: impl AsRef<str>, data: impl AsRef<[u8]>) -> Option<Value> {
    let signature = AscString::new(signature.as_ref());
    let data = AscBytes::from_bytes(data.as_ref());
    let value = unsafe {
        let value = sys::ethereum__decode(signature.as_ptr(), data.as_ptr());
        if value.is_null() {
            return None;
        }
        &*value
    };
    Some(Value::from_raw(value))
}

/// An Ethereum value.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Value {
    Address(Address),
    FixedBytes(Vec<u8>),
    Bytes(Vec<u8>),
    Int(BigInt),
    Uint(BigInt),
    Bool(bool),
    String(String),
    FixedArray(Vec<Value>),
    Array(Vec<Value>),
    Tuple(Vec<Value>),
}

impl Value {
    /// Creates a new instance from a raw value.
    fn from_raw(raw: &'static AscRef<AscEthereumValue>) -> Self {
        match raw.data() {
            AscEthereumValueData::Address(value) => Self::Address(Address::from_raw(value)),
            AscEthereumValueData::FixedBytes(value) => {
                Self::FixedBytes(value.as_slice().to_owned())
            }
            AscEthereumValueData::Bytes(value) => Self::Bytes(value.as_slice().to_owned()),
            AscEthereumValueData::Int(value) => Self::Int(BigInt::from_raw(value)),
            AscEthereumValueData::Uint(value) => Self::Uint(BigInt::from_raw(value)),
            AscEthereumValueData::Bool(value) => Self::Bool(value),
            AscEthereumValueData::String(value) => Self::String(value.to_string_lossy()),
            AscEthereumValueData::FixedArray(value) => Self::FixedArray(
                value
                    .as_slice()
                    .iter()
                    .map(|value| Value::from_raw(value.as_asc_ref()))
                    .collect(),
            ),
            AscEthereumValueData::Array(value) => Self::Array(
                value
                    .as_slice()
                    .iter()
                    .map(|value| Value::from_raw(value.as_asc_ref()))
                    .collect(),
            ),
            AscEthereumValueData::Tuple(value) => Self::Tuple(
                value
                    .as_slice()
                    .iter()
                    .map(|value| Value::from_raw(value.as_asc_ref()))
                    .collect(),
            ),
        }
    }

    /// Creates a raw AssemblyScript value.
    fn to_raw(&self) -> AscBox<AscEthereumValue> {
        match self {
            Self::Address(value) => AscEthereumValue::address(value.to_raw()),
            Self::FixedBytes(value) => AscEthereumValue::fixedbytes(AscBytes::from_bytes(value)),
            Self::Bytes(value) => AscEthereumValue::bytes(AscBytes::from_bytes(value)),
            Self::Int(value) => AscEthereumValue::int(value.as_raw().to_owned()),
            Self::Uint(value) => AscEthereumValue::uint(value.as_raw().to_owned()),
            Self::Bool(value) => AscEthereumValue::bool(*value),
            Self::String(value) => AscEthereumValue::string(AscString::new(value)),
            Self::FixedArray(value) => AscEthereumValue::fixedarray(AscArray::new(
                value.iter().map(|value| value.to_raw()).collect(),
            )),
            Self::Array(value) => AscEthereumValue::array(AscArray::new(
                value.iter().map(|value| value.to_raw()).collect(),
            )),
            Self::Tuple(value) => AscEthereumValue::tuple(AscArray::new(
                value.iter().map(|value| value.to_raw()).collect(),
            )),
        }
    }

    /// Returns the Ethereum value as a address, or `None` if the value the
    /// wrong type.
    pub fn as_address(&self) -> Option<Address> {
        match self {
            Self::Address(value) => Some(*value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a fixed bytes, or `None` if the value the
    /// wrong type.
    pub fn as_fixed_bytes(&self) -> Option<&[u8]> {
        match self {
            Self::FixedBytes(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a bytes, or `None` if the value the wrong
    /// type.
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            Self::Bytes(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a int, or `None` if the value the wrong
    /// type.
    pub fn as_int(&self) -> Option<&BigInt> {
        match self {
            Self::Int(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a uint, or `None` if the value the wrong
    /// type.
    pub fn as_uint(&self) -> Option<&BigInt> {
        match self {
            Self::Uint(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a bool, or `None` if the value the wrong
    /// type.
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(value) => Some(*value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a string, or `None` if the value the wrong
    /// type.
    pub fn as_string(&self) -> Option<&str> {
        match self {
            Self::String(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a fixed array, or `None` if the value the
    /// wrong type.
    pub fn as_fixed_array(&self) -> Option<&[Self]> {
        match self {
            Self::FixedArray(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a array, or `None` if the value the wrong
    /// type.
    pub fn as_array(&self) -> Option<&[Self]> {
        match self {
            Self::Array(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the Ethereum value as a tuple, or `None` if the value the wrong
    /// type.
    pub fn as_tuple(&self) -> Option<&[Self]> {
        match self {
            Self::Tuple(value) => Some(value),
            _ => None,
        }
    }
}

/// A 256-byte bloom filter.
pub type Bloom = [u8; 256];

/// Ethereum block data.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Block {
    pub hash: Hash,
    pub parent_hash: Hash,
    pub uncles_hash: Hash,
    pub author: Address,
    pub state_root: Hash,
    pub transactions_root: Hash,
    pub receipts_root: Hash,
    pub number: BigInt,
    pub gas_used: BigInt,
    pub gas_limit: BigInt,
    pub timestamp: BigInt,
    pub difficulty: BigInt,
    pub total_difficulty: BigInt,
    pub size: Option<BigInt>,
    pub base_fee_per_gas: Option<BigInt>,
}

/// A Raw pointer to an Ethereum block passed into a block handler.
pub type BlockPtr = *const AscRef<AscBlock>;

impl Block {
    fn from_raw(b: &'static AscRef<AscBlock>) -> Self {
        Self {
            hash: b.hash().as_slice().try_into().unwrap(),
            parent_hash: b.parent_hash().as_slice().try_into().unwrap(),
            uncles_hash: b.uncles_hash().as_slice().try_into().unwrap(),
            author: Address::from_raw(b.author()),
            state_root: b.state_root().as_slice().try_into().unwrap(),
            transactions_root: b.transactions_root().as_slice().try_into().unwrap(),
            receipts_root: b.receipts_root().as_slice().try_into().unwrap(),
            number: BigInt::from_raw(b.number()),
            gas_used: BigInt::from_raw(b.gas_used()),
            gas_limit: BigInt::from_raw(b.gas_limit()),
            timestamp: BigInt::from_raw(b.timestamp()),
            difficulty: BigInt::from_raw(b.difficulty()),
            total_difficulty: BigInt::from_raw(b.total_difficulty()),
            size: b.size().map(BigInt::from_raw),
            base_fee_per_gas: b.base_fee_per_gas().map(BigInt::from_raw),
        }
    }

    /// Creates a block from a raw pointer.
    ///
    /// # Safety
    ///
    /// This must be a pointer passed into a block handler.
    pub unsafe fn from_ptr(ptr: BlockPtr) -> Self {
        Self::from_raw(&*ptr)
    }
}

/// An Ethereum transaction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Transaction {
    pub hash: Hash,
    pub index: BigInt,
    pub from: Address,
    pub to: Option<Address>,
    pub value: BigInt,
    pub gas_limit: BigInt,
    pub gas_price: BigInt,
    pub input: Vec<u8>,
    pub nonce: BigInt,
}

impl Transaction {
    fn from_raw(t: &'static AscRef<AscTransaction>) -> Self {
        Self {
            hash: t.hash().as_slice().try_into().unwrap(),
            index: BigInt::from_raw(t.index()),
            from: Address::from_raw(t.from()),
            to: t.to().map(Address::from_raw),
            value: BigInt::from_raw(t.value()),
            gas_limit: BigInt::from_raw(t.gas_limit()),
            gas_price: BigInt::from_raw(t.gas_price()),
            input: t.input().as_slice().to_owned(),
            nonce: BigInt::from_raw(t.nonce()),
        }
    }
}

/// An Ethereum transaction receipt.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TransactionReceipt {
    pub transaction_hash: Hash,
    pub transaction_index: BigInt,
    pub block_hash: Hash,
    pub block_number: BigInt,
    pub cumulative_gas_used: BigInt,
    pub gas_used: BigInt,
    pub contract_address: Address,
    pub logs: Vec<Log>,
    pub status: BigInt,
    pub root: Hash,
    pub logs_bloom: Bloom,
}

impl TransactionReceipt {
    fn from_raw(t: &'static AscRef<AscTransactionReceipt>) -> Self {
        Self {
            transaction_hash: t.transaction_hash().as_slice().try_into().unwrap(),
            transaction_index: BigInt::from_raw(t.transaction_index()),
            block_hash: t.block_hash().as_slice().try_into().unwrap(),
            block_number: BigInt::from_raw(t.block_number()),
            cumulative_gas_used: BigInt::from_raw(t.cumulative_gas_used()),
            gas_used: BigInt::from_raw(t.gas_used()),
            contract_address: Address::from_raw(t.contract_address()),
            logs: t
                .logs()
                .as_slice()
                .iter()
                .map(|l| Log::from_raw(l.as_asc_ref()))
                .collect(),
            status: BigInt::from_raw(t.status()),
            root: t.root().as_slice().try_into().unwrap(),
            logs_bloom: t.logs_bloom().as_slice().try_into().unwrap(),
        }
    }
}

/// An Ethereum log.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Log {
    pub address: Address,
    pub topics: Vec<Hash>,
    pub data: Vec<u8>,
    pub block_hash: Hash,
    pub block_number: BigInt,
    pub transaction_hash: Hash,
    pub transaction_index: BigInt,
    pub log_index: BigInt,
    pub transaction_log_index: BigInt,
    pub log_type: Option<String>,
    pub removed: Option<bool>,
}

impl Log {
    fn from_raw(l: &'static AscRef<AscLog>) -> Self {
        Self {
            address: Address::from_raw(l.address()),
            topics: l
                .topics()
                .as_slice()
                .iter()
                .map(|t| t.as_asc_ref().as_slice().try_into().unwrap())
                .collect(),
            data: l.data().as_slice().to_owned(),
            block_hash: l.block_hash().as_slice().try_into().unwrap(),
            block_number: BigInt::from_raw(l.block_number()),
            transaction_hash: l.transaction_hash().as_slice().try_into().unwrap(),
            transaction_index: BigInt::from_raw(l.transaction_index()),
            log_index: BigInt::from_raw(l.log_index()),
            transaction_log_index: BigInt::from_raw(l.transaction_log_index()),
            log_type: l.log_type().map(AscStr::to_string_lossy),
            removed: l.removed().map(|r| **r),
        }
    }
}

/// Common representation for Ethereum smart contract calls.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Call {
    pub to: Address,
    pub from: Address,
    pub block: Block,
    pub transaction: Transaction,
    pub input_values: IndexMap<String, Value>,
    pub output_values: IndexMap<String, Value>,
}

/// A raw pointer to an Ethereum call passed into an call handler.
pub type CallPtr = *const AscRef<AscCall>;

impl Call {
    fn from_raw(c: &'static AscRef<AscCall>) -> Self {
        Self {
            to: Address::from_raw(c.to()),
            from: Address::from_raw(c.from()),
            block: Block::from_raw(c.block()),
            transaction: Transaction::from_raw(c.transaction()),
            input_values: params(c.input_values()),
            output_values: params(c.output_values()),
        }
    }

    /// Creates a call from a raw pointer.
    ///
    /// # Safety
    ///
    /// This must be a pointer passed into a call handler.
    pub unsafe fn from_ptr(ptr: CallPtr) -> Self {
        Self::from_raw(&*ptr)
    }
}

/// Common representation for Ethereum smart contract events.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Event {
    pub address: Address,
    pub log_index: BigInt,
    pub transaction_log_index: BigInt,
    pub log_type: Option<String>,
    pub block: Block,
    pub transaction: Transaction,
    pub parameters: IndexMap<String, Value>,
    pub receipt: Option<TransactionReceipt>,
}

/// An event pointer for a handler.
pub type EventPtr = *const AscRef<AscEvent>;

impl Event {
    fn from_raw(e: &'static AscRef<AscEvent>) -> Self {
        Self {
            address: Address::from_raw(e.address()),
            log_index: BigInt::from_raw(e.log_index()),
            transaction_log_index: BigInt::from_raw(e.transaction_log_index()),
            log_type: e.log_type().map(AscStr::to_string_lossy),
            block: Block::from_raw(e.block()),
            transaction: Transaction::from_raw(e.transaction()),
            parameters: params(e.parameters()),
            // FIXME(nlordell): Don't parse receipts at all for now. They are
            // non-deterministic and encode `null` for non-nullable fields.
            // <https://github.com/graphprotocol/graph-node/issues/4239>
            receipt: None,
        }
    }

    /// Creates an event from a raw pointer.
    ///
    /// # Safety
    ///
    /// This must be a pointer passed into a call handler.
    pub unsafe fn from_ptr(ptr: EventPtr) -> Self {
        Self::from_raw(&*ptr)
    }

    /// Creates an event from a raw pointer, including the transaction receipt.
    /// This is currently not the default behaviour for events because the
    /// receipts have non-deterministic `null` fields.
    ///
    /// # Safety
    ///
    /// This must be a pointer passed into a call handler and the transaction
    /// receipt is correctly initialized.
    pub unsafe fn from_ptr_with_receipt(ptr: EventPtr) -> Self {
        Self {
            receipt: (*ptr).receipt().map(TransactionReceipt::from_raw),
            ..Self::from_raw(&*ptr)
        }
    }
}

/// Converts a vector of event parameters to an index map.
pub fn params(p: &'static AscRef<AscArray<AscBox<AscEventParam>>>) -> IndexMap<String, Value> {
    p.as_slice()
        .iter()
        .map(|p| {
            let p = p.as_asc_ref();
            (p.name().to_string_lossy(), Value::from_raw(p.value()))
        })
        .collect()
}

/// An Ethereum contract reference.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Contract<'a> {
    /// The name of the contract. This is used by the host for matching with an
    /// ABI for call encoding and decoding.
    pub name: &'a str,
    /// The contract address.
    pub address: &'a Address,
}

/// A contract fuction.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Function<'a> {
    /// The function name.
    pub name: &'a str,
    /// The function signature.
    pub signature: &'a str,
}

/// An Ethereum call.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SmartContractCall<'a> {
    pub contract: Contract<'a>,
    pub function: Function<'a>,
    pub params: &'a [Value],
}

impl SmartContractCall<'_> {
    /// Converts a call into an [`AscEthereumSmartContractCall`].
    fn to_raw(self) -> AscBox<AscEthereumSmartContractCall> {
        AscEthereumSmartContractCall::new(
            AscString::new(self.contract.name),
            self.contract.address.to_raw(),
            AscString::new(self.function.name),
            AscString::new(self.function.signature),
            AscArray::new(self.params.iter().map(|value| value.to_raw()).collect()),
        )
    }
}