Skip to content

Commit

Permalink
Merge pull request #147 from helix-bridge/xiaoch05-support-xtoken
Browse files Browse the repository at this point in the history
support xtoken
  • Loading branch information
xiaoch05 authored Mar 1, 2024
2 parents 4bd34e4 + 5c3276d commit f9620c7
Show file tree
Hide file tree
Showing 25 changed files with 12,883 additions and 10 deletions.
5 changes: 5 additions & 0 deletions apollo/.env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ MANTLE_LNV3_ENDPOINT=https://thegraph-g1.darwinia.network/helix/subgraphs/name/l
SCROLL_LNV3_ENDPOINT=https://api.studio.thegraph.com/query/59403/lnv3-scroll/v1.0.0
DARWINIA_LNV3_ENDPOINT=https://thegraph-g1.darwinia.network/helix/subgraphs/name/lnv3/darwinia

XTOKEN_DARWINIA=https://thegraph.darwinia.network/helix/subgraphs/name/xtokentransfer/darwinia
XTOKEN_CRAB=https://thegraph.darwinia.network/helix/subgraphs/name/xtokentransfer/crab
XTOKEN_DISPATCH_DARWINIA=https://thegraph.darwinia.network/helix/subgraphs/name/xtokendispatch/darwinia
XTOKEN_DISPATCH_CRAB=https://thegraph.darwinia.network/helix/subgraphs/name/xtokendispatch/crab

CHAIN_TYPE=formal
5 changes: 5 additions & 0 deletions apollo/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ LN_ZKSYNC_DEFAULT_ENDPOINT = https://api.studio.thegraph.com/query/61328/ln-defa
ETHEREUM_LNV3_ENDPOINT=https://api.studio.thegraph.com/query/61328/lnv3-sepolia/v1.0.5
ARBITRUM_LNV3_ENDPOINT=https://api.studio.thegraph.com/query/61328/lnv3-arbitrum-sepolia/v1.0.5
ZKSYNC_LNV3_ENDPOINT=https://api.studio.thegraph.com/query/61328/lnv3-zksepolia/v1.0.5

XTOKEN_DARWINIA = https://thegraph-g2.darwinia.network/helix/subgraphs/name/xtokentransfer/pangolin
XTOKEN_ETHEREUM = https://api.studio.thegraph.com/query/61328/xtoken-sepolia/v1.1.3
XTOKEN_DISPATCH_DARWINIA = https://thegraph-g2.darwinia.network/helix/subgraphs/name/xtokendispatch/pangolin
XTOKEN_DISPATCH_ETHEREUM = https://api.studio.thegraph.com/query/61328/xtoken-dispatch-sepolia/v1.1.3
3 changes: 2 additions & 1 deletion apollo/src/aggregation/aggregation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DailyStatistics, HistoryRecord, Prisma, PrismaClient } from '@prisma/cl
import { HistoryRecords, LnBridgeRelayInfo, LnBridgeRelayInfos } from '../graphql';
import { GuardService } from '../guard/guard.service';
// export lnbridge service configure
import { last } from 'lodash';
import { TransferService as Lnv2Service } from '../lnbridgev20/transfer.service';
import { TransferService as Lnv3Service} from '../lnv3/transfer.service';
import { TasksService } from '../tasks/tasks.service';
Expand Down Expand Up @@ -123,7 +124,7 @@ export class AggregationService extends PrismaClient implements OnModuleInit {
record.fromChain,
record.toChain,
record.bridge,
BigInt(record.messageNonce).toString(),
BigInt(last(record.id.split('-'))).toString(),
record.endTime.toString(),
record.recvTokenAddress,
record.recipient,
Expand Down
2 changes: 2 additions & 0 deletions apollo/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Sub2ethv2Module } from './sub2ethv2/sub2ethv2.module';
import { S2sv21Module } from './s2sv21/s2sv21.module';
import { LnbridgeModule } from './lnbridge/lnbridge.module';
import { Lnbridgev20Module } from './lnbridgev20/lnbridgev20.module';
import { xTokenModule } from './xtoken/xtoken.module';
import { Lnv3Module } from './lnv3/lnv3.module';

const chainEnvFilePath = `.env.${process.env.NODE_ENV || 'prod'}`;
Expand Down Expand Up @@ -47,6 +48,7 @@ export class BigIntScalar extends BigInt {}
LnbridgeModule,
S2sv21Module,
Lnbridgev20Module,
xTokenModule,
Lnv3Module,
],
controllers: [AppController],
Expand Down
30 changes: 21 additions & 9 deletions apollo/src/guard/guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class GuardInfo {
toChain: string;
bridge: string;
chainId: number;
depositor: string | null;
contract: string;
}

Expand All @@ -15,18 +16,20 @@ export class GuardService {
private readonly guardConfig: GuardInfo[] = [
{
fromChain: 'pangolin-dvm',
toChain: 'goerli',
bridge: 'helix-sub2ethv2(lock)',
chainId: 5,
contract: '0x8C986EC362A38cA4A6a3fd4188C5318c689A187d',
toChain: 'sepolia',
bridge: 'xtoken-pangolin-dvm',
chainId: 11155111,
depositor: '0x371019523b25Ff4F26d977724f976566b08bf741',
contract: '0x3f200d3b6DA62bcA2F8a93F663b172A7f1AaE9ba',
},
{
fromChain: 'darwinia-dvm',
toChain: 'ethereum',
bridge: 'helix-sub2ethv2(lock)',
chainId: 1,
depositor: null,
contract: '0x61B6B8c7C00aA7F060a2BEDeE6b11927CC9c3eF1',
},
}
];

recoverPubkey(
Expand All @@ -47,6 +50,7 @@ export class GuardService {
return null;
}
const dataHash = this.generateDataHash(
info.depositor,
transferId,
timestamp,
token,
Expand All @@ -59,6 +63,7 @@ export class GuardService {
}

private generateDataHash(
depositor: string,
transferId: string,
timestamp: string,
token: string,
Expand All @@ -67,12 +72,19 @@ export class GuardService {
chainId: number,
contractAddress: string
): string {
const claimSign = this.web3.eth.abi.encodeFunctionSignature(
const claimSign = depositor === null ?
this.web3.eth.abi.encodeFunctionSignature(
'claim(uint256,uint256,address,address,uint256,bytes[])'
) : this.web3.eth.abi.encodeFunctionSignature(
'claim(address,uint256,uint256,address,address,uint256,bytes[])'
);
const param = this.web3.eth.abi.encodeParameters(
['uint256', 'uint256', 'address', 'address', 'uint256'],
[transferId, timestamp, token, recipient, amount]
const param = depositor === null ?
this.web3.eth.abi.encodeParameters(
['uint256', 'uint256', 'address', 'address', 'uint256'],
[transferId, timestamp, token, recipient, amount]
) : this.web3.eth.abi.encodeParameters(
['address', 'uint256', 'uint256', 'address', 'address', 'uint256'],
[depositor, transferId, timestamp, token, recipient, amount]
);
const message = this.web3.eth.abi.encodeParameters(['bytes4', 'bytes'], [claimSign, param]);
const structHash = this.web3.utils.keccak256(message);
Expand Down
132 changes: 132 additions & 0 deletions apollo/src/xtoken/transfer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { BaseTransferServiceT2, PartnerT2 } from '../base/TransferServiceT2';
import { AddressTokenMap } from '../base/AddressToken';

@Injectable()
export class TransferService extends BaseTransferServiceT2 {
private readonly darwainiaUrl = this.configService.get<string>('XTOKEN_DARWINIA');
private readonly crabUrl = this.configService.get<string>('XTOKEN_CRAB');
private readonly ethereumUrl = this.configService.get<string>('XTOKEN_ETHEREUM');
private readonly darwiniaDispatchSubgraph = this.configService.get<string>('XTOKEN_DISPATCH_DARWINIA');
private readonly crabDispatchSubgraph = this.configService.get<string>('XTOKEN_DISPATCH_CRAB');
private readonly ethereumDispatchSubgraph = this.configService.get<string>('XTOKEN_DISPATCH_ETHEREUM');

formalChainTransfers: PartnerT2[] = [
{
chainId: 46,
chain: 'darwinia-dvm',
url: this.darwainiaUrl,
bridge: 'xtokenbridge',
symbols: [
{
key: 'RING',
symbol: 'RING',
address: '0x0000000000000000000000000000000000000000',
protocolFee: 0,
decimals: 18,
},
{
key: 'CRAB',
symbol: 'xWCRAB',
address: '0x656567Eb75b765FC320783cc6EDd86bD854b2305',
protocolFee: 0,
decimals: 18,
}
],
channels: [
{
chain: 'crab-dvm',
channel: 'msgport'
}
]
},
{
chainId: 44,
chain: 'crab-dvm',
url: this.crabUrl,
bridge: 'xtokenbridge',
symbols: [
{
key: 'RING',
symbol: 'xWRING',
address: '0x273131F7CB50ac002BDd08cA721988731F7e1092',
protocolFee: 0,
decimals: 18,
},
{
key: 'CRAB',
symbol: 'CRAB',
address: '0x0000000000000000000000000000000000000000',
protocolFee: 0,
decimals: 18,
}
],
channels: [
{
chain: 'darwinia-dvm',
channel: 'msgport'
}
]

}
];

testChainTransfers: PartnerT2[] = [
{
chainId: 43,
chain: 'pangolin-dvm',
url: this.darwainiaUrl,
bridge: 'xtokenbridge',
symbols: [
{
key: 'PRING',
symbol: 'PRING',
address: '0x0000000000000000000000000000000000000000',
protocolFee: 0,
decimals: 18,
},
],
channels: [
{
chain: 'sepolia',
channel: 'msgport'
}
]
},
{
chainId: 11155111,
chain: 'sepolia',
url: this.ethereumUrl,
bridge: 'xtokenbridge',
symbols: [
{
key: 'PRING',
symbol: 'xPRING',
address: '0xBC43cb6175FcC8E577a0846256eA699b87eFcEE5',
protocolFee: 0,
decimals: 18,
},
],
channels: [
{
chain: 'pangolin-dvm',
channel: 'msgport'
}
]
},
];

dispatchEndPoints = {
'pangolin-dvm': this.darwiniaDispatchSubgraph,
sepolia: this.ethereumDispatchSubgraph,
'darwinia-dvm': this.darwiniaDispatchSubgraph,
ethereum: this.ethereumDispatchSubgraph,
'crab-dvm': this.crabDispatchSubgraph,
};
addressToTokenInfo: { [key: string]: AddressTokenMap } = {};

constructor(public configService: ConfigService) {
super(configService);
}
}
11 changes: 11 additions & 0 deletions apollo/src/xtoken/xtoken.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { xTokenService } from './xtoken.service';
import { AggregationModule } from '../aggregation/aggregation.module';
import { TasksModule } from '../tasks/tasks.module';
import { TransferService } from './transfer.service';

@Module({
imports: [AggregationModule, TasksModule],
providers: [xTokenService, TransferService],
})
export class xTokenModule {}
Loading

0 comments on commit f9620c7

Please sign in to comment.