Skip to content

Instantly share code, notes, and snippets.

@focusj
Created March 1, 2024 03:09
Show Gist options
  • Save focusj/98d5453f9515f22a855350356c98f913 to your computer and use it in GitHub Desktop.
Save focusj/98d5453f9515f22a855350356c98f913 to your computer and use it in GitHub Desktop.
最大化吃掉各个pool之间的流动性
package main
import "fmt"
func main() {
targetPool := "A"
arbPools0 := []string{"B"}
hops0 := spreadSingleHop(targetPool, arbPools0)
fmt.Println(hops0)
arbPools1 := []string{"B", "C"}
hops1 := spreadSingleHop(targetPool, arbPools1)
fmt.Println(hops1)
arbPools2 := []string{"B", "C", "D"}
hops2 := spreadSingleHop(targetPool, arbPools2)
fmt.Println(hops2)
arbPools3 := []string{"B", "C", "D", "E"}
hops3 := spreadSingleHop(targetPool, arbPools3)
fmt.Println(hops3)
arbPools4 := []string{"B", "C", "D", "E", "F"}
hops4 := spreadSingleHop(targetPool, arbPools4)
fmt.Println(hops4)
arbPools5 := []string{"B", "C", "D", "E", "F", "G"}
hops5 := spreadSingleHop(targetPool, arbPools5)
fmt.Println(hops5)
//output:
//[[A B]]
//[[A B] [A C]]
//[[A B] [A C] [B D]]
//[[A B] [A C] [B D] [A E]]
//[[A B] [A C] [B D] [A E] [C F]]
//[[A B] [A C] [B D] [A E] [C F] [B G]]
}
func spreadSingleHop(targetPool string, arbPools []string) [][]string {
if len(arbPools) == 0 {
return nil
}
if len(arbPools) == 1 {
return [][]string{{targetPool, arbPools[0]}}
}
if len(arbPools) == 2 {
pool0, pool1 := arbPools[0], arbPools[1]
return [][]string{{targetPool, pool0}, {targetPool, pool1}}
}
var bundles [][]string
var index = 0
for len(arbPools) > 0 {
if len(bundles) == 0 { // 第一次排列,先讲targetPool和pool0组合放置到bundles中第0个位置
pool0 := arbPools[0]
bundles = append(bundles, []string{targetPool, pool0})
arbPools = arbPools[1:]
} else { // 以bundles[index]为target pools,去和arbPools中未参与过套利的pool进行组合
indexBundle := bundles[index]
poolA, poolB := indexBundle[0], indexBundle[1]
if len(arbPools) == 1 { // 如果arbPool只剩余1个元素,被组合之后直接返回
pool0 := arbPools[0]
bundles = append(bundles, []string{poolA, pool0})
arbPools = arbPools[1:]
index += 1
} else { // arbPool中剩余的元素可以与bundles[index]完全组合
pool0, pool1 := arbPools[0], arbPools[1]
bundles = append(bundles, []string{poolA, pool0}, []string{poolB, pool1})
arbPools = arbPools[2:]
index += 1
}
}
}
return bundles
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment