Skip to content

Commit 98d6277

Browse files
authored
Support Sui Gas Fee Calc (#1267)
* sui fee calc * fix linter * fix lint
1 parent 17cb9bc commit 98d6277

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

internal/libs/mathslib/calc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ func CalculateUsdPerUnitGas(
8888
tmp := new(big.Int).Mul(sourceGasPrice, usdPerFeeCoin)
8989
return new(big.Int).Div(tmp, big.NewInt(1e18)), nil
9090

91+
case chainsel.FamilySui:
92+
// In Sui, sourceGasPrice is denoted in mist/gas unit or 1e-9 SUI/gas.
93+
// SUI has 9 decimals, usdPerFeeCoin represents 1e18 USD * 1e9 = 1e27 USD per full SUI.
94+
95+
// usdPerFeeCoin is 1e18 USD per 1e18 smallest token unit by convention.
96+
// When the token has 9 decimals (SUI), 1e18 smallest token units = 1e9 whole tokens.
97+
// Therefore, usdPerFeeCoin represents 1e18 USD * 1e9 = 1e27 USD per SUI.
98+
99+
// = (mist / gas) * (1e18 USD * 1e9 / mist) / 1e18
100+
// = (sourceGasPrice * usdPerFeeCoin) / 1e18
101+
102+
tmp := new(big.Int).Mul(sourceGasPrice, usdPerFeeCoin)
103+
return new(big.Int).Div(tmp, big.NewInt(1e18)), nil
104+
91105
case chainsel.FamilyTon:
92106
// In TON, sourceGasPrice is denoted in nanoton/gas or 1e-9 TON/gas.
93107
// TON has 9 decimals, usdPerFeeCoin represents 1e18 USD * 1e9 / nanoton.

internal/libs/mathslib/calc_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
SolChainSelector = ccipocr3.ChainSelector(sel.SOLANA_DEVNET.Selector)
1818
EvmChainSelector = ccipocr3.ChainSelector(sel.ETHEREUM_TESTNET_SEPOLIA.Selector)
1919
AptChainSelector = ccipocr3.ChainSelector(sel.APTOS_TESTNET.Selector)
20+
SuiChainSelector = ccipocr3.ChainSelector(sel.SUI_TESTNET.Selector)
2021
TonChainSelector = ccipocr3.ChainSelector(sel.TON_TESTNET.Selector)
2122
)
2223

@@ -198,6 +199,28 @@ func TestCalculateUsdPerUnitGas(t *testing.T) {
198199
chainSelector: AptChainSelector,
199200
exp: big.NewInt(0 * 1 * 1e10), // gasprice * USD per APT * (USD / APT)
200201
},
202+
{
203+
name: "sui high fee case",
204+
sourceGasPrice: big.NewInt(100000),
205+
// usdPerFeeCoin = $4 * 1e9 * 1e18 = 4e27
206+
usdPerFeeCoin: new(big.Int).Mul(big.NewInt(4), new(big.Int).Mul(big.NewInt(1e9), big.NewInt(1e18))),
207+
chainSelector: SuiChainSelector,
208+
exp: big.NewInt(100000 * 4e9),
209+
},
210+
{
211+
name: "sui low fee case",
212+
sourceGasPrice: big.NewInt(100),
213+
usdPerFeeCoin: new(big.Int).Mul(big.NewInt(4), new(big.Int).Mul(big.NewInt(1e9), big.NewInt(1e18))),
214+
chainSelector: SuiChainSelector,
215+
exp: big.NewInt(100 * 4e9),
216+
},
217+
{
218+
name: "sui 0 fee case",
219+
sourceGasPrice: big.NewInt(0),
220+
usdPerFeeCoin: new(big.Int).Mul(big.NewInt(4), new(big.Int).Mul(big.NewInt(1e9), big.NewInt(1e18))),
221+
chainSelector: SuiChainSelector,
222+
exp: big.NewInt(0),
223+
},
201224
{
202225
name: "ton base case",
203226
sourceGasPrice: big.NewInt(400),

0 commit comments

Comments
 (0)