Skip to content

Instantly share code, notes, and snippets.

View jinglescode's full-sized avatar
🎯
stay focus

Hong Jing (Jingles) jinglescode

🎯
stay focus
View GitHub Profile

About transaction building with JSON

The goal is to pass a JSON object to the Rust code, which will build a transaction and return the serialized transaction in hex format. This is used between Mesh and the Rust code.

Instead of build transactions directly with cardano-serialization-lib, we build the whisky package that wraps the cardano-serialization-lib package. Doing so, this allow us to create useful functions that can be used in the Rust code base.

Then, we create a JSON parser in Rust that can parse JSON objects and build transactions with the whisky package.

Example

{
"type": "PlutusScriptV2",
"description": "",
"cborHex": "5907655907620100003232323232323232323232323232332232323232322232325335320193333573466e1cd55cea80124000466442466002006004646464646464646464646464646666ae68cdc39aab9d500c480008cccccccccccc88888888888848cccccccccccc00403403002c02802402001c01801401000c008cd4050054d5d0a80619a80a00a9aba1500b33501401635742a014666aa030eb9405cd5d0a804999aa80c3ae501735742a01066a02803e6ae85401cccd54060081d69aba150063232323333573466e1cd55cea801240004664424660020060046464646666ae68cdc39aab9d5002480008cc8848cc00400c008cd40a9d69aba15002302b357426ae8940088c98c80b4cd5ce01701681589aab9e5001137540026ae854008c8c8c8cccd5cd19b8735573aa004900011991091980080180119a8153ad35742a00460566ae84d5d1280111931901699ab9c02e02d02b135573ca00226ea8004d5d09aba2500223263202933573805405204e26aae7940044dd50009aba1500533501475c6ae854010ccd540600708004d5d0a801999aa80c3ae200135742a004603c6ae84d5d1280111931901299ab9c026025023135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba250
for epoch in range(n_epochs):
for real_samples, _ in tqdm(dataloader):
batch_size = len(real_samples)
real_samples = real_samples.view(batch_size, -1).to(device)
# train discriminator
discriminator_optim.zero_grad()
discriminator_loss = get_discriminator_loss(generator_net, discriminator_net, criterion, real_samples, batch_size, dim_noise, device)
discriminator_loss.backward(retain_graph=True)
def get_generator_loss(generator, discriminator, criterion, n_samples, dim_noise, device):
'''
Generator generates and get discriminator's loss
Parameters:
generator:
generator network
discriminator:
discriminator network
criterion:
loss function, likely `nn.BCEWithLogitsLoss()`
def get_discriminator_loss(generator, discriminator, criterion, real_samples, n_samples, dim_noise, device):
'''
Discriminator predict and get loss
Parameters:
generator:
generator network
discriminator:
discriminator network
criterion:
loss function, likely `nn.BCEWithLogitsLoss()`
class Generator(nn.Module):
'''
Generator Class
Parameters:
dim_noise: int, default: 10
the dimension of the noise vector
in_dim: int, default: 784
the dimension of the images, fitted for the dataset used
(MNIST images are 28x28, so 784 so is the default)
hidden_dim: int, default: 128
class Discriminator(nn.Module):
'''
Discriminator Class
Parameters:
in_dim: int, default: 784
the dimension of the input (MNIST images are 28x28, so 784 so is the default)
hidden_dim: int, default: 128
the inner dimension
out_dim: int, default: 1
default 1 because we returns a 1-dimension tensor representing fake/real
class TestConv1d(nn.Module):
def __init__(self):
super(TestConv1d, self).__init__()
self.conv = nn.Conv1d(in_channels=2, out_channels=4, kernel_size=1, groups=2, bias=False)
self.init_weights()
def forward(self, x):
return self.conv(x)
def init_weights(self):
class TestConv1d(nn.Module):
def __init__(self):
super(TestConv1d, self).__init__()
self.conv = nn.Conv1d(in_channels=2, out_channels=2, kernel_size=1, groups=2, bias=False)
self.init_weights()
def forward(self, x):
return self.conv(x)
def init_weights(self):
class TestConv1d(nn.Module):
def __init__(self):
super(TestConv1d, self).__init__()
self.conv = nn.Conv1d(in_channels=1, out_channels=1, kernel_size=3, dilation=2, bias=False)
self.init_weights()
def forward(self, x):
return self.conv(x)
def init_weights(self):