Best Gauge code snippet using version.Swap
builderc20swaptx.go
Source:builderc20swaptx.go
...12 "github.com/anyswap/CrossChain-Router/v3/tokens/eth/abicoder"13)14// router contract's func hashs15var (16 defSwapDeadlineOffset = int64(36000)17 ForceAnySwapInAutoTokenVersion = uint64(10001)18 ForceAnySwapInTokenVersion = uint64(10002)19 ForceAnySwapInUnderlyingTokenVersion = uint64(10003)20 ForceAnySwapInNativeTokenVersion = uint64(10004)21 ForceAnySwapInAndCallTokenVersion = uint64(10005)22 ForceAnySwapInUnerlyingAndCallTokenVersion = uint64(10006)23 MintBurnWrapperTokenVersion = uint64(20000)24 // anySwapIn(bytes32 txs, address token, address to, uint amount, uint fromChainID)25 AnySwapInFuncHash = common.FromHex("0x825bb13c")26 // anySwapInUnderlying(bytes32 txs, address token, address to, uint amount, uint fromChainID)27 AnySwapInUnderlyingFuncHash = common.FromHex("0x3f88de89")28 // anySwapInNative(bytes32 txs, address token, address to, uint amount, uint fromChainID)29 AnySwapInNativeFuncHash = common.FromHex("0x21974f28")30 // anySwapInAuto(bytes32 txs, address token, address to, uint amount, uint fromChainID)31 AnySwapInAutoFuncHash = common.FromHex("0x0175b1c4")32 // anySwapInExactTokensForTokens(bytes32 txs, uint amountIn, uint amountOutMin, address[] path, address to, uint deadline, uint fromChainID)33 AnySwapInExactTokensForTokensFuncHash = common.FromHex("0x2fc1e728")34 // anySwapInExactTokensForNative(bytes32 txs, uint amountIn, uint amountOutMin, address[] path, address to, uint deadline, uint fromChainID)35 AnySwapInExactTokensForNativeFuncHash = common.FromHex("0x52a397d5")36 // anySwapInAndExec(bytes32 txs, address token, address to, uint amount, uint fromChainID, address anycallProxy, bytes calldata data)37 AnySwapInAndExecFuncHash = common.FromHex("0x86377115")38 // anySwapInUnderlyingAndExec(bytes32 txs, address token, address to, uint amount, uint fromChainID, address anycallProxy, bytes calldata data)39 AnySwapInUnderlyingAndExecFuncHash = common.FromHex("0x3a4ff8dc")40)41// GetSwapInFuncHash get swapin func hash42func GetSwapInFuncHash(tokenCfg *tokens.TokenConfig, forUnderlying bool) []byte {43 if forUnderlying {44 return AnySwapInUnderlyingFuncHash45 }46 switch tokenCfg.ContractVersion {47 case ForceAnySwapInAutoTokenVersion:48 return AnySwapInAutoFuncHash49 case ForceAnySwapInTokenVersion, ForceAnySwapInAndCallTokenVersion, MintBurnWrapperTokenVersion:50 return AnySwapInFuncHash51 case ForceAnySwapInUnderlyingTokenVersion, ForceAnySwapInUnerlyingAndCallTokenVersion:52 return AnySwapInUnderlyingFuncHash53 case ForceAnySwapInNativeTokenVersion:54 return AnySwapInNativeFuncHash55 case 0:56 if common.HexToAddress(tokenCfg.GetUnderlying()) == (common.Address{}) {57 // without underlying58 return AnySwapInFuncHash59 }60 default:61 if common.HexToAddress(tokenCfg.GetUnderlying()) == (common.Address{}) &&62 !params.IsForceAnySwapInAuto() {63 // without underlying, and not force swapinAuto64 return AnySwapInFuncHash65 }66 }67 return AnySwapInAutoFuncHash68}69// GetSwapInAndExecFuncHash get swapin and call func hash70func GetSwapInAndExecFuncHash(tokenCfg *tokens.TokenConfig) []byte {71 switch tokenCfg.ContractVersion {72 case ForceAnySwapInAndCallTokenVersion, MintBurnWrapperTokenVersion:73 return AnySwapInAndExecFuncHash74 case ForceAnySwapInUnerlyingAndCallTokenVersion:75 return AnySwapInUnderlyingAndExecFuncHash76 default:77 if common.HexToAddress(tokenCfg.GetUnderlying()) == (common.Address{}) {78 return AnySwapInAndExecFuncHash79 }80 }81 return AnySwapInUnderlyingAndExecFuncHash82}83func (b *Bridge) buildERC20SwapTxInput(args *tokens.BuildTxArgs) (err error) {84 if args.ERC20SwapInfo == nil || args.ERC20SwapInfo.TokenID == "" {85 return errors.New("build router swaptx without tokenID")86 }87 erc20SwapInfo := args.ERC20SwapInfo88 multichainToken := router.GetCachedMultichainToken(erc20SwapInfo.TokenID, args.ToChainID.String())89 if multichainToken == "" {90 log.Warn("get multichain token failed", "tokenID", erc20SwapInfo.TokenID, "chainID", args.ToChainID)91 return tokens.ErrMissTokenConfig92 }93 if erc20SwapInfo.CallProxy != "" {94 return b.buildSwapAndExecTxInput(args, multichainToken)95 }96 if len(erc20SwapInfo.Path) > 0 {97 return b.buildERC20SwapTradeTxInput(args, multichainToken)98 }99 return b.buildERC20SwapoutTxInput(args, multichainToken)100}101func (b *Bridge) buildSwapAndExecTxInput(args *tokens.BuildTxArgs, multichainToken string) (err error) {102 receiver, amount, err := b.getReceiverAndAmount(args, multichainToken)103 if err != nil {104 return err105 }106 toTokenCfg := b.GetTokenConfig(multichainToken)107 if toTokenCfg == nil {108 return tokens.ErrMissTokenConfig109 }110 erc20SwapInfo := args.ERC20SwapInfo111 funcHash := GetSwapInAndExecFuncHash(toTokenCfg)112 input := abicoder.PackDataWithFuncHash(funcHash,113 common.HexToHash(args.SwapID),114 common.HexToAddress(multichainToken),115 receiver,116 amount,117 args.FromChainID,118 common.HexToAddress(erc20SwapInfo.CallProxy),119 erc20SwapInfo.CallData,120 )121 args.Input = (*hexutil.Bytes)(&input) // input122 args.To = b.GetRouterContract(multichainToken) // to123 args.SwapValue = amount // swapValue124 return nil125}126func (b *Bridge) buildERC20SwapoutTxInput(args *tokens.BuildTxArgs, multichainToken string) (err error) {127 receiver, amount, err := b.getReceiverAndAmount(args, multichainToken)128 if err != nil {129 return err130 }131 toTokenCfg := b.GetTokenConfig(multichainToken)132 if toTokenCfg == nil {133 return tokens.ErrMissTokenConfig134 }135 funcHash := GetSwapInFuncHash(toTokenCfg, args.ERC20SwapInfo.ForUnderlying)136 input := abicoder.PackDataWithFuncHash(funcHash,137 common.HexToHash(args.SwapID),138 common.HexToAddress(multichainToken),139 receiver,140 amount,141 args.FromChainID,142 )143 args.Input = (*hexutil.Bytes)(&input) // input144 args.To = b.GetRouterContract(multichainToken) // to145 args.SwapValue = amount // swapValue146 return nil147}148func (b *Bridge) buildERC20SwapTradeTxInput(args *tokens.BuildTxArgs, multichainToken string) (err error) {149 receiver, amount, err := b.getReceiverAndAmount(args, multichainToken)150 if err != nil {151 return err152 }153 erc20SwapInfo := args.ERC20SwapInfo154 var funcHash []byte155 if erc20SwapInfo.ForNative {156 funcHash = AnySwapInExactTokensForNativeFuncHash157 } else {158 funcHash = AnySwapInExactTokensForTokensFuncHash159 }160 input := abicoder.PackDataWithFuncHash(funcHash,161 common.HexToHash(args.SwapID),162 amount,163 erc20SwapInfo.AmountOutMin,164 toAddresses(erc20SwapInfo.Path),165 receiver,166 calcSwapDeadline(args),167 args.FromChainID,168 )169 args.Input = (*hexutil.Bytes)(&input) // input170 args.To = b.GetRouterContract(multichainToken) // to171 args.SwapValue = amount // swapValue172 return nil173}174func calcSwapDeadline(args *tokens.BuildTxArgs) int64 {175 var deadline int64176 if args.Extra != nil && args.Extra.EthExtra != nil {177 deadline = args.Extra.EthExtra.Deadline178 } else if serverCfg := params.GetRouterServerConfig(); serverCfg != nil {179 offset := serverCfg.SwapDeadlineOffset180 if offset == 0 {181 offset = defSwapDeadlineOffset182 }183 deadline = time.Now().Unix() + offset184 getOrInitEthExtra(args).Deadline = deadline185 }186 return deadline187}188func (b *Bridge) getReceiverAndAmount(args *tokens.BuildTxArgs, multichainToken string) (receiver common.Address, amount *big.Int, err error) {189 erc20SwapInfo := args.ERC20SwapInfo190 receiver = common.HexToAddress(args.Bind)191 if receiver == (common.Address{}) || !common.IsHexAddress(args.Bind) {192 log.Warn("swapout to wrong receiver", "receiver", args.Bind)193 return receiver, amount, errors.New("can not swapout to empty or invalid receiver")194 }195 fromBridge := router.GetBridgeByChainID(args.FromChainID.String())196 if fromBridge == nil {197 return receiver, amount, tokens.ErrNoBridgeForChainID198 }199 fromTokenCfg := fromBridge.GetTokenConfig(erc20SwapInfo.Token)200 if fromTokenCfg == nil {201 log.Warn("get token config failed", "chainID", args.FromChainID, "token", erc20SwapInfo.Token)202 return receiver, amount, tokens.ErrMissTokenConfig203 }204 toTokenCfg := b.GetTokenConfig(multichainToken)205 if toTokenCfg == nil {206 return receiver, amount, tokens.ErrMissTokenConfig207 }208 amount = tokens.CalcSwapValue(erc20SwapInfo.TokenID, args.FromChainID.String(), b.ChainConfig.ChainID, args.OriginValue, fromTokenCfg.Decimals, toTokenCfg.Decimals, args.OriginFrom, args.OriginTxTo)209 return receiver, amount, err210}211func toAddresses(path []string) []common.Address {212 addresses := make([]common.Address, len(path))213 for i, addr := range path {214 addresses[i] = common.HexToAddress(addr)215 }216 return addresses217}...
mem_freebsd.go
Source:mem_freebsd.go
...68 ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.069 return ret, nil70}71// Return swapinfo72func SwapMemory() (*SwapMemoryStat, error) {73 return SwapMemoryWithContext(context.Background())74}75// Constants from vm/vm_param.h76// nolint: golint77const (78 XSWDEV_VERSION11 = 179 XSWDEV_VERSION = 280)81// Types from vm/vm_param.h82type xswdev struct {83 Version uint32 // Version is the version84 Dev uint64 // Dev is the device identifier85 Flags int32 // Flags is the swap flags applied to the device86 NBlks int32 // NBlks is the total number of blocks87 Used int32 // Used is the number of blocks used88}89// xswdev11 is a compatibility for under FreeBSD 1190// sys/vm/swap_pager.c91type xswdev11 struct {92 Version uint32 // Version is the version93 Dev uint32 // Dev is the device identifier94 Flags int32 // Flags is the swap flags applied to the device95 NBlks int32 // NBlks is the total number of blocks96 Used int32 // Used is the number of blocks used97}98func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {99 // FreeBSD can have multiple swap devices so we total them up100 i, err := common.SysctlUint("vm.nswapdev")101 if err != nil {102 return nil, err103 }104 if i == 0 {105 return nil, errors.New("no swap devices found")106 }107 c := int(i)108 i, err = common.SysctlUint("vm.stats.vm.v_page_size")109 if err != nil {110 return nil, err111 }112 pageSize := i113 var buf []byte114 s := &SwapMemoryStat{}115 for n := 0; n < c; n++ {116 buf, err = unix.SysctlRaw("vm.swap_info", n)117 if err != nil {118 return nil, err119 }120 // first, try to parse with version 2121 xsw := (*xswdev)(unsafe.Pointer(&buf[0]))122 if xsw.Version == XSWDEV_VERSION11 {123 // this is version 1, so try to parse again124 xsw := (*xswdev11)(unsafe.Pointer(&buf[0]))125 if xsw.Version != XSWDEV_VERSION11 {126 return nil, errors.New("xswdev version mismatch(11)")127 }128 s.Total += uint64(xsw.NBlks)...
swap.go
Source:swap.go
...11 hash lntypes.Hash12 height int3213 log *swap.PrefixLog14 lastUpdateTime time.Time15 cost loopdb.SwapCost16 state loopdb.SwapState17 contract *loopdb.SwapContract18 swapType swap.Type19 swapConfig20}21func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,22 contract *loopdb.SwapContract) *swapKit {23 log := &swap.PrefixLog{24 Hash: hash,25 Logger: log,26 }27 return &swapKit{28 swapConfig: *cfg,29 hash: hash,30 log: log,31 state: loopdb.StateInitiated,32 contract: contract,33 swapType: swapType,34 }35}36// GetHtlcScriptVersion returns the correct HTLC script version for the passed37// protocol version.38func GetHtlcScriptVersion(39 protocolVersion loopdb.ProtocolVersion) swap.ScriptVersion {40 if protocolVersion != loopdb.ProtocolVersionUnrecorded &&41 protocolVersion >= loopdb.ProtocolVersionHtlcV2 {42 // Use HTLC v2 script only if we know the swap was initiated43 // with a client that supports HTLC v2. Unrecorded protocol44 // version implies that there was no protocol version stored45 // along side a serialized swap that we're resuming in which46 // case the swap was initiated with HTLC v1 script.47 return swap.HtlcV248 }49 return swap.HtlcV150}51// getHtlc composes and returns the on-chain swap script.52func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {53 return swap.NewHtlc(54 GetHtlcScriptVersion(s.contract.ProtocolVersion),55 s.contract.CltvExpiry, s.contract.SenderKey,56 s.contract.ReceiverKey, s.hash, outputType,57 s.swapConfig.lnd.ChainParams,58 )59}60// swapInfo constructs and returns a filled SwapInfo from61// the swapKit.62func (s *swapKit) swapInfo() *SwapInfo {63 return &SwapInfo{64 SwapContract: *s.contract,65 SwapHash: s.hash,66 SwapType: s.swapType,67 LastUpdate: s.lastUpdateTime,68 SwapStateData: loopdb.SwapStateData{69 State: s.state,70 Cost: s.cost,71 },72 }73}74type genericSwap interface {75 execute(mainCtx context.Context, cfg *executeConfig,76 height int32) error77}78type swapConfig struct {79 lnd *lndclient.LndServices80 store loopdb.SwapStore81 server swapServerClient82}83func newSwapConfig(lnd *lndclient.LndServices, store loopdb.SwapStore,84 server swapServerClient) *swapConfig {85 return &swapConfig{86 lnd: lnd,87 store: store,88 server: server,89 }90}...
Swap
Using AI Code Generation
1import (2func main() {3 v1 := semver.MustParse("1.2.3")4 v2 := semver.MustParse("1.2.4")5 v1.Swap(v2)6 fmt.Println(v1)7 fmt.Println(v2)8}
Swap
Using AI Code Generation
1import (2func main() {3 v1 := version.New(1, 2, 3)4 v2 := version.New(4, 5, 6)5 fmt.Println(v1, v2)6 v1.Swap(v2)7 fmt.Println(v1, v2)8}
Swap
Using AI Code Generation
1import (2func main() {3 a := version{major: 1, minor: 2, patch: 3}4 b := version{major: 4, minor: 5, patch: 6}5 fmt.Println(a)6 fmt.Println(b)7 a.Swap(&b)8 fmt.Println(a)9 fmt.Println(b)10}11{1 2 3}12{4 5 6}13{4 5 6}14{1 2 3}15import (16func main() {17 a := version{major: 1, minor: 2, patch: 3}18 b := version{major: 4, minor: 5, patch: 6}19 fmt.Println(a)20 fmt.Println(b)21 fmt.Println(a.Compare(b))22}23{1 2 3}24{4 5 6}25import (26func main() {27 a := version{major: 1, minor: 2, patch: 3}28 b := version{major: 4, minor: 5, patch: 6}29 fmt.Println(a)30 fmt.Println(b)31 fmt.Println(a.IsEqual(b))32}33{1 2 3}34{4 5 6}35import (36func main() {37 a := version{major: 1, minor: 2, patch: 3}38 b := version{major: 4, minor: 5, patch: 6}39 fmt.Println(a)40 fmt.Println(b)41 fmt.Println(a.IsGreaterThan(b))42}43{1 2 3}44{4 5 6}45import (46func main() {47 a := version{major: 1, minor: 2, patch: 3}48 b := version{major:
Swap
Using AI Code Generation
1import (2func main() {3v1 := version.NewVersion(1, 2, 3)4v2 := version.NewVersion(4, 5, 6)5fmt.Println("Before swap: v1 = ", v1, " v2 = ", v2)6v1.Swap(v2)7fmt.Println("After swap: v1 = ", v1, " v2 = ", v2)8}
Swap
Using AI Code Generation
1import (2func main() {3 irisData, err := ioutil.ReadFile("iris.csv")4 if err != nil {5 log.Fatal(err)6 }7 irisDF := dataframe.ReadCSV(strings.NewReader(string(irisData)))8 species := irisDF.Col("species")9 swapped := species.Swap(0, 2)10 fmt.Println(swapped)11}
Swap
Using AI Code Generation
1import (2func main() {3 client := appinsights.NewTelemetryClient("YOUR INSTRUMENTATION KEY")4 client.TrackTrace("trace message")5 client.Context().GetInternalLogger().SetTraceOutput(os.Stdout)6 client.Context().GetInternalLogger().SetLevel(appinsights.Verbose)7 client.Flush()8}
Swap
Using AI Code Generation
1import (2func main() {3 var a = []int{10, 20, 30, 40, 50, 60}4 sort.Ints(a)5 fmt.Println(a)6 sort.Sort(sort.Reverse(sort.IntSlice(a)))7 fmt.Println(a)8}
Swap
Using AI Code Generation
1import (2func main() {3 v1 := NewVersion(s1)4 v2 := NewVersion(s2)5 fmt.Println("Before Swap")6 fmt.Println("v1: ", v1)7 fmt.Println("v2: ", v2)8 v1.Swap(v2)9 fmt.Println("After Swap")10 fmt.Println("v1: ", v1)11 fmt.Println("v2: ", v2)12}
Swap
Using AI Code Generation
1import (2func main() {3 v1 := versions.Version{1, 2, 3}4 v2 := versions.Version{4, 5, 6}5 fmt.Println(v1, v2)6 v1.Swap(v2)7 fmt.Println(v1, v2)8}9func (v *Version) Swap(v2 Version) {10}11func (v Version) Swap(v2 Version) {12}13func (v *Version) Swap(v2 *Version) {14}15func (v Version) Swap(v2 *Version) {16}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!