-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
always use latest gas rate for Bitcoin outbound; enforce gas rate cap
- Loading branch information
1 parent
847e2f1
commit c507687
Showing
9 changed files
with
174 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package math | ||
|
||
import "math" | ||
|
||
// IncreaseIntByPercent is a function that increases integer by a percentage. | ||
// Example1: IncreaseIntByPercent(10, 15, true) = 10 * 1.15 = 12 | ||
// Example2: IncreaseIntByPercent(10, 15, false) = 10 + 10 * 0.15 = 11 | ||
func IncreaseIntByPercent(value int64, percent uint64, round bool) int64 { | ||
switch { | ||
case percent == 0: | ||
return value | ||
case percent%100 == 0: | ||
// optimization: a simple multiplication | ||
increase := value * int64(percent/100) | ||
return value + increase | ||
default: | ||
increase := float64(value) * float64(percent) / 100 | ||
if round { | ||
return value + int64(math.Round(increase)) | ||
} | ||
return value + int64(increase) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package math | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_IncreaseIntByPercent(t *testing.T) { | ||
for i, tt := range []struct { | ||
value int64 | ||
percent uint64 | ||
round bool | ||
expected int64 | ||
}{ | ||
{value: 10, percent: 0, round: false, expected: 10}, | ||
{value: 10, percent: 15, round: false, expected: 11}, | ||
{value: 10, percent: 15, round: true, expected: 12}, | ||
{value: 10, percent: 14, round: false, expected: 11}, | ||
{value: 10, percent: 14, round: true, expected: 11}, | ||
{value: 10, percent: 200, round: false, expected: 30}, | ||
{value: 10, percent: 200, round: true, expected: 30}, | ||
} { | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
result := IncreaseIntByPercent(tt.value, tt.percent, tt.round) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.