Skip to content

Commit

Permalink
Merge pull request #301 from Junyong-Suh/fee_account_added
Browse files Browse the repository at this point in the history
feat: fee collector address added
  • Loading branch information
shkangr authored Jun 26, 2024
2 parents b88cb0a + 75e0f78 commit afa3479
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 39 deletions.
3 changes: 3 additions & 0 deletions bridge/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ MAX_GAS_PRICE="300000000000"
PAGERDUTY_ROUTING_KEY="..."

SLACK_CHANNEL_NAME="#nine-chronicles-bridge-bot-test"

# Libplanet account to collect fees
FEE_COLLECTOR_ADDRESS="0x123456"
5 changes: 4 additions & 1 deletion bridge/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,7 @@ FEE_RANGE_DIVIDER_AMOUNT=
PLANET_ODIN_ID=
PLANET_HEIMDALL_ID=

ODIN_TO_HEIMDALL_VALUT_ADDRESS=
ODIN_TO_HEIMDALL_VALUT_ADDRESS=

# Libplanet account to collect fees
FEE_COLLECTOR_ADDRESS="0x123456"
16 changes: 12 additions & 4 deletions bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ process.on("uncaughtException", console.error);
};

if (!web3.utils.isAddress(NCG_MINTER)) {
throw Error(
"NCG_MINTER variable seems invalid because it is not valid address format."
);
throw Error("NCG_MINTER is invalid - it is not valid address format.");
}

const kmsAddresses = await kmsProvider.getAccounts();
Expand Down Expand Up @@ -395,6 +393,15 @@ process.on("uncaughtException", console.error);
1
);

const FEE_COLLECTOR_ADDRESS: string = Configuration.get(
"FEE_COLLECTOR_ADDRESS"
);
if (!web3.utils.isAddress(FEE_COLLECTOR_ADDRESS)) {
throw Error(
"FEE_COLLECTOR_ADDRESS is invalid - it is not valid address format."
);
}

async function makeSafeWrappedNCGMinter(): Promise<SafeWrappedNCGMinter> {
if (
!USE_SAFE_WRAPPED_NCG_MINTER ||
Expand Down Expand Up @@ -531,7 +538,8 @@ process.on("uncaughtException", console.error);
addressBanPolicy,
integration,
FAILURE_SUBSCRIBERS,
whitelistAccounts
whitelistAccounts,
FEE_COLLECTOR_ADDRESS
);
const nineChroniclesMonitor = new NineChroniclesTransferredEventMonitor(
await monitorStateStore.load("nineChronicles"),
Expand Down
1 change: 0 additions & 1 deletion bridge/src/interfaces/wrapped-ncg-minter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TransactionReceipt } from "web3-core";
import Decimal from "decimal.js";

/**
Expand Down
9 changes: 8 additions & 1 deletion bridge/src/messages/wrapped-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class WrappedEvent extends WrappingEvent {
private readonly _refundTxId: string | null;
private readonly _isWhitelistEvent: boolean;
private readonly _description: string | undefined;
private readonly _feeTransferTxId: TxId;

constructor(
explorerUrl: string,
Expand All @@ -31,7 +32,8 @@ export class WrappedEvent extends WrappingEvent {
refundAmount: string | null,
refundTxId: TxId | null,
isWhitelistEvent: boolean,
description: string | undefined
description: string | undefined,
feeTransferTxId: TxId
) {
super(explorerUrl, ncscanUrl, useNcscan, etherscanUrl);

Expand All @@ -45,6 +47,7 @@ export class WrappedEvent extends WrappingEvent {
this._refundTxId = refundTxId;
this._isWhitelistEvent = isWhitelistEvent;
this._description = description;
this._feeTransferTxId = feeTransferTxId;
}

render(): ForceOmit<Partial<ChatPostMessageArguments>, "channel"> {
Expand Down Expand Up @@ -98,6 +101,10 @@ export class WrappedEvent extends WrappingEvent {
title: "fee",
value: this._fee.toString(),
},
{
title: "9c network transaction (fee transfer)",
value: this.toExplorerUrl(this._feeTransferTxId),
},
...refundFields,
],
fallback: `NCG ${this._sender} → wNCG ${this._recipient}`,
Expand Down
23 changes: 19 additions & 4 deletions bridge/src/observers/nine-chronicles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class NCGTransferredEventObserver

private readonly _integration: Integration;
private readonly _whitelistAccounts: WhitelistAccount[];
private readonly _feeCollectorAddress: string;

constructor(
ncgTransfer: INCGTransfer,
Expand All @@ -82,7 +83,8 @@ export class NCGTransferredEventObserver
addressBanPolicy: IAddressBanPolicy,
integration: Integration,
failureSubscribers: string,
whitelistAccounts: WhitelistAccount[]
whitelistAccounts: WhitelistAccount[],
feeCollectorAddress: string
) {
this._ncgTransfer = ncgTransfer;
this._wrappedNcgTransfer = wrappedNcgTransfer;
Expand All @@ -101,6 +103,7 @@ export class NCGTransferredEventObserver
this._integration = integration;
this._failureSubscribers = failureSubscribers;
this._whitelistAccounts = whitelistAccounts;
this._feeCollectorAddress = feeCollectorAddress;
}

async notify(data: {
Expand Down Expand Up @@ -401,8 +404,18 @@ export class NCGTransferredEventObserver
recipient!,
ethereumExchangeAmount
);

console.log("Receipt", transactionHash);
console.log("WNCG mint tx", transactionHash);

// Transfer fee to the fee collector address if any
let feeTransferTxId: string = "No Fee Incurred";
if (fee.greaterThan(0)) {
feeTransferTxId = await this._ncgTransfer.transfer(
this._feeCollectorAddress,
fee.toString(),
"I'm bridge and the fee is sent to fee collector."
);
console.log("Fee transfer tx", feeTransferTxId);
}

const isWhitelistEvent: boolean = accountType !== ACCOUNT_TYPE.GENERAL;
this._slackMessageSender.sendMessage(
Expand All @@ -420,14 +433,16 @@ export class NCGTransferredEventObserver
refundAmount,
refundTxId,
isWhitelistEvent,
whitelistDescription
whitelistDescription,
feeTransferTxId
)
);
this._opensearchClient.to_opensearch("info", {
content: "NCG -> wNCG request success",
libplanetTxId: txId,
ethereumTxId: transactionHash,
fee: fee.toNumber(),
feeTransferTxId: feeTransferTxId,
sender: sender,
recipient: recipient,
amount: exchangeAmount.toNumber(),
Expand Down
12 changes: 12 additions & 0 deletions bridge/test/messages/__snapshots__/wrapped-event.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Object {
"title": "fee",
"value": "1",
},
Object {
"title": "9c network transaction (fee transfer)",
"value": "https://explorer.libplanet.io/9c-internal/transaction?0x9360cd40682a91a71f0afbfac3dd381866cdb319dc01c13531dfe648f8a28bc8",
},
],
},
],
Expand Down Expand Up @@ -71,6 +75,10 @@ Object {
"title": "fee",
"value": "1",
},
Object {
"title": "9c network transaction (fee transfer)",
"value": "https://explorer.libplanet.io/9c-internal/transaction?0x9360cd40682a91a71f0afbfac3dd381866cdb319dc01c13531dfe648f8a28bc8",
},
Object {
"title": "refund amount",
"value": "9999900000",
Expand Down Expand Up @@ -118,6 +126,10 @@ Object {
"title": "fee",
"value": "1",
},
Object {
"title": "9c network transaction (fee transfer)",
"value": "https://explorer.libplanet.io/9c-internal/transaction?0x9360cd40682a91a71f0afbfac3dd381866cdb319dc01c13531dfe648f8a28bc8",
},
Object {
"title": "description",
"value": "test description",
Expand Down
27 changes: 18 additions & 9 deletions bridge/test/messages/wrapped-event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe("WrappedEvent", () => {
const NINE_CHRONICLES_TX_ID =
"3409cdbaa24ec6f7c8d2c0f636325a2b2e9611e5e6df5c593cfcd299860d8043";
const FEE = new Decimal(1);
const isWhitelistEvent = false;
const IS_WHITELIST_EVENT = false;
const FEE_TRANSFER_TX_ID =
"0x9360cd40682a91a71f0afbfac3dd381866cdb319dc01c13531dfe648f8a28bc8";
expect(
new WrappedEvent(
EXPLORER_URL,
Expand All @@ -31,8 +33,9 @@ describe("WrappedEvent", () => {
FEE,
null,
null,
isWhitelistEvent,
undefined
IS_WHITELIST_EVENT,
undefined,
FEE_TRANSFER_TX_ID
).render()
).toMatchSnapshot();
});
Expand All @@ -53,7 +56,9 @@ describe("WrappedEvent", () => {
const REFUND_AMOUNT = "9999900000";
const REFUND_TX_ID =
"a3cd151aa0cb24b3e692f433b857f08bd347dad0d2d6ca3666f26420b8b8d096";
const isWhitelistEvent = false;
const IS_WHITELIST_EVENT = false;
const FEE_TRANSFER_TX_ID =
"0x9360cd40682a91a71f0afbfac3dd381866cdb319dc01c13531dfe648f8a28bc8";
expect(
new WrappedEvent(
EXPLORER_URL,
Expand All @@ -68,8 +73,9 @@ describe("WrappedEvent", () => {
FEE,
REFUND_AMOUNT,
REFUND_TX_ID,
isWhitelistEvent,
undefined
IS_WHITELIST_EVENT,
undefined,
FEE_TRANSFER_TX_ID
).render()
).toMatchSnapshot();
});
Expand All @@ -87,7 +93,9 @@ describe("WrappedEvent", () => {
const NINE_CHRONICLES_TX_ID =
"3409cdbaa24ec6f7c8d2c0f636325a2b2e9611e5e6df5c593cfcd299860d8044";
const FEE = new Decimal(1);
const isWhitelistEvent = true;
const IS_WHITELIST_EVENT = true;
const FEE_TRANSFER_TX_ID =
"0x9360cd40682a91a71f0afbfac3dd381866cdb319dc01c13531dfe648f8a28bc8";
expect(
new WrappedEvent(
EXPLORER_URL,
Expand All @@ -102,8 +110,9 @@ describe("WrappedEvent", () => {
FEE,
null,
null,
isWhitelistEvent,
"test description"
IS_WHITELIST_EVENT,
"test description",
FEE_TRANSFER_TX_ID
).render()
).toMatchSnapshot();
});
Expand Down
22 changes: 16 additions & 6 deletions bridge/test/observers/__snapshots__/nine-chronicles.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ Array [
Array [
"info",
Object {
"amount": 48651.5,
"amount": 97151.5,
"content": "NCG -> wNCG request success",
"ethereumTxId": "TRANSACTION-HASH",
"fee": 1298.5,
"fee": 2798.5,
"feeTransferTxId": "TX-ID",
"libplanetTxId": "TX-A",
"recipient": "0x4029bC50b4747A037d38CF2197bCD335e22Ca301",
"sender": "0x2734048eC2892d111b4fbAB224400847544FC872",
Expand All @@ -18,7 +19,7 @@ Array [
"error",
Object {
"amount": 150,
"cause": "24 hr transfer maximum 50000 reached. User transferred 49950 NCGs in 24 hrs.",
"cause": "24 hr transfer maximum 100000 reached. User transferred 99950 NCGs in 24 hrs.",
"content": "NCG -> wNCG request failure",
"libplanetTxId": "TX-SHOULD-REFUND",
"recipient": "0x4029bC50b4747A037d38CF2197bCD335e22Ca301",
Expand Down Expand Up @@ -69,11 +70,15 @@ Array [
},
Object {
"title": "amount",
"value": "48651.5",
"value": "97151.5",
},
Object {
"title": "fee",
"value": "1298.5",
"value": "2798.5",
},
Object {
"title": "9c network transaction (fee transfer)",
"value": "https://explorer.libplanet.io/9c-internal/transaction?TX-ID",
},
],
},
Expand All @@ -91,7 +96,7 @@ Array [
"fields": Array [
Object {
"title": "Reason",
"value": "0x2734048eC2892d111b4fbAB224400847544FC872 tried to exchange 150 and already exchanged 49950 and users can exchange until 50000 in 24 hours so refund NCG as 100",
"value": "0x2734048eC2892d111b4fbAB224400847544FC872 tried to exchange 150 and already exchanged 99950 and users can exchange until 100000 in 24 hours so refund NCG as 100",
},
Object {
"title": "Address",
Expand Down Expand Up @@ -278,6 +283,7 @@ Array [
"content": "NCG -> wNCG request success",
"ethereumTxId": "TRANSACTION-HASH",
"fee": 10,
"feeTransferTxId": "TX-ID",
"libplanetTxId": "TX-ID",
"recipient": "0x4029bC50b4747A037d38CF2197bCD335e22Ca301",
"sender": "0x2734048eC2892d111b4fbAB224400847544FC872",
Expand Down Expand Up @@ -320,6 +326,10 @@ Array [
"title": "fee",
"value": "10",
},
Object {
"title": "9c network transaction (fee transfer)",
"value": "https://explorer.libplanet.io/9c-internal/transaction?TX-ID",
},
],
},
],
Expand Down
Loading

0 comments on commit afa3479

Please sign in to comment.