Skip to content

Commit

Permalink
bugfix: target margin to source margin
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoch05 committed Sep 12, 2023
1 parent 44c0c4e commit 45c203a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions apollo/src/base/TransferServiceT2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface PartnerSymbol {
symbol: string;
address: string;
protocolFee: number;
decimals: number;
}

export interface PartnerT2 {
Expand Down
30 changes: 22 additions & 8 deletions apollo/src/lnbridgev20/lnbridgev20.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class Lnbridgev20Service implements OnModuleInit {
id: firstPendingRecord.id
});
this.fetchCache[index].confirmedNonce = this.formatSortedMessageNonce(Number(firstPendingRecord.nonce));
const toPartner = this.findPartnerByChainId(record.remoteChainId);
const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge);

// add correct tx
await this.aggregationService.createHistoryRecord({
Expand Down Expand Up @@ -235,7 +235,7 @@ export class Lnbridgev20Service implements OnModuleInit {
}

const fromToken = fromSymbol.symbol;
const toPartner = this.findPartnerByChainId(record.remoteChainId);
const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge);
const toSymbol = toPartner.symbols.find((item) => item.address.toLowerCase() === record.targetToken) ?? null;
if (!toSymbol) {
this.logger.warn(`cannot find to symbol, toChain: ${toPartner.chain} address ${record.targetToken}`);
Expand Down Expand Up @@ -366,16 +366,16 @@ export class Lnbridgev20Service implements OnModuleInit {
return `lnv20-${fromChainId}-${toChainId}-${provider}-${sourceToken}`;
}

private findPartnerByChainId(chainId: number) {
return this.transferService.transfers.find((item) => item.chainId === chainId) ?? null;
private findPartnerByChainId(chainId: number, bridge: string) {
return this.transferService.transfers.find((item) => item.chainId === chainId && item.bridge === bridge) ?? null;
}

private findPartnerByChainName(chainName: string, bridge: string) {
return this.transferService.transfers.find((item) => item.chain === chainName && item.bridge === bridge) ?? null;
}

async fetchMarginInfoFromTarget(transfer: PartnerT2, index: number) {
const { chain: toChain } = transfer;
const { chain: toChain, symbols } = transfer;
let latestNonce = this.fetchCache[index].latestRelayerInfoTargetNonce;
try {
if (latestNonce == -1) {
Expand Down Expand Up @@ -407,8 +407,22 @@ export class Lnbridgev20Service implements OnModuleInit {
id: id,
});
if (relayerInfo) {
// transfer target margin to source margin
const toSymbol = symbols.find((item) => item.address.toLowerCase() === record.targetToken) ?? null;
if (!toSymbol) {
this.logger.warn(`to symbol not find ${record.targetToken}`);
return;
}
const sourcePartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge);
const fromSymbol = sourcePartner.symbols.find((item) => item.address.toLowerCase() === record.sourceToken) ?? null;
if (!fromSymbol) {
this.logger.warn(`to symbol not find ${record.sourceToken}`);
return;
}

const sourceMargin = Number(record.margin) * Math.pow(10, fromSymbol.decimals - toSymbol.decimals);
const updateData = {
margin: record.margin,
margin: BigInt(sourceMargin).toString(),
slashCount: relayerInfo.slashCount,
withdrawNonce: relayerInfo.withdrawNonce,
targetNonce: latestNonce + 1,
Expand Down Expand Up @@ -474,7 +488,7 @@ export class Lnbridgev20Service implements OnModuleInit {
if (symbol == null) {
return;
}
const toPartner = this.findPartnerByChainId(record.remoteChainId);
const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge);
if (!relayerInfo) {
// if not exist create
await this.aggregationService.createLnv20RelayInfo({
Expand Down Expand Up @@ -558,7 +572,7 @@ export class Lnbridgev20Service implements OnModuleInit {
if (symbol == null) {
return;
}
const toPartner = this.findPartnerByChainId(record.remoteChainId);
const toPartner = this.findPartnerByChainId(record.remoteChainId, transfer.bridge);
if (!relayerInfo) {
// if not exist create
const margin = record.margin === null ? '0' : record.margin;
Expand Down
16 changes: 16 additions & 0 deletions apollo/src/lnbridgev20/transfer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0x1a70127284b774ff4a4dbfe0115114642f0eca65',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0x2303e4d55BF16a897Cb5Ab71c6225399509d9314',
protocolFee: 100000000000000000000,
decimals: 18,
},
],
},
Expand All @@ -61,11 +63,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0x1a70127284B774fF4A4dbfe0115114642f0eca65',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0x2303e4d55BF16a897Cb5Ab71c6225399509d9314',
protocolFee: 100000000000000000000,
decimals: 18,
},
],
},
Expand All @@ -79,11 +83,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0x39de82e1d9b8f62e11022fc3fc127a82f93fe47e',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0x6d828718c1097a4c573bc25c638cc05bf10dfeaf',
protocolFee: 100000000000000000000,
decimals: 18,
},
],
},
Expand All @@ -97,11 +103,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0x39de82e1d9b8f62e11022fc3fc127a82f93fe47e',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0x6d828718c1097a4c573bc25c638cc05bf10dfeaf',
protocolFee: 100000000000000000000,
decimals: 18,
},
],
},
Expand All @@ -115,11 +123,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0x0258eb547bfed540ed17843658c018569fe1e328',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0x5f8d4232367759bce5d9488d3ade77fcff6b9b6b',
protocolFee: 100000000000000000000,
decimals: 18,
},
],
},
Expand All @@ -133,11 +143,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0x0258eb547bfed540ed17843658c018569fe1e328',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0x5f8d4232367759bce5d9488d3ade77fcff6b9b6b',
protocolFee: 100000000000000000000,
decimals: 18,
},
],
},
Expand All @@ -151,11 +163,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0xb5e028f980df5533cb0e8f04530b76637383d993',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0xbc1a2f123dc9cd2ec8d3ce42ef16c28f3c9ba686',
protocolFee: 100000000000000000000,
decimals: 6,
},
],
},
Expand All @@ -169,11 +183,13 @@ export class TransferService extends BaseTransferServiceT2 {
symbol: 'USDC',
address: '0xb5e028f980df5533cb0e8f04530b76637383d993',
protocolFee: 100000000,
decimals: 6,
},
{
symbol: 'USDT',
address: '0xbc1a2f123dc9cd2ec8d3ce42ef16c28f3c9ba686',
protocolFee: 100000000000000000000,
decimals: 6,
},
],
},
Expand Down

0 comments on commit 45c203a

Please sign in to comment.