2022-03-29 11:01:23 +00:00
|
|
|
import axios from "axios";
|
|
|
|
import { MessageTypes, signTypedData, SignTypedDataVersion } from "@metamask/eth-sig-util";
|
2022-03-30 05:21:03 +00:00
|
|
|
import { generateEndpointAccount, generateEndpointBroadcast, generatePostBodyBroadcast } from '@tharsis/provider';
|
|
|
|
import {
|
|
|
|
createMessageSend,
|
|
|
|
createTxRawEIP712,
|
|
|
|
signatureToWeb3Extension,
|
|
|
|
createTxMsgVote,
|
|
|
|
Chain,
|
2022-03-31 10:00:02 +00:00
|
|
|
Sender,
|
|
|
|
MessageMsgVote
|
2022-03-30 05:21:03 +00:00
|
|
|
} from '@tharsis/transactions'
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-31 10:00:02 +00:00
|
|
|
import { createTxMsgDeposit, MessageMsgDeposit } from "./gov";
|
2022-04-01 03:31:00 +00:00
|
|
|
import { createTxMsgCreateBond, createTxMsgRefillBond, MessageMsgCreateBond, MessageMsgRefillBond } from "./bond";
|
2022-03-31 10:00:02 +00:00
|
|
|
|
2022-03-29 11:01:23 +00:00
|
|
|
const ETHERMINT_REST_ENDPOINT = 'http://127.0.0.1:1317'
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-29 11:01:23 +00:00
|
|
|
interface TypedMessageDomain {
|
|
|
|
name?: string;
|
|
|
|
version?: string;
|
|
|
|
chainId?: number;
|
|
|
|
verifyingContract?: string;
|
|
|
|
salt?: ArrayBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const sendTokens = async (senderPrivateKey: string, senderAddress: string, destinationAddress: string) => {
|
|
|
|
let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`)
|
|
|
|
|
|
|
|
const chain = {
|
|
|
|
chainId: 9000,
|
|
|
|
cosmosChainId: 'ethermint_9000-1',
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = {
|
|
|
|
accountAddress: addrData.account.base_account.address,
|
|
|
|
sequence: addrData.account.base_account.sequence,
|
|
|
|
accountNumber: addrData.account.base_account.account_number,
|
|
|
|
pubkey: addrData.account.base_account.pub_key.key,
|
|
|
|
}
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-29 11:01:23 +00:00
|
|
|
const fee = {
|
|
|
|
amount: '20',
|
|
|
|
denom: 'aphoton',
|
|
|
|
gas: '200000',
|
|
|
|
}
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-29 11:01:23 +00:00
|
|
|
const memo = ''
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-29 11:01:23 +00:00
|
|
|
const params = {
|
|
|
|
destinationAddress: destinationAddress,
|
|
|
|
amount: '10',
|
|
|
|
denom: 'aphoton',
|
|
|
|
}
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-29 11:01:23 +00:00
|
|
|
// Create a MsgSend transaction.
|
|
|
|
const msg = createMessageSend(chain, sender, fee, memo, params)
|
2022-03-30 05:21:03 +00:00
|
|
|
|
|
|
|
await signAndSendMessage(senderPrivateKey, chain, sender, msg)
|
|
|
|
}
|
|
|
|
|
2022-03-31 10:00:02 +00:00
|
|
|
export const sendVote = async (senderPrivateKey: string, senderAddress: string, params: MessageMsgVote) => {
|
2022-03-30 05:21:03 +00:00
|
|
|
let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`)
|
|
|
|
|
|
|
|
const chain = {
|
|
|
|
chainId: 9000,
|
|
|
|
cosmosChainId: 'ethermint_9000-1',
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = {
|
|
|
|
accountAddress: addrData.account.base_account.address,
|
|
|
|
sequence: addrData.account.base_account.sequence,
|
|
|
|
accountNumber: addrData.account.base_account.account_number,
|
|
|
|
pubkey: addrData.account.base_account.pub_key.key,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fee = {
|
|
|
|
amount: '20',
|
|
|
|
denom: 'aphoton',
|
|
|
|
gas: '200000',
|
|
|
|
}
|
|
|
|
|
|
|
|
const memo = ''
|
|
|
|
|
|
|
|
const msg = createTxMsgVote(chain, sender, fee, memo, params)
|
|
|
|
await signAndSendMessage(senderPrivateKey, chain, sender, msg)
|
2022-03-31 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const sendDeposit = async (senderPrivateKey: string, senderAddress: string, params: MessageMsgDeposit) => {
|
|
|
|
let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`)
|
|
|
|
|
|
|
|
const chain = {
|
|
|
|
chainId: 9000,
|
|
|
|
cosmosChainId: 'ethermint_9000-1',
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = {
|
|
|
|
accountAddress: addrData.account.base_account.address,
|
|
|
|
sequence: addrData.account.base_account.sequence,
|
|
|
|
accountNumber: addrData.account.base_account.account_number,
|
|
|
|
pubkey: addrData.account.base_account.pub_key.key,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fee = {
|
|
|
|
amount: '20',
|
|
|
|
denom: 'aphoton',
|
|
|
|
gas: '200000',
|
|
|
|
}
|
|
|
|
|
|
|
|
const memo = ''
|
|
|
|
|
|
|
|
const msg = createTxMsgDeposit(chain, sender, fee, memo, params)
|
|
|
|
await signAndSendMessage(senderPrivateKey, chain, sender, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createBond = async (senderPrivateKey: string, senderAddress: string, params: MessageMsgCreateBond) => {
|
|
|
|
let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`)
|
|
|
|
|
|
|
|
const chain = {
|
|
|
|
chainId: 9000,
|
|
|
|
cosmosChainId: 'ethermint_9000-1',
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = {
|
|
|
|
accountAddress: addrData.account.base_account.address,
|
|
|
|
sequence: addrData.account.base_account.sequence,
|
|
|
|
accountNumber: addrData.account.base_account.account_number,
|
|
|
|
pubkey: addrData.account.base_account.pub_key.key,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fee = {
|
|
|
|
amount: '20',
|
|
|
|
denom: 'aphoton',
|
|
|
|
gas: '200000',
|
|
|
|
}
|
|
|
|
|
|
|
|
const memo = ''
|
|
|
|
|
|
|
|
const msg = createTxMsgCreateBond(chain, sender, fee, memo, params)
|
|
|
|
await signAndSendMessage(senderPrivateKey, chain, sender, msg)
|
2022-04-01 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const refillBond = async (senderPrivateKey: string, senderAddress: string, params: MessageMsgRefillBond) => {
|
|
|
|
let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`)
|
|
|
|
|
|
|
|
const chain = {
|
|
|
|
chainId: 9000,
|
|
|
|
cosmosChainId: 'ethermint_9000-1',
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = {
|
|
|
|
accountAddress: addrData.account.base_account.address,
|
|
|
|
sequence: addrData.account.base_account.sequence,
|
|
|
|
accountNumber: addrData.account.base_account.account_number,
|
|
|
|
pubkey: addrData.account.base_account.pub_key.key,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fee = {
|
|
|
|
amount: '20',
|
|
|
|
denom: 'aphoton',
|
|
|
|
gas: '200000',
|
|
|
|
}
|
|
|
|
|
|
|
|
const memo = ''
|
|
|
|
|
|
|
|
const msg = createTxMsgRefillBond(chain, sender, fee, memo, params)
|
|
|
|
await signAndSendMessage(senderPrivateKey, chain, sender, msg)
|
2022-03-30 05:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const signAndSendMessage = async (senderPrivateKey: string, chain: Chain, sender: Sender, msg: any) => {
|
2022-03-29 11:01:23 +00:00
|
|
|
const eipMessageDomain: any = msg.eipToSign.domain;
|
|
|
|
|
|
|
|
// Sign transaction.
|
|
|
|
const signature = signTypedData({
|
|
|
|
data: {
|
|
|
|
types: msg.eipToSign.types as MessageTypes,
|
|
|
|
primaryType: msg.eipToSign.primaryType,
|
|
|
|
domain: eipMessageDomain as TypedMessageDomain,
|
|
|
|
message: msg.eipToSign.message as Record<string, unknown>
|
2022-03-24 06:40:03 +00:00
|
|
|
},
|
2022-03-29 11:01:23 +00:00
|
|
|
privateKey: Buffer.from(senderPrivateKey, 'hex'),
|
|
|
|
version: SignTypedDataVersion.V4
|
|
|
|
})
|
|
|
|
|
|
|
|
let extension = signatureToWeb3Extension(chain, sender, signature)
|
|
|
|
|
2022-03-30 05:21:03 +00:00
|
|
|
// Create the txRaw.
|
2022-03-29 11:01:23 +00:00
|
|
|
let rawTx = createTxRawEIP712(msg.legacyAmino.body, msg.legacyAmino.authInfo, extension)
|
|
|
|
|
|
|
|
const body = generatePostBodyBroadcast(rawTx)
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-30 05:21:03 +00:00
|
|
|
// Broadcast transaction.
|
|
|
|
return axios.post(
|
2022-03-29 11:01:23 +00:00
|
|
|
`${ETHERMINT_REST_ENDPOINT}${generateEndpointBroadcast()}`,
|
|
|
|
JSON.parse(body)
|
|
|
|
)
|
2022-03-24 06:40:03 +00:00
|
|
|
|
2022-03-30 05:21:03 +00:00
|
|
|
// TODO: Check for successful broadcast.
|
2022-03-24 06:40:03 +00:00
|
|
|
}
|