Fix create bond message

This commit is contained in:
nabarun 2022-04-01 09:01:00 +05:30 committed by Ashwin Phatak
parent 4c3a3558d3
commit 6bb264d69c
4 changed files with 166 additions and 10 deletions

View File

@ -75,3 +75,9 @@ Follow these steps to run the tests:
# Check votes tally # Check votes tally
ethermintd query gov tally 1 ethermintd query gov tally 1
``` ```
- Check bonds list:
```bash
ethermintd query bond list
```

View File

@ -17,9 +17,21 @@ import * as coin from './proto/cosmos/base/v1beta1/coin'
const MSG_CREATE_BOND_TYPES = { const MSG_CREATE_BOND_TYPES = {
MsgValue: [ MsgValue: [
{ name: 'signer', type: 'string' }, { name: 'signer', type: 'string' },
{ name: 'coins', type: 'TypeCoin[]' }, { name: 'coins', type: 'TypeCoins[]' },
], ],
TypeCoin: [ TypeCoins: [
{ name: 'denom', type: 'string' },
{ name: 'amount', type: 'string' },
],
}
const MSG_REFILL_BOND_TYPES = {
MsgValue: [
{ name: 'id', type: 'string' },
{ name: 'signer', type: 'string' },
{ name: 'coins', type: 'TypeCoins[]' },
],
TypeCoins: [
{ name: 'denom', type: 'string' }, { name: 'denom', type: 'string' },
{ name: 'amount', type: 'string' }, { name: 'amount', type: 'string' },
], ],
@ -30,6 +42,12 @@ export interface MessageMsgCreateBond {
denom: string denom: string
} }
export interface MessageMsgRefillBond {
id: string,
amount: string
denom: string
}
export function createTxMsgCreateBond( export function createTxMsgCreateBond(
chain: Chain, chain: Chain,
sender: Sender, sender: Sender,
@ -89,6 +107,67 @@ export function createTxMsgCreateBond(
} }
} }
export function createTxMsgRefillBond(
chain: Chain,
sender: Sender,
fee: Fee,
memo: string,
params: MessageMsgRefillBond,
) {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(MSG_REFILL_BOND_TYPES)
const msg = createMsgRefillBond(
params.id,
sender.accountAddress,
params.amount,
params.denom
)
const messages = generateMessage(
sender.accountNumber.toString(),
sender.sequence.toString(),
chain.cosmosChainId,
memo,
feeObject,
msg,
)
const eipToSign = createEIP712(types, chain.chainId, messages)
// Cosmos
const msgCosmos = protoCreateMsgRefillBond(
params.id,
sender.accountAddress,
params.amount,
params.denom
)
const tx = createTransaction(
msgCosmos,
memo,
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
'ethsecp256',
sender.pubkey,
sender.sequence,
sender.accountNumber,
chain.cosmosChainId,
)
return {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
}
}
function createMsgCreateBond( function createMsgCreateBond(
signer: string, signer: string,
amount: string, amount: string,
@ -97,13 +176,13 @@ function createMsgCreateBond(
return { return {
type: 'bond/MsgCreateBond', type: 'bond/MsgCreateBond',
value: { value: {
signer,
coins: [ coins: [
{ {
amount, amount,
denom, denom,
}, },
], ],
signer
}, },
} }
} }
@ -118,13 +197,57 @@ const protoCreateMsgCreateBond = (
amount, amount,
}) })
const depositMessage = new bondTx.vulcanize.bond.v1beta1.MsgCreateBond({ const createBondMessage = new bondTx.vulcanize.bond.v1beta1.MsgCreateBond({
signer, signer,
coins: [value] coins: [value]
}) })
return { return {
message: depositMessage, message: createBondMessage,
path: 'vulcanize.bond.v1beta1.MsgCreateBond', path: 'vulcanize.bond.v1beta1.MsgCreateBond',
} }
} }
function createMsgRefillBond(
id: string,
signer: string,
amount: string,
denom: string,
) {
return {
type: 'bond/MsgRefillBond',
value: {
coins: [
{
amount,
denom,
},
],
id,
signer
},
}
}
const protoCreateMsgRefillBond = (
id: string,
signer: string,
amount: string,
denom: string,
) => {
const value = new coin.cosmos.base.v1beta1.Coin({
denom,
amount,
})
const refillBondMessage = new bondTx.vulcanize.bond.v1beta1.MsgRefillBond({
id,
signer,
coins: [value]
})
return {
message: refillBondMessage,
path: 'vulcanize.bond.v1beta1.MsgRefillBond',
}
}

View File

@ -1,8 +1,8 @@
import { createBond, sendDeposit, sendTokens, sendVote } from './index' import { createBond, sendDeposit, sendTokens, sendVote } from './index'
const SENDER_ADDRESS = 'ethm1ztkuzewqh0att04kn4gpkfk7vupmylcp3gr4zj'; const SENDER_ADDRESS = 'ethm1kgwzff36qmx5tvfvfr7wvdurp5mr25csyqxgdm';
const SENDER_PRIVATE_KEY = '765082238ae967c6e6514a7984410dfe268f233959a01c7dc746e8a5ac0faa9a'; const SENDER_PRIVATE_KEY = '12e94bcc0daecd936b499f3eeb3b3b76ac1410cbaff2ee6c6f64d768453db0cf';
const TO_ADDRESS = 'ethm1h3drdgazq4m4gtnxtl6kkuq3cxczrmnq6u24eq'; const TO_ADDRESS = 'ethm1e6r855un2ufnne9cdpujvan5srxjand37pepuz';
test('Send tokens', async () => { test('Send tokens', async () => {
await sendTokens(SENDER_PRIVATE_KEY, SENDER_ADDRESS, TO_ADDRESS) await sendTokens(SENDER_PRIVATE_KEY, SENDER_ADDRESS, TO_ADDRESS)
@ -29,7 +29,7 @@ describe('Gov module', () => {
}) })
}) })
xdescribe('Bond module', () => { describe('Bond module', () => {
test('Create bond', async () => { test('Create bond', async () => {
const bondParams = { const bondParams = {
amount: '100', amount: '100',

View File

@ -12,7 +12,7 @@ import {
} from '@tharsis/transactions' } from '@tharsis/transactions'
import { createTxMsgDeposit, MessageMsgDeposit } from "./gov"; import { createTxMsgDeposit, MessageMsgDeposit } from "./gov";
import { createTxMsgCreateBond, MessageMsgCreateBond } from "./bond"; import { createTxMsgCreateBond, createTxMsgRefillBond, MessageMsgCreateBond, MessageMsgRefillBond } from "./bond";
const ETHERMINT_REST_ENDPOINT = 'http://127.0.0.1:1317' const ETHERMINT_REST_ENDPOINT = 'http://127.0.0.1:1317'
@ -140,6 +140,33 @@ export const createBond = async (senderPrivateKey: string, senderAddress: string
await signAndSendMessage(senderPrivateKey, chain, sender, msg) await signAndSendMessage(senderPrivateKey, chain, sender, msg)
} }
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)
}
const signAndSendMessage = async (senderPrivateKey: string, chain: Chain, sender: Sender, msg: any) => { const signAndSendMessage = async (senderPrivateKey: string, chain: Chain, sender: Sender, msg: any) => {
const eipMessageDomain: any = msg.eipToSign.domain; const eipMessageDomain: any = msg.eipToSign.domain;