-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhubExample.js
More file actions
139 lines (127 loc) · 3.32 KB
/
Copy pathhubExample.js
File metadata and controls
139 lines (127 loc) · 3.32 KB
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
const { JsonRpcProvider } = require("ethers/providers");
const { Wallet, ethers } = require("ethers");
const { any } = require("@any-sender/client");
// prerequisites
const message = "Hello world";
const userWallet = new Wallet("<private key here>");
const provider = new JsonRpcProvider(
"https://ropsten.infura.io/v3/7333c8bcd07b4a179b0b0a958778762b"
);
// An echo contract with the _msgSender() standard.
const echoContractAddress = "0x23a2F5caD758a2b9a46969C3742D6dfB245CE0cf";
const echoAbi = [
{
inputs: [
{
internalType: "address",
name: "_relayHub",
type: "address",
},
],
stateMutability: "payable",
type: "constructor",
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "address",
name: "from",
type: "address",
},
{
indexed: false,
internalType: "string",
name: "message",
type: "string",
},
],
name: "Broadcast",
type: "event",
},
{
inputs: [],
name: "lastMessage",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "relayHub",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "string",
name: "_message",
type: "string",
},
],
name: "sendMessage",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
];
const run = async (
userWallet,
provider,
message,
echoContractAddress,
echoAbi
) => {
// the wallet must be connected to a provider
const connectedUser = userWallet.connect(provider);
// set up the any.sender client & proxy accounts
const userAnyWallet = any.senderHub(connectedUser);
// construct the data we want to send
const echoInterface = new ethers.utils.Interface(echoAbi);
const data = echoInterface.functions.sendMessage.encode([
`-- ${message} -- (at ${new Date(Date.now()).toString()})`,
]);
const blockBeforeSend = await provider.getBlockNumber();
// use the any.sendTransaction function to send via any.sender
const relayReceipt = await userAnyWallet.any.sendTransaction({
to: echoContractAddress,
data: data,
gasLimit: 200000,
});
// wait until the transaction is mined
console.log("Transaction sent, waiting for blocks to be mined.");
const txReceipt = await relayReceipt.wait();
const blocksUntilMined = txReceipt.blockNumber - blockBeforeSend;
console.log(
`Tx relayed after ${blocksUntilMined - 1} block${
blocksUntilMined > 2 ? "s" : ""
}. Pretty cool, I guess. (⌐■_■)`
);
console.log(
`See your message at https://ropsten.etherscan.io/tx/${txReceipt.transactionHash}#eventlog`
);
const event = echoInterface.events.Broadcast.decode(
txReceipt.logs[1].data,
txReceipt.logs[1].topics
);
console.log("Your signing key address: " + userAnyWallet.address);
console.log("msg.sender for echo contract: " + event[0]);
console.log("Message: " + event[1]);
console.log("All sent via a RelayHub contract!");
};
run(userWallet, provider, message, echoContractAddress, echoAbi);