Skip to content

Commit 78f4992

Browse files
authored
chore: example for msg send with eip-712 (evmos#83)
1 parent 419bae1 commit 78f4992

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Message Send with Eip712
2+
3+
Send coins to another wallet and check wallet balance
4+
5+
```ts
6+
import { ethToEvmos } from '@evmos/address-converter'
7+
import { Wallet } from '@ethersproject/wallet'
8+
import { getSender, MAINNET_CHAIN, MAINNET_FEE, broadcast, singTransactionUsingEIP712 } from '@hanchon/evmos-ts-wallet'
9+
import { createMessageSend, MessageSendParams, Sender} from '@evmos/transactions'
10+
11+
// CONSTANTS
12+
const privateKey = "pluck view carry maid bamboo river major where dutch wood certain oval order wise awkward clerk adult summer because number raven coil crunch hat"
13+
const url = 'https://rest.bd.evmos.org:1317'
14+
const urlBalance = 'https://rest.bd.evmos.org:1317/cosmos/bank/v1beta1/balances/'
15+
16+
17+
async function fetchGet(route: string){
18+
try {
19+
const request = await fetch(route)
20+
if (request.status !== 200){
21+
return "0"
22+
}
23+
const response = await request.json()
24+
return response
25+
} catch (e) {
26+
console.log(e);
27+
console.error("Error calling fetch GET: " + e);
28+
return "0";
29+
}
30+
}
31+
32+
async function getWalletBalance (wallet: string, urlBalance: string): Promise<string> {
33+
// given a wallet, it returns the balances and pagination
34+
const requestUrl = urlBalance + wallet
35+
let response = await fetchGet(requestUrl)
36+
// if there was an error
37+
if (response === "0"){
38+
return "0"
39+
}
40+
41+
const balances = response.balances
42+
43+
if (balances.length !== 0){
44+
return balances[0].amount
45+
}else {
46+
return "0"
47+
}
48+
}
49+
50+
async function signAndBroadcastTx(wallet: Wallet, sender: Sender, url: string) {
51+
const params: MessageSendParams = {
52+
destinationAddress: 'evmos1pmk2r32ssqwps42y3c9d4clqlca403yd9wymgr',
53+
amount: '1',
54+
denom: 'aevmos',
55+
}
56+
let fee = MAINNET_FEE
57+
// the fee amount it has to be bigger than the gas
58+
fee.gas = "150000"
59+
fee.amount = '3000000000000000'
60+
61+
// create the message
62+
const message = createMessageSend(MAINNET_CHAIN, sender, MAINNET_FEE, 'message', params)
63+
// sign the tx
64+
let txBody = await singTransactionUsingEIP712(wallet, sender.accountAddress, message, MAINNET_CHAIN)
65+
// broadcast the tx signed
66+
await broadcast(txBody, url)
67+
}
68+
69+
async function main(){
70+
// create a wallet using the private keys
71+
const wallet = Wallet.fromMnemonic(privateKey)
72+
// For sending txns it's needed: accountNumber, sequence, pubkey
73+
const sender = await getSender(wallet, url)
74+
/* getSender: returns the accountAddress, the sequence,
75+
the accountNumber and the pubkey
76+
If the pubkey is not created, it has a method that 'creates' it
77+
*/
78+
await signAndBroadcastTx(wallet, sender, url)
79+
await getWalletBalance(sender.accountAddress, urlBalance)
80+
}
81+
82+
main()

0 commit comments

Comments
 (0)