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
use std::{rc::Rc, str::FromStr};

use eth_rpc::call_contract;
use ethers_core::{
    abi::{Contract, Token},
    types::{Address, RecoveryMessage, Signature},
};
use util::to_hex;

mod eth_rpc;
mod util;

// Load relevant ABIs (Ethereum equivalent of Candid interfaces)
thread_local! {
    static ERC_721: Rc<Contract> = Rc::new(include_abi!("../abi/erc721.json"));
    static ERC_1155: Rc<Contract> = Rc::new(include_abi!("../abi/erc1155.json"));
}

#[cfg(feature = "canpack")]
canpack::export! {
    /// Verify an ECDSA signature (message signed by an Ethereum wallet).
    #[canpack(update)]
    pub fn ecdsa_verify(eth_address: String, message: String, signature: String) -> bool {
        Signature::from_str(&signature)
            .unwrap()
            .verify(
                RecoveryMessage::Data(message.into_bytes()),
                Address::from_str(&eth_address).unwrap(),
            )
            .is_ok()
    }

    /// Find the owner of an ERC-721 NFT by calling the Ethereum blockchain.
    #[canpack(update)]
    pub async fn erc721_owner_of(network: String, contract_address: String, token_id: u64) -> String {
        // TODO: access control
        // TODO: cycles estimation for HTTP outcalls

        let abi = &ERC_721.with(Rc::clone);
        let result = call_contract(
            &network,
            contract_address,
            abi,
            "ownerOf",
            &[Token::Uint(token_id.into())],
        )
        .await;
        match result.get(0) {
            Some(Token::Address(a)) => to_hex(a.as_bytes()),
            _ => panic!("Unexpected result"),
        }
    }

    /// Find the balance of an ERC-1155 token by calling the Ethereum blockchain.
    #[canpack(update)]
    pub async fn erc1155_balance_of(
        network: String,
        contract_address: String,
        owner_address: String,
        token_id: u64,
    ) -> u128 {
        let owner_address =
            ethers_core::types::Address::from_str(&owner_address).expect("Invalid owner address");

        let abi = &ERC_1155.with(Rc::clone);
        let result = call_contract(
            &network,
            contract_address,
            abi,
            "balanceOf",
            &[
                Token::Address(owner_address.into()),
                Token::Uint(token_id.into()),
            ],
        )
        .await;
        match result.get(0) {
            Some(Token::Uint(n)) => n.as_u128(),
            _ => panic!("Unexpected result"),
        }
    }
}