Published
Edited
Mar 9, 2022
Insert cell
Insert cell
Insert cell
Insert cell
sender = "evmos1dx67l23hz9l0k9hcher8xz04uj7wf3yu26l2yn"
Insert cell
dest = converter.ethToEvmos('0x31d370c8b3dDc16388B60A24b2d316519C4E65e1')
Insert cell
addrRawData = await fetch(
`${RPC_URL}:1317${provider.accountEndpoint}${sender}`,
httpOptions
);
Insert cell
addrData = await addrRawData.json()
Insert cell
test('metamask')
// test('keplr') // not working yet
Insert cell
txconfig = ({
fee: {
amount: '20',
denom: 'aevmos',
gas: '200000',
},
memo: '',
params: {
destinationAddress: dest,
amount: '10000000000000000000',
denom: 'aevmos',
},
})
Insert cell
async function test(wallet = 'metamask') {
let _sender = addrData.account.base_account
_sender = {
..._sender,
accountAddress: _sender.address,
pubkey: _sender.pub_key.key,
accountNumber: parseInt(_sender.account_number),
sequence: parseInt(_sender.sequence),
}
const msg = await prepareTx(_sender, txconfig);
console.log('msg', msg);
if (wallet == 'metamask') {
const response = await sendTxMetamask(_sender, msg);
console.log('response', response);
return response;
}
else {
const response = await sendTxKeplr(_sender, msg);
console.log('response', response);
return response;
}
}
Insert cell
async function prepareTx(sender, txconfig) {
return tx.createMessageSend(chain, sender, txconfig.fee, txconfig.memo, txconfig.params)
}
Insert cell
async function sendTxMetamask (sender, msg) {
// Init Metamask
await window.ethereum.enable();

console.log(sender, sender.accountAddress)
const _sender = converter.evmosToEth(sender.accountAddress)
console.log('_sender', _sender)

// Request the signature
let signature = await window.ethereum.request({
method: 'eth_signTypedData_v4',
params: [_sender, JSON.stringify(msg.eipToSign)],
});
console.log('signature', signature);

let extension = tx.signatureToWeb3Extension(chain, sender, signature)
let rawTx = tx.createTxRawEIP712(msg.legacyAmino.body, msg.legacyAmino.authInfo, extension)
console.log('rawTx', rawTx);
// Broadcast it
const postOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: `{ "tx_bytes": [${rawTx.message
.serializeBinary()
.toString()}], "mode": "BROADCAST_MODE_SYNC" }`,
};

let broadcastPost = await fetch(
`${RPC_URL}:1317${provider.broadcastEndpoint}`,
postOptions
);
let response = await broadcastPost.json();
return response;
}
Insert cell
async function sendTxKeplr(sender, msg) {
const connected = await KeplrConnect();
if (!connected) throw new Error("Keplr not connected")

const offlineSigner = window.getOfflineSigner(chain.chainId);
console.log('offlineSigner', offlineSigner);
const accounts = await offlineSigner.getAccounts();
console.log('accounts', accounts);

// body, authInfo, signBytes
console.log('signDirect', msg.signDirect)

// const cosmJS = new SigningCosmosClient(
// "https://rpc-osmosis.blockapsis.com",
// accounts[0].address,
// offlineSigner,
// );
// const client = await SigningStargateClient.connectWithSigner(
// "https://rpc-osmosis.blockapsis.com",
// offlineSigner
// )

// const amountFinal = {
// denom: 'aevmos',
// amount: amount.toString(),
// }
// const fee = {
// amount: [{
// denom: 'aevmos',
// amount: '5000',
// }, ],
// gas: '200000',
// }
// const result = await client.sendTokens(accounts[0].address, recipient, [amountFinal], fee, "")

// if (result.code !== undefined &&
// result.code !== 0) {
// alert("Failed to send tx: " + result.log || result.rawLog);
// } else {
// alert("Succeed to send tx:" + result.transactionHash);
// }
}
Insert cell
async function KeplrConnect() {
if (!window.keplr.experimentalSuggestChain) {
console.error("No Keplr API found")
return false;
}
try {
await window.keplr.experimentalSuggestChain({
chainId: chain.chainId,
chainName: chain.name,
rpc: chain.rpc,
rest: chain.rest,
stakeCurrency: {
coinDenom: "evmos",
coinMinimalDenom: "aevmos",
coinDecimals: 18,
},
bip44: {
coinType: 60,
},
bech32Config: {
bech32PrefixAccAddr: "evmos",
bech32PrefixAccPub: "evmospub",
bech32PrefixValAddr: "evmosvaloper",
bech32PrefixValPub: "evmosvaloperpub",
bech32PrefixConsAddr: "evmosvalcons",
bech32PrefixConsPub: "evmosvalconspub"
},
currencies: [{
coinDenom: "evmos",
coinMinimalDenom: "aevmos",
coinDecimals: 18,
}],
feeCurrencies: [{
coinDenom: "evmos",
coinMinimalDenom: "aevmos",
coinDecimals: 18,
}],
coinType: 118,
gasPriceStep: {
low: 0.01,
average: 0.025,
high: 0.04
}
});
} catch (e) {
console.error("Failed to suggest the chain", e);
}

await window.keplr.enable(chain.chainId);
}
Insert cell
Insert cell
// RPC_URL = 'https://eth.bd.evmos.dev'
RPC_URL = 'http://localhost'
Insert cell
Insert cell
httpOptions = ({
method: 'GET',
headers: { 'Content-Type': 'application/json' },
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more