Skip to content

Instantly share code, notes, and snippets.

@HighlyHybrid
Last active August 19, 2020 14:59
Show Gist options
  • Save HighlyHybrid/f42f33e98d1475614018722d4e36e8f7 to your computer and use it in GitHub Desktop.
Save HighlyHybrid/f42f33e98d1475614018722d4e36e8f7 to your computer and use it in GitHub Desktop.
Swift-EOSIO Atomic Assets
import EOSIO
enum AtomicEnum: ABIEncodable {
case string(String)
case uint8(UInt8)
case uint32(UInt32)
enum CodingKeys: CodingKey {
case string, uint8, uint32
}
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
switch self {
case .string(let str):
try container.encode("string")
try container.encode(str)
case .uint8(let int8):
try container.encode("uint8")
try container.encode(int8)
case .uint32(let int32):
try container.encode("uint32")
try container.encode(int32)
}
}
public func abiEncode(to encoder: ABIEncoder) throws {
var container = encoder.unkeyedContainer()
switch self {
case .string(let str):
try container.encode(0 as UInt8)
try container.encode(str)
case .uint8(let int):
try container.encode(1 as UInt8)
try container.encode(int)
case .uint32(let int):
try container.encode(2 as UInt8)
try container.encode(int)
}
}
} // END OF ENUM
struct AtomicAttribute: ABIEncodable {
var key: String
var value: AtomicEnum
}
// ATOMIC WRITE DATA //
struct MintingData: ABIEncodable {
var authorized_minter: String
var collection_name: String
var schema_name: String
var template_id: Int32
var new_asset_owner: String
var immutable_data: [AtomicAttribute]
var mutable_data: [AtomicAttribute]
var tokens_to_back: [String]
}
// ATOMIC WRITE FUNCTION
func atomicWriteFunc() {
// STRUCT VARIANTS
let nameVariant = AtomicEnum.string(atomicName)
let imgVariant = AtomicEnum.string(IPFS)
let resonanceVariant = AtomicEnum.uint32(UInt32(hybridSporeResonance))
let homeVariant = AtomicEnum.string(globalHome)
let earthGuildVariant = AtomicEnum.string(globalEarthGuild)
let elementVariant = AtomicEnum.string(elementalSign)
let stepsVariant = AtomicEnum.uint32(UInt32(totalSteps))
let speedVariant = AtomicEnum.string(reflexClass)
let aimVariant = AtomicEnum.string(accuracyClass)
let forestVariant = AtomicEnum.uint8(UInt8(forestSporeScore))
let riverVariant = AtomicEnum.uint8(UInt8(riverSporeScore))
let fieldVariant = AtomicEnum.uint8(UInt8(fieldSporeScore))
let sunVariant = AtomicEnum.uint8(UInt8(sunSporeScore))
let shadowVariant = AtomicEnum.uint8(UInt8(shadowSporeScore))
let gravityVariant = AtomicEnum.uint8(UInt8(gravitySporeScore))
let skyVariant = AtomicEnum.uint8(UInt8(skySporeScore))
let quantumVariant = AtomicEnum.uint8(UInt8(quantumSporeScore))
let creatorIdVariant = AtomicEnum.string(creatorID)
let beaconVariant = AtomicEnum.string(beaconString)
// STRUCT ATTRIBUTES
let nameAttribute = AtomicAttribute(key: "name", value: nameVariant)
let imgAttribute = AtomicAttribute(key: "img", value: imgVariant)
let resonanceAttribute = AtomicAttribute(key: "resonance", value: resonanceVariant)
let homeAttribute = AtomicAttribute(key: "home", value: homeVariant)
let earthGuildAttribute = AtomicAttribute(key: "earth guild", value: earthGuildVariant)
let elementAttribute = AtomicAttribute(key: "element", value: elementVariant)
let stepsAttribute = AtomicAttribute(key: "steps", value: stepsVariant)
let speedAttribute = AtomicAttribute(key: "speed class", value: speedVariant)
let aimAttribute = AtomicAttribute(key: "aim class", value: aimVariant)
let forestAttribute = AtomicAttribute(key: "forest", value: forestVariant)
let riverAttribute = AtomicAttribute(key: "river", value: riverVariant)
let fieldAttribute = AtomicAttribute(key: "field", value: fieldVariant)
let sunAttribute = AtomicAttribute(key: "sun", value: sunVariant)
let shadowAttribute = AtomicAttribute(key: "shadow", value: shadowVariant)
let gravityAttribute = AtomicAttribute(key: "gravity", value: gravityVariant)
let skyAttribute = AtomicAttribute(key: "sky", value: skyVariant)
let quantumAttribute = AtomicAttribute(key: "quantum", value: quantumVariant)
let creatorIdAttribute = AtomicAttribute(key: "creator ID", value: creatorIdVariant)
let beaconAttribute = AtomicAttribute(key: "beacon", value: beaconVariant)
// ATTRIBUTE MAPS
attributeMap = [nameAttribute, imgAttribute, resonanceAttribute, homeAttribute, earthGuildAttribute, elementAttribute, stepsAttribute, speedAttribute, aimAttribute, forestAttribute, riverAttribute, fieldAttribute, sunAttribute, shadowAttribute, gravityAttribute, skyAttribute, quantumAttribute, creatorIdAttribute]
mutableMap = [beaconAttribute]
print("ATTRIBUTE MAP: \(attributeMap)")
print("MUTABLE MAP: \(mutableMap)")
// SEND DATA
pushTransaction()
}
// PUSH TRANSACTION FUNCTION
func pushTransaction() {
let nodeAddress = URL(string: pinkNetwork)!
let client = Client(address: nodeAddress, session: URLSession.shared)
let key = PrivateKey(privateKey)
let mint = MintingData(authorized_minter: minter, collection_name: collection, schema_name: schema, template_id: atomicTemplateId, new_asset_owner: recipient, immutable_data: attributeMap, mutable_data: mutableMap, tokens_to_back: [])
let action = try! Action(
account: "atomicassets",
name: "mintasset",
authorization: [PermissionLevel(Name(minter), "active")],
value: mint
)
let info = try! client.sendSync(API.V1.Chain.GetInfo()).get()
let expiration = info.headBlockTime.addingTimeInterval(60)
let header = TransactionHeader(expiration: TimePointSec(expiration),
refBlockId: info.lastIrreversibleBlockId)
let transaction = Transaction(header, actions: [action])
let signature = try! key!.sign(transaction, using: info.chainId)
let signedTransaction = SignedTransaction(transaction, signatures: [signature])
let req = API.V1.Chain.PushTransaction(signedTransaction)
let res = try! client.sendSync(req).get()
Timer.after(4.seconds) {
if res.transactionId != "" { print("transaction went through") } else { print("no transaction") }
print("TRANSACTION ID: \(res.transactionId)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment