Skip to content

Instantly share code, notes, and snippets.

@EvanJRichard
Created March 30, 2020 19:14
Show Gist options
  • Save EvanJRichard/51b4d46900a01eab0e4eea149a008c9b to your computer and use it in GitHub Desktop.
Save EvanJRichard/51b4d46900a01eab0e4eea149a008c9b to your computer and use it in GitHub Desktop.
jason's update, which fixes the behavior
// GetSwapAssetsTransaction is a
// Utility function to create the two grouped transactions
// This should be replaced in the future with LimitOrder
// GetSwapAsssetsTransaction function
func GetSwapAssetsTransaction(assetAmount uint64, microAlgoAmount uint64, contract, secretKey []byte, params types.SuggestedParams, owner string, assetID uint64) ([]byte, error) {
var buyerAddress types.Address
copy(buyerAddress[:], secretKey[32:])
contractAddress := crypto.AddressFromProgram(contract)
algosForAssets, err := future.MakePaymentTxn(contractAddress.String(), buyerAddress.String(), microAlgoAmount, nil, "", params)
if err != nil {
return nil, err
}
assetsForAlgos, err := future.MakeAssetTransferTxn(buyerAddress.String(), owner, assetAmount, nil, params, "", assetID)
if err != nil {
return nil, err
}
gid, err := crypto.ComputeGroupID([]types.Transaction{algosForAssets, assetsForAlgos})
if err != nil {
return nil, err
}
algosForAssets.Group = gid
assetsForAlgos.Group = gid
logicSig, err := crypto.MakeLogicSig(contract, nil, nil, crypto.MultisigAccount{})
if err != nil {
return nil, err
}
_, algosForAssetsSigned, err := crypto.SignLogicsigTransaction(logicSig, algosForAssets)
if err != nil {
return nil, err
}
_, assetsForAlgosSigned, err := crypto.SignTransaction(secretKey, assetsForAlgos)
if err != nil {
return nil, err
}
var signedGroup []byte
signedGroup = append(signedGroup, algosForAssetsSigned...)
signedGroup = append(signedGroup, assetsForAlgosSigned...)
return signedGroup, nil
}
// This example creates a contract account
func main() {
// CHANGE ME
const algodAddress = "http://localhost:8080"
const algodToken = "f1dee49e36a82face92fdb21cd3d340a1b369925cd12f3ee7371378f1665b9b1"
// Create an algod client
algodClient, err := algod.MakeClient(algodAddress, algodToken)
if err != nil {
fmt.Printf("failed to make algod client: %s\n", err)
return
}
// Inputs
owner := "AJNNFQN7DSR7QEY766V7JDG35OPM53ZSNF7CU264AWOOUGSZBMLMSKCRIU"
// Recover accounts used in example
// Account 1 is the asset owner
// Used later in the example
assetOwnerMnemonic := "portion never forward pill lunch organ biology" +
" weird catch curve isolate plug innocent skin grunt" +
" bounce clown mercy hole eagle soul chunk type absorb trim"
sk1, _ := mnemonic.ToPrivateKey(assetOwnerMnemonic)
// Inputs
// Limit contract should be two receivers in an
// ratn/ratd ratio of assetid to microalgos
// ratn is the number of assets
// ratd is the number of microAlgos
ratn := 1
ratd := 3000
expiryRound := uint64(5000000)
maxFee := uint64(2000)
minTrade := 2999
// assetID is the asset id number
// of the asset to be traded
assetID := 319290;
//Instaniate the Template
limit, err := templates.MakeLimitOrder(owner, uint64(assetID), uint64(ratn), uint64(ratd), expiryRound, uint64(minTrade), maxFee)
if err != nil {
fmt.Printf("Creating Contract Failed with %v\n", err)
}
// At this point the contract address can be funded
// The program bytes can also be saved off
// to be used at a later time or in a
// different application
program := limit.GetProgram()
addr := limit.GetAddress()
fmt.Printf("Escrow Address: %s\n" , addr )
// Create a set of grouped transactions against
// the limit contract
sp, err := algodClient.BuildSuggestedParams()
if err != nil {
fmt.Printf("Failed to Build Suggested Parameters with %v\n", err)
}
assetAmount := 1
microAlgoAmount := 3000
sp.FlatFee = true
sp.Fee = 1000
txnBytes, err := GetSwapAssetsTransaction(uint64(assetAmount), uint64(microAlgoAmount), program, sk1, sp, owner, uint64(assetID))
if err != nil {
fmt.Printf("Creating Transactions Failed with %v\n", err)
}
txIDResponse, err := algodClient.SendRawTransaction(txnBytes)
if err != nil {
fmt.Printf("Sending failed with %v\n", err)
}
fmt.Printf("Transaction: %v\n", txIDResponse.TxID)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment