Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly convert osmosis legacy transactions #17

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/proto-tx-builder",
"version": "0.6.0",
"version": "0.7.0",
"description": "Builds and signs new-style protobuf SIGN_MODE_DIRECT Cosmos SDK transactions",
"author": "ShapeShift DAO",
"license": "MIT",
Expand Down Expand Up @@ -43,3 +43,5 @@
"osmojs": "^0.37.0"
}
}


103 changes: 75 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import BN from 'bn.js'
import { osmosis, thorchain } from './amino'
import * as codecs from './proto'

type AgnosticStdTx = Omit<amino.StdTx, 'msg'> &
pastaghost marked this conversation as resolved.
Show resolved Hide resolved
pastaghost marked this conversation as resolved.
Show resolved Hide resolved
(
| {
readonly msg?: readonly amino.AminoMsg[]
pastaghost marked this conversation as resolved.
Show resolved Hide resolved
msgs?: never;
}
| {
readonly msgs?: readonly amino.AminoMsg[]
pastaghost marked this conversation as resolved.
Show resolved Hide resolved
msg: never;
pastaghost marked this conversation as resolved.
Show resolved Hide resolved
}
)

export interface ProtoTx {
readonly msg: readonly EncodeObject[]
readonly fee: {
Expand Down Expand Up @@ -76,6 +88,7 @@ export async function sign(
myRegistry.register('/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn', codecs.osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn)
myRegistry.register('/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut', codecs.osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut)
myRegistry.register('/osmosis.gamm.v1beta1.SwapAmountInRoute', codecs.osmosis.gamm.v1beta1.SwapAmountInRoute)
myRegistry.register('/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn', codecs.osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn)
myRegistry.register('/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn', codecs.osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn)
myRegistry.register('/osmosis.lockup.MsgLockTokens', codecs.osmosis.lockup.MsgLockTokens)
myRegistry.register('/osmosis.lockup.MsgBeginUnlocking', codecs.osmosis.lockup.MsgBeginUnlocking)
Expand Down Expand Up @@ -122,31 +135,34 @@ const scrubCoin = (x: Coin) => {

const scrubCoins = (x: readonly Coin[]) => x.filter(c => c.amount).map(scrubCoin)

type Route = { poolId: unknown; tokenOutDenom: unknown }
type Route = { pool_id: unknown; token_out_denom: unknown }

const scrubRoute = (x: Route) => {
if (!x.poolId) throw new Error('missing route poolId')
if (!x.tokenOutDenom) throw new Error('missing route tokenOutDenom')
if (!x.pool_id) throw new Error('missing route pool_id')
if (!x.token_out_denom) throw new Error('missing route token_out_denom')

return {
poolId: x.poolId,
tokenOutDenom: x.tokenOutDenom
poolId: x.pool_id,
tokenOutDenom: x.token_out_denom
}
}

const scrubRoutes = (x: Route[]) => x.map(scrubRoute)

function parse_legacy_tx_format({ fee, memo, msg, signatures }: amino.StdTx): ProtoTx {
if (msg.length !== 1) throw new Error('multiple msgs not supported!')
function parse_legacy_tx_format(tx: AgnosticStdTx): ProtoTx {
const msgOrMsgs = tx.msg ?? tx.msgs
pastaghost marked this conversation as resolved.
Show resolved Hide resolved
if(!msgOrMsgs) throw new Error('msgs array improperly formatted!')

if (msgOrMsgs.length !== 1) throw new Error('multiple msgs not supported!')

return {
...convertLegacyMsg(msg[0]),
...convertLegacyMsg(msgOrMsgs[0]),
fee: {
amount: scrubCoins(fee.amount),
gas: fee.gas
amount: scrubCoins(tx.fee.amount),
gas: tx.fee.gas
},
memo: memo,
signatures: signatures,
memo: tx.memo,
signatures: tx.signatures,
}
}

Expand Down Expand Up @@ -306,52 +322,70 @@ function convertLegacyMsg(msg: amino.AminoMsg): Pick<ProtoTx, 'msg'> {
}
case 'osmosis/gamm/swap-exact-amount-in':
if (!msg.value.sender) throw new Error('Missing sender in msg')
if (!msg.value.tokenIn) throw new Error('Missing tokenIn in msg')
if (!msg.value.tokenOutMinAmount) throw new Error('Missing tokenOutMinAmount in msg')
if (!msg.value.token_in) throw new Error('Missing token_in in msg')
pastaghost marked this conversation as resolved.
Show resolved Hide resolved
if (!msg.value.token_out_min_amount) throw new Error('Missing token_out_min_amount in msg')
if (msg.value.routes.length !== 1) throw new Error('bad routes length')

return {
msg: [{
typeUrl: '/osmosis.gamm.v1beta1.MsgSwapExactAmountIn',
value: {
sender: msg.value.sender,
tokenIn: scrubCoin(msg.value.tokenIn),
tokenOutMinAmount: msg.value.tokenOutMinAmount,
tokenIn: scrubCoin(msg.value.token_in),
tokenOutMinAmount: msg.value.token_out_min_amount,
routes: scrubRoutes(msg.value.routes)
}
}]
}
case 'osmosis/gamm/join-swap-extern-amount-in':
if (!msg.value.pool_id) throw new Error('Missing pool_id in msg')
if (!msg.value.sender) throw new Error('Missing sender in msg')
if (!msg.value.share_out_min_amount) throw new Error('Missing share_out_min_amount in msg')
if (!msg.value.tokenIn) throw new Error('Missing tokenIn in msg')

return {
msg: [{
typeUrl: '/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn',
value: {
poolId: msg.value.pool_id,
sender: msg.value.sender,
shareOutMinAmount: msg.value.share_out_min_amount,
tokenIn: scrubCoin(msg.value.token_in),

}
}]
}
case 'osmosis/gamm/join-pool':
if (!msg.value.sender) throw new Error('Missing sender in msg')
if (!msg.value.poolId) throw new Error('Missing poolId in msg')
if (!msg.value.shareOutAmount) throw new Error('Missing shareOutAmount in msg')
if (msg.value.tokenInMaxs.length !== 2) throw new Error('bad tokenInMaxs length')
if (!msg.value.pool_id) throw new Error('Missing pool_id in msg')
if (!msg.value.share_out_amount) throw new Error('Missing share_out_amount in msg')
if (msg.value.token_in_maxs.length !== 2) throw new Error('Bad token_in_maxs length')

return {
msg: [{
typeUrl: '/osmosis.gamm.v1beta1.MsgJoinPool',
value: {
sender: msg.value.sender,
poolId: msg.value.poolId,
shareOutAmount: msg.value.shareOutAmount,
tokenInMaxs: scrubCoins(msg.value.tokenInMaxs)
poolId: msg.value.pool_id,
shareOutAmount: msg.value.share_out_amount,
tokenInMaxs: scrubCoins(msg.value.token_in_maxs)
}
}]
}
case 'osmosis/gamm/exit-pool':
if (!msg.value.sender) throw new Error('Missing sender in msg')
if (!msg.value.poolId) throw new Error('Missing poolId in msg')
if (!msg.value.shareInAmount) throw new Error('Missing shareInAmount in msg')
if (msg.value.tokenOutMins.length !== 2) throw new Error('bad tokenOutMins length')
if (!msg.value.pool_id) throw new Error('Missing pool_id in msg')
if (!msg.value.share_in_amount) throw new Error('Missing share_in_amount in msg')
if (msg.value.token_out_mins.length !== 2) throw new Error('Bad token_out_mins length')

return {
msg: [{
typeUrl: '/osmosis.gamm.v1beta1.MsgExitPool',
value: {
sender: msg.value.sender,
poolId: msg.value.poolId,
shareInAmount: msg.value.shareInAmount,
tokenOutMins: scrubCoins(msg.value.tokenOutMins)
poolId: msg.value.pool_id,
shareInAmount: msg.value.share_in_amount,
tokenOutMins: scrubCoins(msg.value.token_out_mins)
}
}]
}
Expand Down Expand Up @@ -386,6 +420,19 @@ function convertLegacyMsg(msg: amino.AminoMsg): Pick<ProtoTx, 'msg'> {
}
}]
}
case 'osmosis/lockup/begin-unlock-by-id':
if (!msg.value.id) throw new Error('Missing id in msg')
if (!msg.value.owner) throw new Error('Missing owner in msg')

return {
msg: [{
typeUrl: '/osmosis.lockup.MsgBeginUnlocking',
value: {
owner: msg.value.owner,
id: msg.value.id
}
}]
}
default:
throw new Error('Unhandled tx type! type: ' + msg.type)
}
Expand Down
6 changes: 3 additions & 3 deletions src/reference-data/defi/tx01.mainnet.osmosis.lp-add.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"type": "osmosis/gamm/join-pool",
"value": {
"sender": "osmo15cenya0tr7nm3tz2wn3h3zwkht2rxrq7g9ypmq",
"poolId": "1",
"shareOutAmount": "402238349184328773",
"tokenInMaxs": [
"pool_id": "1",
"share_out_amount": "402238349184328773",
"token_in_maxs": [
{
"denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
"amount": "8198"
Expand Down
6 changes: 3 additions & 3 deletions src/reference-data/defi/tx01.mainnet.osmosis.lp-add.tx.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@
{
"@type": "/osmosis.gamm.v1beta1.MsgJoinPool",
"sender": "osmo15cenya0tr7nm3tz2wn3h3zwkht2rxrq7g9ypmq",
"poolId": "1",
"shareOutAmount": "402238349184328773",
"tokenInMaxs": [
"pool_id": "1",
"share_out_amount": "402238349184328773",
"token_in_maxs": [
{
"denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
"amount": "8198"
Expand Down
6 changes: 3 additions & 3 deletions src/reference-data/defi/tx01.mainnet.osmosis.lp-remove.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"type": "osmosis/gamm/exit-pool",
"value": {
"sender": "osmo15cenya0tr7nm3tz2wn3h3zwkht2rxrq7g9ypmq",
"poolId": "1",
"shareInAmount": "78719426289889034",
"tokenOutMins": [
"pool_id": "1",
"share_in_amount": "78719426289889034",
"token_out_mins": [
{
"denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
"amount": "1532"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@
{
"@type": "/osmosis.gamm.v1beta1.MsgExitPool",
"sender": "osmo15cenya0tr7nm3tz2wn3h3zwkht2rxrq7g9ypmq",
"poolId": "1",
"shareInAmount": "78719426289889034",
"tokenOutMins": [
"pool_id": "1",
"share_in_amount": "78719426289889034",
"token_out_mins": [
{
"denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
"amount": "1532"
Expand Down
8 changes: 4 additions & 4 deletions src/reference-data/defi/tx01.mainnet.osmosis.swap.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
"value": {
"routes": [
{
"poolId": "1",
"tokenOutDenom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
"pool_id": "1",
"token_out_denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
}
],
"sender": "osmo15cenya0tr7nm3tz2wn3h3zwkht2rxrq7g9ypmq",
"tokenIn": {
"token_in": {
"amount": "6500",
"denom": "uosmo"
},
"tokenOutMinAmount": "8204"
"token_out_min_amount": "8204"
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions src/reference-data/defi/tx01.mainnet.osmosis.swap.tx.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@
"sender": "osmo15cenya0tr7nm3tz2wn3h3zwkht2rxrq7g9ypmq",
"routes": [
{
"poolId": "1",
"tokenOutDenom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
"pool_id": "1",
"token_out_denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
}
],
"tokenIn": {
"token_in": {
"denom": "uosmo",
"amount": "65000"
},
"tokenOutMinAmount": "8204"
"token_out_min_amount": "8204"
}
],
"memo": "",
Expand Down
Loading