Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
feat(stellar): add support for StellarManageBuyOfferOp and StellarPat…
Browse files Browse the repository at this point in the history
…hPaymentStrictSendOp
  • Loading branch information
overcat committed Nov 5, 2021
1 parent c933d23 commit 1f8d1c4
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 78 deletions.
3 changes: 2 additions & 1 deletion scripts/protobuf-patches.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const RULE_PATCH = {
'NEMMosaicDefinition.networks': 'optional', // never used according to implementation/tests
'NEMAggregateModification.modifications': 'optional', // its valid to be undefined according to implementation/tests
'StellarAssetType.code': 'required',
'StellarPathPaymentOp.paths': 'optional', // its valid to be undefined according to implementation/tests
'StellarPathPaymentStrictReceiveOp.paths': 'optional', // its valid to be undefined according to implementation/tests
'StellarPathPaymentStrictSendOp.paths': 'optional', // its valid to be undefined according to implementation/tests
};

// custom types IN to trezor
Expand Down
38 changes: 32 additions & 6 deletions src/js/core/methods/helpers/stellarSignTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ const transformOperation = (op: StellarOperation): ?StellarOperationMessage => {
amount: op.amount,
};

case 'pathPayment':
case 'pathPaymentStrictReceive':
validateParams(op, [{ name: 'destAmount', type: 'amount', obligatory: true }]);
return {
type: 'StellarPathPaymentOp',
type: 'StellarPathPaymentStrictReceiveOp',
source_account: op.source,
send_asset: op.sendAsset,
send_max: op.sendMax,
Expand All @@ -100,10 +100,23 @@ const transformOperation = (op: StellarOperation): ?StellarOperationMessage => {
paths: op.path,
};

case 'createPassiveOffer':
case 'pathPaymentStrictSend':
validateParams(op, [{ name: 'destMin', type: 'amount', obligatory: true }]);
return {
type: 'StellarPathPaymentStrictSendOp',
source_account: op.source,
send_asset: op.sendAsset,
send_amount: op.sendAmount,
destination_account: op.destination,
destination_asset: op.destAsset,
destination_min: op.destMin,
paths: op.path,
};

case 'createPassiveSellOffer':
validateParams(op, [{ name: 'amount', type: 'amount', obligatory: true }]);
return {
type: 'StellarCreatePassiveOfferOp',
type: 'StellarCreatePassiveSellOfferOp',
source_account: op.source,
buying_asset: op.buying,
selling_asset: op.selling,
Expand All @@ -112,10 +125,23 @@ const transformOperation = (op: StellarOperation): ?StellarOperationMessage => {
price_d: op.price.d,
};

case 'manageOffer':
case 'manageSellOffer':
validateParams(op, [{ name: 'amount', type: 'amount', obligatory: true }]);
return {
type: 'StellarManageSellOfferOp',
source_account: op.source,
buying_asset: op.buying,
selling_asset: op.selling,
amount: op.amount,
offer_id: op.offerId || 0,
price_n: op.price.n,
price_d: op.price.d,
};

case 'manageBuyOffer':
validateParams(op, [{ name: 'amount', type: 'amount', obligatory: true }]);
return {
type: 'StellarManageOfferOp',
type: 'StellarManageBuyOfferOp',
source_account: op.source,
buying_asset: op.buying,
selling_asset: op.selling,
Expand Down
25 changes: 1 addition & 24 deletions src/js/plugins/stellar/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,6 @@ const transformAsset = asset => {
*/
const transformAmount = amount => new BigNumber(amount).times(10000000).toString();

/**
* Transforms StellarSdk.Operation.type to TrezorConnect.StellarTransaction.Operation.type
* @param {string} type
* @returns {string}
*/
const transformType = type => {
switch (type) {
case 'pathPaymentStrictReceive':
// case 'pathPaymentStrictSend':
return 'pathPayment';

case 'createPassiveSellOffer':
return 'createPassiveOffer';

case 'manageSellOffer':
// case 'manageBuyOffer':
return 'manageOffer';
default:
return type;
}
};

/**
* Transforms StellarSdk.Memo to TrezorConnect.StellarTransaction.Memo
* @param {string} type
Expand Down Expand Up @@ -172,8 +150,7 @@ const transformTransaction = (path, transaction) => {
operation.value = operation.value.toString('hex');
}

// transform type
operation.type = transformType(o.type);
operation.type = o.type;

return operation;
});
Expand Down
71 changes: 66 additions & 5 deletions src/js/types/__tests__/stellar.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const stellarSignTransaction = async () => {
value: undefined,
},
{
type: 'pathPayment',
type: 'pathPaymentStrictReceive',
sendAsset: {
type: 1,
code: 'X',
Expand All @@ -180,7 +180,7 @@ export const stellarSignTransaction = async () => {
destAmount: '500111000',
},
{
type: 'pathPayment',
type: 'pathPaymentStrictReceive',
sendAsset: {
type: 2,
code: 'ABCDEFGHIJKL',
Expand Down Expand Up @@ -208,7 +208,51 @@ export const stellarSignTransaction = async () => {
],
},
{
type: 'createPassiveOffer',
type: 'pathPaymentStrictSend',
sendAsset: {
type: 1,
code: 'X',
issuer: 'GAUYJFQCYIHFQNS7CI6BFWD2DSSFKDIQZUQ3BLQODDKE4PSW7VVBKENC',
},
sendAmount: '500111000',
destination: 'GBOVKZBEM2YYLOCDCUXJ4IMRKHN4LCJAE7WEAEA2KF562XFAGDBOB64V',
destAsset: {
type: 1,
code: 'X',
issuer: 'GAUYJFQCYIHFQNS7CI6BFWD2DSSFKDIQZUQ3BLQODDKE4PSW7VVBKENC',
},
destMin: '500111000',
},
{
type: 'pathPaymentStrictSend',
sendAsset: {
type: 2,
code: 'ABCDEFGHIJKL',
issuer: 'GAUYJFQCYIHFQNS7CI6BFWD2DSSFKDIQZUQ3BLQODDKE4PSW7VVBKENC',
},
sendAmount: '500111000',
destination: 'GBOVKZBEM2YYLOCDCUXJ4IMRKHN4LCJAE7WEAEA2KF562XFAGDBOB64V',
destAsset: {
type: 2,
code: 'ABCDEFGHIJKL',
issuer: 'GAUYJFQCYIHFQNS7CI6BFWD2DSSFKDIQZUQ3BLQODDKE4PSW7VVBKENC',
},
destMin: '500111000',
path: [
{
type: 1,
code: 'X',
issuer: 'GAUYJFQCYIHFQNS7CI6BFWD2DSSFKDIQZUQ3BLQODDKE4PSW7VVBKENC',
},
{
type: 2,
code: 'ABCDEFGHIJKL',
issuer: 'GAUYJFQCYIHFQNS7CI6BFWD2DSSFKDIQZUQ3BLQODDKE4PSW7VVBKENC',
},
],
},
{
type: 'createPassiveSellOffer',
selling: {
type: 0,
code: 'XLM',
Expand All @@ -224,7 +268,7 @@ export const stellarSignTransaction = async () => {
},
},
{
type: 'createPassiveOffer',
type: 'createPassiveSellOffer',
selling: {
type: 1,
code: 'X',
Expand All @@ -242,7 +286,24 @@ export const stellarSignTransaction = async () => {
},
},
{
type: 'manageOffer',
type: 'manageSellOffer',
selling: {
type: 0,
code: 'XLM',
},
buying: {
type: 0,
code: 'XLM',
},
amount: '500111000',
price: {
n: 500111,
d: 10000,
},
offerId: '101',
},
{
type: 'manageBuyOffer',
selling: {
type: 0,
code: 'XLM',
Expand Down
67 changes: 49 additions & 18 deletions src/js/types/networks/stellar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import type {
StellarMemoType,
StellarPaymentOp,
StellarCreateAccountOp,
StellarPathPaymentOp,
StellarManageOfferOp,
StellarCreatePassiveOfferOp,
StellarPathPaymentStrictSendOp,
StellarPathPaymentStrictReceiveOp,
StellarCreatePassiveSellOfferOp,
StellarManageBuyOfferOp,
StellarManageSellOfferOp,
StellarSetOptionsOp,
StellarChangeTrustOp,
StellarAllowTrustOp,
Expand Down Expand Up @@ -41,8 +43,8 @@ export type StellarPaymentOperation = {
amount: string, // Proto: ok
};

export type StellarPathPaymentOperation = {
type: 'pathPayment', // Proto: "StellarPathPaymentOp"
export type StellarPathPaymentStrictReceiveOperation = {
type: 'pathPaymentStrictReceive', // Proto: "StellarPathPaymentStrictReceiveOp"
source?: string, // Proto: "source_account"
sendAsset: StellarAsset, // Proto: "send_asset"
sendMax: string, // Proto: "send_max"
Expand All @@ -52,17 +54,38 @@ export type StellarPathPaymentOperation = {
path?: StellarAsset[], // Proto: "paths"
};

export type StellarPassiveOfferOperation = {
type: 'createPassiveOffer', // Proto: "StellarCreatePassiveOfferOp"
export type StellarPathPaymentStrictSendOperation = {
type: 'pathPaymentStrictSend', // Proto: "StellarPathPaymentStrictSendOp"
source?: string, // Proto: "source_account"
sendAsset: StellarAsset, // Proto: "send_asset"
sendAmount: string, // Proto: "send_amount"
destination: string, // Proto: "destination_account"
destAsset: StellarAsset, // Proto: "destination_asset"
destMin: string, // Proto "destination_min"
path?: StellarAsset[], // Proto: "paths"
};

export type StellarPassiveSellOfferOperation = {
type: 'createPassiveSellOffer', // Proto: "StellarCreatePassiveSellOfferOp"
source?: string, // Proto: "source_account"
buying: StellarAsset, // Proto: "buying_asset"
selling: StellarAsset, // Proto: "selling_asset"
amount: string, // Proto: ok
price: { n: number, d: number }, // Proto: "price_n" and "price_d"
};

export type StellarManageOfferOperation = {
type: 'manageOffer', // Proto: "StellarManageOfferOp"
export type StellarManageSellOfferOperation = {
type: 'manageSellOffer', // Proto: "StellarManageSellOfferOp"
source?: string, // Proto: "source_account"
buying: StellarAsset, // Proto: "buying_asset"
selling: StellarAsset, // Proto: "selling_asset"
amount: string, // Proto: ok
offerId?: string, // Proto: "offer_id" // not found in stellar-sdk
price: { n: number, d: number }, // Proto: "price_n" and "price_d"
};

export type StellarManageBuyOfferOperation = {
type: 'manageBuyOffer', // Proto: "StellarManageBuyOfferOp"
source?: string, // Proto: "source_account"
buying: StellarAsset, // Proto: "buying_asset"
selling: StellarAsset, // Proto: "selling_asset"
Expand Down Expand Up @@ -134,9 +157,11 @@ export type StellarInflationOperation = {
export type StellarOperation =
| StellarCreateAccountOperation
| StellarPaymentOperation
| StellarPathPaymentOperation
| StellarPassiveOfferOperation
| StellarManageOfferOperation
| StellarPathPaymentStrictReceiveOperation
| StellarPathPaymentStrictSendOperation
| StellarPassiveSellOfferOperation
| StellarManageSellOfferOperation
| StellarManageBuyOfferOperation
| StellarSetOptionsOperation
| StellarChangeTrustOperation
| StellarAllowTrustOperation
Expand Down Expand Up @@ -176,14 +201,20 @@ export type StellarOperationMessage =
type: 'StellarPaymentOp',
} & StellarPaymentOp)
| ({
type: 'StellarPathPaymentOp',
} & StellarPathPaymentOp)
type: 'StellarPathPaymentStrictReceiveOp',
} & StellarPathPaymentStrictReceiveOp)
| ({
type: 'StellarPathPaymentStrictSendOp',
} & StellarPathPaymentStrictSendOp)
| ({
type: 'StellarManageSellOfferOp',
} & StellarManageSellOfferOp)
| ({
type: 'StellarManageOfferOp',
} & StellarManageOfferOp)
type: 'StellarManageBuyOfferOp',
} & StellarManageBuyOfferOp)
| ({
type: 'StellarCreatePassiveOfferOp',
} & StellarCreatePassiveOfferOp)
type: 'StellarCreatePassiveSellOfferOp',
} & StellarCreatePassiveSellOfferOp)
| ({
type: 'StellarSetOptionsOp',
} & StellarSetOptionsOp)
Expand Down
4 changes: 2 additions & 2 deletions src/js/types/trezor/protobuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ export type StellarPathPaymentStrictReceiveOp = {
destination_account: string,
destination_asset: StellarAsset,
destination_amount: string | number,
paths: StellarAsset[],
paths?: StellarAsset[],
};

// StellarPathPaymentStrictSendOp
Expand All @@ -1936,7 +1936,7 @@ export type StellarPathPaymentStrictSendOp = {
destination_account: string,
destination_asset: StellarAsset,
destination_min: string | number,
paths: StellarAsset[],
paths?: StellarAsset[],
};

// StellarManageSellOfferOp
Expand Down
Loading

0 comments on commit 1f8d1c4

Please sign in to comment.