registry-sdk/src/bond.ts

254 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-03-31 10:00:02 +00:00
import {
createEIP712,
generateFee,
generateMessage,
generateTypes,
} from '@tharsis/eip712'
import {
Chain,
Sender,
Fee,
} from '@tharsis/transactions'
import { createTransaction } from '@tharsis/proto'
import * as bondTx from './proto/vulcanize/bond/v1beta1/tx'
import * as coin from './proto/cosmos/base/v1beta1/coin'
const MSG_CREATE_BOND_TYPES = {
MsgValue: [
{ name: 'signer', type: 'string' },
2022-04-01 03:31:00 +00:00
{ name: 'coins', type: 'TypeCoins[]' },
2022-03-31 10:00:02 +00:00
],
2022-04-01 03:31:00 +00:00
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: [
2022-03-31 10:00:02 +00:00
{ name: 'denom', type: 'string' },
{ name: 'amount', type: 'string' },
],
}
export interface MessageMsgCreateBond {
amount: string
denom: string
}
2022-04-01 03:31:00 +00:00
export interface MessageMsgRefillBond {
id: string,
amount: string
denom: string
}
2022-03-31 10:00:02 +00:00
export function createTxMsgCreateBond(
chain: Chain,
sender: Sender,
fee: Fee,
memo: string,
params: MessageMsgCreateBond,
) {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(MSG_CREATE_BOND_TYPES)
const msg = createMsgCreateBond(
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 = protoCreateMsgCreateBond(
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,
}
}
2022-04-01 03:31:00 +00:00
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,
}
}
2022-03-31 10:00:02 +00:00
function createMsgCreateBond(
signer: string,
amount: string,
denom: string,
) {
return {
type: 'bond/MsgCreateBond',
value: {
coins: [
{
amount,
denom,
},
],
2022-04-01 03:31:00 +00:00
signer
2022-03-31 10:00:02 +00:00
},
}
}
const protoCreateMsgCreateBond = (
signer: string,
amount: string,
denom: string,
) => {
const value = new coin.cosmos.base.v1beta1.Coin({
denom,
amount,
})
2022-04-01 03:31:00 +00:00
const createBondMessage = new bondTx.vulcanize.bond.v1beta1.MsgCreateBond({
2022-03-31 10:00:02 +00:00
signer,
coins: [value]
})
return {
2022-04-01 03:31:00 +00:00
message: createBondMessage,
2022-03-31 10:00:02 +00:00
path: 'vulcanize.bond.v1beta1.MsgCreateBond',
}
}
2022-04-01 03:31:00 +00:00
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',
}
}